Akron | ca9bd98 | 2016-12-06 16:59:57 +0100 | [diff] [blame^] | 1 | import json |
| 2 | |
| 3 | from flask import Blueprint, request, session, render_template, url_for, flash |
| 4 | from flask_email import EmailMessage |
| 5 | from flask_login import logout_user, login_user |
| 6 | from werkzeug.utils import redirect |
| 7 | from flask_babel import lazy_gettext as _ |
| 8 | |
| 9 | import APIFactory |
| 10 | from app import secured |
| 11 | from models import ProfileForm, UserEncoder, SigninForm, SignupForm |
| 12 | import providers |
| 13 | |
| 14 | |
| 15 | __author__ = 'hanl' |
| 16 | |
| 17 | router = Blueprint('user', __name__, |
| 18 | template_folder='templates') |
| 19 | |
| 20 | |
| 21 | @router.route('/', methods=['GET']) |
| 22 | @secured |
| 23 | def index(): |
| 24 | return redirect(url_for('.profile')) |
| 25 | |
| 26 | |
| 27 | @router.route('/profile', methods=['GET', 'POST']) |
| 28 | @secured |
| 29 | def profile(): |
| 30 | user = providers.PROVIDER.get_user(session, True) |
| 31 | form = ProfileForm(obj=user) |
| 32 | if request.method == 'POST': |
| 33 | pass |
| 34 | elif request.method == 'GET': |
| 35 | return render_template('profile.html', form=form, submit=_('Update'), |
| 36 | complete=user.has_details()) |
| 37 | |
| 38 | |
| 39 | @router.route('/profile/<user>', methods=['GET']) |
| 40 | def user_profile(user=None): |
| 41 | user = providers.User(username=user) |
| 42 | form = ProfileForm(obj=user) |
| 43 | if request.method == 'POST': |
| 44 | pass |
| 45 | elif request.method == 'GET': |
| 46 | return render_template('profile.html', form=form, submit=_('Update')) |
| 47 | |
| 48 | |
| 49 | # @router.route('/delete', methods=['POST', 'GET']) |
| 50 | def delete(): |
| 51 | response = APIFactory.request('delete') |
| 52 | pass |
| 53 | |
| 54 | |
| 55 | @router.route('/login', methods=['GET', 'POST']) |
| 56 | def login(): |
| 57 | form = SigninForm() |
| 58 | if request.method == 'POST': |
| 59 | if not form.validate(): |
| 60 | return render_template('login.html', form=form) |
| 61 | else: |
| 62 | user = providers.User(username=form.username.data, password=form.password.data) |
| 63 | success = providers.PROVIDER.login(session, user) |
| 64 | if success: |
| 65 | login_user(user) |
| 66 | else: |
| 67 | return redirect(url_for('.login')) |
| 68 | print "the data serialized %s" % json.dumps(user, cls=UserEncoder) |
| 69 | return redirect(request.args.get('next') or url_for('.profile')) |
| 70 | elif request.method == 'GET': |
| 71 | providers.PROVIDER.is_admin("") |
| 72 | return render_template('login.html', form=form, submit=_('sign in')) |
| 73 | |
| 74 | |
| 75 | @router.route('/logout', methods=['GET']) |
| 76 | @secured |
| 77 | def logout(): |
| 78 | if not providers.PROVIDER.logout(session): |
| 79 | redirect(url_for('.login')) |
| 80 | logout_user() |
| 81 | return redirect(url_for('.index')) |
| 82 | |
| 83 | |
| 84 | @router.route('/signup', methods=['GET', 'POST']) |
| 85 | @secured |
| 86 | def signup(): |
| 87 | form = SignupForm() |
| 88 | if request.method == 'POST': |
| 89 | if not form.validate(): |
| 90 | return render_template('signup.html', form=form) |
| 91 | else: |
| 92 | newuser = providers.User(firstName=form.firstName.data, password=form.password.data, |
| 93 | lastName=form.lastName.data, |
| 94 | email=form.email.data, username=form.username.data, |
| 95 | institution=form.institution.data, phone=form.phone.data, |
| 96 | address=form.address.data) |
| 97 | |
| 98 | response = APIFactory.post("user/register", |
| 99 | data=json.dumps(newuser, cls=UserEncoder)) |
| 100 | if response is None: |
| 101 | return redirect(url_for('.index')) |
| 102 | elif providers.MESSAGE_HANDLER.isError(response): |
| 103 | providers.MESSAGE_HANDLER.notify(response.json(), flash) |
| 104 | else: |
| 105 | # send email |
| 106 | email = EmailMessage(subject='successful registration', to=['micha.hanl@gmail.com'], |
| 107 | body=response.content) |
| 108 | email.send() |
| 109 | print "the response %s" % str(response.content) |
| 110 | providers.MESSAGE_HANDLER.notify("You successfully registered. Congrats", flash, category='success') |
| 111 | return render_template('signup.html', form=form, submit=_('sign up')) |
| 112 | |
| 113 | elif request.method == 'GET': |
| 114 | return render_template('signup.html', form=form, submit=_('sign up')) |
| 115 | |
| 116 | |
| 117 | @router.route('/reset', methods=['POST', 'GET']) |
| 118 | def reset(): |
| 119 | email = EmailMessage(subject='successful registration', to=['micha.hanl@gmail.com'], |
| 120 | body="This is a body") |
| 121 | print "the messages %s " % str(email.send()) |
| 122 | return redirect(url_for('.index')) |