diff --git a/app/__init__.py b/app/__init__.py index dbf7f61..d22d209 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -16,7 +16,7 @@ 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_security import SQLAlchemyUserDatastore, current_user @@ -50,5 +50,28 @@ def hello(): 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__': app.run() diff --git a/app/config_dev.py b/app/config_dev.py index 7c5d90b..51f0fff 100644 --- a/app/config_dev.py +++ b/app/config_dev.py @@ -1,7 +1,10 @@ DEBUG = True ENV = 'dev' +REGISTRATION_ENABLED = True + SQLALCHEMY_DATABASE_URI = 'sqlite:///local.db' SQLALCHEMY_TRACK_MODIFICATIONS = False SECRET_KEY = 'ThisIsNotSoSecret' SECURITY_PASSWORD_HASH = 'bcrypt' SECURITY_PASSWORD_SALT = SECRET_KEY +SECURITY_REGISTERABLE = False diff --git a/app/forms.py b/app/forms.py new file mode 100644 index 0000000..dd5e3d8 --- /dev/null +++ b/app/forms.py @@ -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!')) diff --git a/app/templates/registration-disabled.html b/app/templates/registration-disabled.html new file mode 100644 index 0000000..56316f2 --- /dev/null +++ b/app/templates/registration-disabled.html @@ -0,0 +1,5 @@ +{% extends 'base.html' %} + +{% block content %} +

Registration is disabled on this instance

+{% endblock content %} diff --git a/app/templates/registration.html b/app/templates/registration.html new file mode 100644 index 0000000..cb1a69c --- /dev/null +++ b/app/templates/registration.html @@ -0,0 +1,29 @@ +{% extends 'base.html' %} + +{% block content %} +
+ {{ form.hidden_tag() }} + + {{ form.username.errors }} + {{ form.username.label }} + {{ form.username }} +
+ + {{ form.email.errors }} + {{ form.email.label }} + {{ form.email }} +
+ + {{ form.password.errors }} + {{ form.password.label }} + {{ form.password }} +
+ + {{ form.password_retype.errors }} + {{ form.password_retype.label }} + {{ form.password_retype }} +
+ + +
+{% endblock %}