Registration

This commit is contained in:
Gergely Polonkai 2018-06-29 14:00:45 +02:00
parent 57bc0b2a77
commit bc47310cfb
5 changed files with 76 additions and 1 deletions

View File

@ -16,7 +16,7 @@
import os import os
from flask import Flask, render_template from flask import Flask, current_app, redirect, render_template, url_for
from flask_babel import Babel from flask_babel import Babel
from flask_security import SQLAlchemyUserDatastore, current_user from flask_security import SQLAlchemyUserDatastore, current_user
@ -50,5 +50,28 @@ def hello():
return render_template('index.html') return render_template('index.html')
@app.route('/register', methods=['POST', 'GET'])
def register():
if not current_app.config['REGISTRATION_ENABLED']:
return render_template('registration-disabled.html')
from app.forms import RegistrationForm
from app.models import db, User
form = RegistrationForm()
if form.validate_on_submit():
# TODO: This might become False later, if we want registrations to be confirmed via E-mail
user = User(active=True)
form.populate_obj(user)
db.session.add(user)
db.session.commit()
return redirect(url_for('hello'))
return render_template('registration.html', form=form)
if __name__ == '__main__': if __name__ == '__main__':
app.run() app.run()

View File

@ -1,7 +1,10 @@
DEBUG = True DEBUG = True
ENV = 'dev' ENV = 'dev'
REGISTRATION_ENABLED = True
SQLALCHEMY_DATABASE_URI = 'sqlite:///local.db' SQLALCHEMY_DATABASE_URI = 'sqlite:///local.db'
SQLALCHEMY_TRACK_MODIFICATIONS = False SQLALCHEMY_TRACK_MODIFICATIONS = False
SECRET_KEY = 'ThisIsNotSoSecret' SECRET_KEY = 'ThisIsNotSoSecret'
SECURITY_PASSWORD_HASH = 'bcrypt' SECURITY_PASSWORD_HASH = 'bcrypt'
SECURITY_PASSWORD_SALT = SECRET_KEY SECURITY_PASSWORD_SALT = SECRET_KEY
SECURITY_REGISTERABLE = False

15
app/forms.py Normal file
View File

@ -0,0 +1,15 @@
from flask_babel import lazy_gettext as _
from flask_wtf import FlaskForm
from wtforms import PasswordField, StringField
from wtforms.validators import DataRequired, Email, ValidationError
class RegistrationForm(FlaskForm):
username = StringField(_('Username'), validators=[DataRequired()])
email = StringField(_('Email address'), validators=[Email()])
password = PasswordField(_('Password'), validators=[DataRequired()])
password_retype = PasswordField(_('Password, once more'), validators=[DataRequired()])
def validate_password_retype(self, field):
if field.data != self.password.data:
raise ValidationError(_('The two passwords must match!'))

View File

@ -0,0 +1,5 @@
{% extends 'base.html' %}
{% block content %}
<h1>Registration is disabled on this instance</h1>
{% endblock content %}

View File

@ -0,0 +1,29 @@
{% extends 'base.html' %}
{% block content %}
<form method="post">
{{ form.hidden_tag() }}
{{ form.username.errors }}
{{ form.username.label }}
{{ form.username }}
<br>
{{ form.email.errors }}
{{ form.email.label }}
{{ form.email }}
<br>
{{ form.password.errors }}
{{ form.password.label }}
{{ form.password }}
<br>
{{ form.password_retype.errors }}
{{ form.password_retype.label }}
{{ form.password_retype }}
<br>
<button type="submit">{% trans %}Register{% endtrans %}</button>
</form>
{% endblock %}