Added registration page and Flask-DebugToolbar

This commit is contained in:
kelepirci 2016-07-16 21:14:08 +03:00
parent 02423d94de
commit 52a22d418a
6 changed files with 76 additions and 19 deletions

View File

@ -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

View File

@ -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

View File

@ -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.')

View File

@ -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)

View File

@ -42,6 +42,15 @@
</div>
</div>
</form>
<div class="row">
<div class="col-xs-12 text-center">
<hr />
<h4 class="box-title"> If you do not have account?</h4>
<a href="{{ url_for('auth.register') }}">
<button type="button" class="btn btn-block btn-danger btn-flat">Register Here</button>
</a>
</div>
</div>
</div>
</div>

View File

@ -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 action="" method="post" name="register">
<form action="{{ url_for('auth.register') }}" method="post" name="register">
{{ form.hidden_tag() }}
<div class="form-group has-feedback">
<input type="text" class="form-control" placeholder="Full name">
<input type="email" name="email" class="form-control" placeholder="Email">
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
{% if form.email.errors %}
<span class="text-red">{% for error in form.email.errors %} {{ error }} {% endfor %}</span>
{% endif %}
</div>
<div class="form-group has-feedback">
<input type="text" name="username" class="form-control" placeholder="User name">
<span class="glyphicon glyphicon-user form-control-feedback"></span>
{% if form.username.errors %}
<span class="text-red">{% for error in form.username.errors %} {{ error }} {% endfor %}</span>
{% endif %}
</div>
<div class="form-group has-feedback">
<input type="text" name="full_name" class="form-control" placeholder="Full name">
<span class="glyphicon glyphicon-user form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<input type="email" class="form-control" placeholder="Email">
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<input type="password" class="form-control" placeholder="Password">
<input type="password" name="password" class="form-control" placeholder="Password">
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
{% if form.password.errors %}
<span class="text-red">{% for error in form.password.errors %} {{ error }} {% endfor %}</span>
{% endif %}
</div>
<div class="form-group has-feedback">
<input type="password" class="form-control" placeholder="Retype password">
<input type="password" name="password2" class="form-control" placeholder="Retype password">
<span class="glyphicon glyphicon-log-in form-control-feedback"></span>
{% if form.password2.errors %}
<span class="text-red">{% for error in form.password2.errors %} {{ error }} {% endfor %}</span>
{% endif %}
</div>
<div class="row">
<div class="col-xs-8 col-xs-offset-0">
<div class="checkbox">
<label>
<input type="checkbox"> I agree to the <a href="#">terms</a>
<input type="checkbox" name="terms"> I agree to the <a href="#">terms</a>
</label>
{% if form.terms.errors %}
<br />
<span class="text-red">{% for error in form.terms.errors %} {{ error }} {% endfor %}</span>
{% endif %}
</div>
</div>
<!-- /.col -->
<div class="col-xs-4">
<button type="submit" class="btn btn-primary btn-block btn-flat">Register</button>
<button type="submit" name="submit" class="btn btn-primary btn-block btn-flat" value="Register">Register</button>
</div>
<!-- /.col -->
</div>
@ -66,8 +86,12 @@
Google+</a>
</div>
-->
<a href="login.html" class="text-center">I already have a membership</a>
<div class="row">
<div class="col-xs-12 text-center">
<hr />
<a href="{{ url_for('auth.login') }}">I already have a membership</a>
</div>
</div>
</div>
<!-- /.form-box -->
</div>