Skip to content

Commit

Permalink
[account,auth][m]: (fixes #2) working user accounts with registration…
Browse files Browse the repository at this point in the history
…, login and logout.

* TODO: issue with showing user info in top nav as mako system does not support context_processor (need to switch to jinja - see #13)
  • Loading branch information
rufuspollock committed Sep 17, 2011
1 parent 04b174d commit 6add8e1
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 0 deletions.
Empty file added bibserver/view/__init__.py
Empty file.
68 changes: 68 additions & 0 deletions bibserver/view/account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from flask import Blueprint, request, url_for, flash, redirect
from flaskext.mako import render_template
from flaskext.login import login_user, logout_user
from flaskext.wtf import Form, TextField, PasswordField, validators

import bibserver.dao as dao

blueprint = Blueprint('account', __name__)


@blueprint.route('/')
def index():
return 'Accounts'


class LoginForm(Form):
username = TextField('Username', [validators.Required()])
password = PasswordField('Password', [validators.Required()])

@blueprint.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm(request.form, csrf_enabled=False)
if request.method == 'POST' and form.validate():
password = form.password.data
username = form.username.data
user = dao.Account.get(username)
if user and user.check_password(password):
login_user(user, remember=True)
flash('Welcome back', 'success')
return redirect(url_for('home'))
else:
flash('Incorrect email/password', 'error')
if request.method == 'POST' and not form.validate():
flash('Invalid form', 'error')
return render_template('account/login.html', form=form)


@blueprint.route('/logout')
def logout():
logout_user()
flash('You are now logged out', 'success')
return redirect(url_for('home'))


class RegisterForm(Form):
username = TextField('Username', [validators.Length(min=3, max=25)])
email = TextField('Email Address', [validators.Length(min=3, max=35)])
password = PasswordField('New Password', [
validators.Required(),
validators.EqualTo('confirm', message='Passwords must match')
])
confirm = PasswordField('Repeat Password')

@blueprint.route('/register', methods=['GET', 'POST'])
def register():
# TODO: re-enable csrf
form = RegisterForm(request.form, csrf_enabled=False)
if request.method == 'POST' and form.validate():
account = dao.Account(id=form.username.data, email=form.email.data)
account.set_password(form.password.data)
account.save()
login_user(account, remember=True)
flash('Thanks for signing-up', 'success')
return redirect(url_for('home'))
if request.method == 'POST' and not form.validate():
flash('Please correct the errors', 'error')
return render_template('account/register.html', form=form)

3 changes: 3 additions & 0 deletions bibserver/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import bibserver.iomanager
import bibserver.importer
from bibserver.core import app, login_manager
from bibserver.view.account import blueprint as account

app.register_blueprint(account, url_prefix='/account')


# NB: the decorator appears to kill the function for normal usage
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# need Flask-Mako from source. See README.rst.
"Flask-Mako",
"Flask-Login",
"Flask-WTF",
"pyes==0.16",
# need solrpy from HEAD. See README.rst.
"solrpy",
Expand Down
14 changes: 14 additions & 0 deletions templates/_formhelpers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<%def name="render_field(field, **kwargs)">
${ field.label }
<div class="input">
${ field(**kwargs) }
% if field.errors:
<ul class="errors">
% for error in field.errors:
<li>${ error }</li>
% endfor
</ul>
% endif
</div>
</%def>

10 changes: 10 additions & 0 deletions templates/account/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%include file="/header.html"/>
<%namespace name="helpers" file="/_formhelpers.html" />

<form method="post" action="">
${ helpers.render_field(form.username, placeholder="your username") }
${ helpers.render_field(form.password, placeholder="********") }
<input type="submit" value="Login" class="btn" />
</form>

<%include file="/footer.html"/>
10 changes: 10 additions & 0 deletions templates/account/register.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%include file="/header.html"/>
<%namespace name="helpers" file="/_formhelpers.html" />

<form method="post" action="${url_for('account.register')}">
${ helpers.render_field(form.username, placeholder="myusername") }
${ helpers.render_field(form.email, placeholder="hello@mywebsite.org") }
${ helpers.render_field(form.password, placeholder="********") }
${ helpers.render_field(form.confirm, placeholder="********") }
<input type="submit" value="Signup" class="btn" />
</form>

0 comments on commit 6add8e1

Please sign in to comment.