| import json |
| |
| from flask import Blueprint, request, session, render_template, url_for, flash |
| from flask_email import EmailMessage |
| from flask_login import logout_user, login_user |
| from werkzeug.utils import redirect |
| from flask_babel import lazy_gettext as _ |
| |
| import APIFactory |
| from app import secured |
| from models import ProfileForm, UserEncoder, SigninForm, SignupForm |
| import providers |
| |
| |
| __author__ = 'hanl' |
| |
| router = Blueprint('user', __name__, |
| template_folder='templates') |
| |
| |
| @router.route('/', methods=['GET']) |
| @secured |
| def index(): |
| return redirect(url_for('.profile')) |
| |
| |
| @router.route('/profile', methods=['GET', 'POST']) |
| @secured |
| def profile(): |
| user = providers.PROVIDER.get_user(session, True) |
| form = ProfileForm(obj=user) |
| if request.method == 'POST': |
| pass |
| elif request.method == 'GET': |
| return render_template('profile.html', form=form, submit=_('Update'), |
| complete=user.has_details()) |
| |
| |
| @router.route('/profile/<user>', methods=['GET']) |
| def user_profile(user=None): |
| user = providers.User(username=user) |
| form = ProfileForm(obj=user) |
| if request.method == 'POST': |
| pass |
| elif request.method == 'GET': |
| return render_template('profile.html', form=form, submit=_('Update')) |
| |
| |
| # @router.route('/delete', methods=['POST', 'GET']) |
| def delete(): |
| response = APIFactory.request('delete') |
| pass |
| |
| |
| @router.route('/login', methods=['GET', 'POST']) |
| def login(): |
| form = SigninForm() |
| if request.method == 'POST': |
| if not form.validate(): |
| return render_template('login.html', form=form) |
| else: |
| user = providers.User(username=form.username.data, password=form.password.data) |
| success = providers.PROVIDER.login(session, user) |
| if success: |
| login_user(user) |
| else: |
| return redirect(url_for('.login')) |
| print "the data serialized %s" % json.dumps(user, cls=UserEncoder) |
| return redirect(request.args.get('next') or url_for('.profile')) |
| elif request.method == 'GET': |
| providers.PROVIDER.is_admin("") |
| return render_template('login.html', form=form, submit=_('sign in')) |
| |
| |
| @router.route('/logout', methods=['GET']) |
| @secured |
| def logout(): |
| if not providers.PROVIDER.logout(session): |
| redirect(url_for('.login')) |
| logout_user() |
| return redirect(url_for('.index')) |
| |
| |
| @router.route('/signup', methods=['GET', 'POST']) |
| @secured |
| def signup(): |
| form = SignupForm() |
| if request.method == 'POST': |
| if not form.validate(): |
| return render_template('signup.html', form=form) |
| else: |
| newuser = providers.User(firstName=form.firstName.data, password=form.password.data, |
| lastName=form.lastName.data, |
| email=form.email.data, username=form.username.data, |
| institution=form.institution.data, phone=form.phone.data, |
| address=form.address.data) |
| |
| response = APIFactory.post("user/register", |
| data=json.dumps(newuser, cls=UserEncoder)) |
| if response is None: |
| return redirect(url_for('.index')) |
| elif providers.MESSAGE_HANDLER.isError(response): |
| providers.MESSAGE_HANDLER.notify(response.json(), flash) |
| else: |
| # send email |
| email = EmailMessage(subject='successful registration', to=['micha.hanl@gmail.com'], |
| body=response.content) |
| email.send() |
| print "the response %s" % str(response.content) |
| providers.MESSAGE_HANDLER.notify("You successfully registered. Congrats", flash, category='success') |
| return render_template('signup.html', form=form, submit=_('sign up')) |
| |
| elif request.method == 'GET': |
| return render_template('signup.html', form=form, submit=_('sign up')) |
| |
| |
| @router.route('/reset', methods=['POST', 'GET']) |
| def reset(): |
| email = EmailMessage(subject='successful registration', to=['micha.hanl@gmail.com'], |
| body="This is a body") |
| print "the messages %s " % str(email.send()) |
| return redirect(url_for('.index')) |