From 52a22d418ac6e5d6b6d12f7718e751918886d11e Mon Sep 17 00:00:00 2001 From: kelepirci Date: Sat, 16 Jul 2016 21:14:08 +0300 Subject: [PATCH] Added registration page and Flask-DebugToolbar --- config.py | 4 +++ dash/__init__.py | 4 +++ dash/auth/forms.py | 10 ++++--- dash/auth/views.py | 18 +++++++++-- dash/templates/auth/login.html | 9 ++++++ dash/templates/auth/register.html | 50 +++++++++++++++++++++++-------- 6 files changed, 76 insertions(+), 19 deletions(-) diff --git a/config.py b/config.py index 0e4204e..9f88153 100644 --- a/config.py +++ b/config.py @@ -24,6 +24,10 @@ class DevelopmentConfig(Config): SQLALCHEMY_TRACK_MODIFICATIONS = True WTF_CSRF_ENABLED = True SECRET_KEY = 'you-will-never-guess' + # Debug configuration + FLASK_DEBUG = True + SQLALCHEMY_ECHO = True + DEBUG_TB_INTERCEPT_REDIRECTS = False class TestingConfig(Config): TESTING = True diff --git a/dash/__init__.py b/dash/__init__.py index 5246778..64838d0 100644 --- a/dash/__init__.py +++ b/dash/__init__.py @@ -5,13 +5,16 @@ from flask_mail import Mail from flask_moment import Moment from flask_sqlalchemy import SQLAlchemy from flask_login import LoginManager +from flask_debugtoolbar import DebugToolbarExtension from config import config + AdminLTE = AdminLTE() mail = Mail() moment = Moment() db = SQLAlchemy() +toolbar = DebugToolbarExtension() # initialize flask_login login_manager = LoginManager() @@ -34,6 +37,7 @@ def create_app(config_name): moment.init_app(dash) db.init_app(dash) login_manager.init_app(dash) + toolbar.init_app(dash) # attach routes and custom error pages here diff --git a/dash/auth/forms.py b/dash/auth/forms.py index 457851c..024a1ce 100644 --- a/dash/auth/forms.py +++ b/dash/auth/forms.py @@ -1,7 +1,7 @@ from flask_wtf import Form -from wtforms import StringField, PasswordField, BooleanField, SubmitField +from flask import flash +from wtforms import StringField, PasswordField, BooleanField, SubmitField, ValidationError from wtforms.validators import Required, Length, Email, Regexp, EqualTo -from wtforms import ValidationError from ..models import User class LoginForm(Form): @@ -18,15 +18,17 @@ class RegistrationForm(Form): Required(), Length(1, 64), Regexp('^[A-Za-z][A-Za-z0-9_.]*$', 0, 'Usernames must have only letters, ' 'numbers, dots or underscores')]) + full_name = StringField('Full name', validators=[Required(), Length(1, 255)]) password = PasswordField('Password', validators=[ Required(), EqualTo('password2', message='Passwords must match.')]) password2 = PasswordField('Confirm password', validators=[Required()]) + terms = BooleanField('I agree to the terms', validators=[Required(message='You must agree terms.')]) submit = SubmitField('Register') - + def validate_email(self, field): if User.query.filter_by(email=field.data).first(): raise ValidationError('Email already registered.') - + def validate_username(self, field): if User.query.filter_by(username=field.data).first(): raise ValidationError('Username already in use.') \ No newline at end of file diff --git a/dash/auth/views.py b/dash/auth/views.py index bec7031..52a4abd 100644 --- a/dash/auth/views.py +++ b/dash/auth/views.py @@ -1,7 +1,11 @@ +import datetime from flask import render_template, redirect, request, url_for, flash -from flask_login import login_user, logout_user, login_required +from flask_login import login_user, logout_user, login_required, \ + current_user from . import auth +from .. import db from ..models import User +from ..email import send_email from .forms import LoginForm, RegistrationForm @@ -23,7 +27,17 @@ def logout(): flash('You have been logged out.') return redirect(url_for('main.index')) -@auth.route('/register') +@auth.route('/register', methods=['GET', 'POST']) def register(): form = RegistrationForm() + if form.validate_on_submit(): + user = User(email=form.email.data, + username=form.username.data, + full_name=form.full_name.data, + password=form.password.data, + avatar="/static/img/user2-160x160.jpg", + created_at=datetime.datetime.now()) + db.session.add(user) + flash('You can now login.') + return redirect(url_for('auth.login')) return render_template('auth/register.html', form=form) \ No newline at end of file diff --git a/dash/templates/auth/login.html b/dash/templates/auth/login.html index 27ca879..39f78aa 100644 --- a/dash/templates/auth/login.html +++ b/dash/templates/auth/login.html @@ -42,6 +42,15 @@ +
+
+
+

If you do not have account?

+ + + +
+
diff --git a/dash/templates/auth/register.html b/dash/templates/auth/register.html index 91b441b..6e46ecf 100644 --- a/dash/templates/auth/register.html +++ b/dash/templates/auth/register.html @@ -2,7 +2,7 @@ {% block title %}Register{% endblock %} {% block description %}Register a new account{% endblock %} -{% block bodytag %}register-page{% endblock %} +{% block bodytag %}login-page{% endblock %} {% block body %} @@ -24,35 +24,55 @@ {% endif %} {% endwith %} {# Render the login form. #} -
+ {{ form.hidden_tag() }}
- + + + {% if form.email.errors %} + {% for error in form.email.errors %} {{ error }} {% endfor %} + {% endif %} +
+
+ + + {% if form.username.errors %} + {% for error in form.username.errors %} {{ error }} {% endfor %} + {% endif %} +
+
+
- - -
-
- + + {% if form.password.errors %} + {% for error in form.password.errors %} {{ error }} {% endfor %} + {% endif %}
- + + {% if form.password2.errors %} + {% for error in form.password2.errors %} {{ error }} {% endfor %} + {% endif %}
+ {% if form.terms.errors %} +
+ {% for error in form.terms.errors %} {{ error }} {% endfor %} + {% endif %}
- +
@@ -66,8 +86,12 @@ Google+ --> - - I already have a membership +