blob: b97d1381ebd2113eaf4ee987b6adaee35b5716f9 [file] [log] [blame]
Akronca9bd982016-12-06 16:59:57 +01001import json
2
3from flask import Blueprint, request, session, render_template, url_for, flash
4from flask_email import EmailMessage
5from flask_login import logout_user, login_user
6from werkzeug.utils import redirect
7from flask_babel import lazy_gettext as _
8
9import APIFactory
10from app import secured
11from models import ProfileForm, UserEncoder, SigninForm, SignupForm
12import providers
13
14
15__author__ = 'hanl'
16
17router = Blueprint('user', __name__,
18 template_folder='templates')
19
20
21@router.route('/', methods=['GET'])
22@secured
23def index():
24 return redirect(url_for('.profile'))
25
26
27@router.route('/profile', methods=['GET', 'POST'])
28@secured
29def 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'])
40def 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'])
50def delete():
51 response = APIFactory.request('delete')
52 pass
53
54
55@router.route('/login', methods=['GET', 'POST'])
56def 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
77def 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
86def 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'])
118def 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'))