1 | from pyramid.httpexceptions import HTTPFound |
---|
2 | from pyramid.view import view_config |
---|
3 | from pyramid.security import remember, forget |
---|
4 | from pyramid.response import Response |
---|
5 | from pyramid.i18n import TranslationStringFactory |
---|
6 | from pyramid_simpleform import Form, State |
---|
7 | |
---|
8 | from ..models.user import User |
---|
9 | from ..schemas.user import ( |
---|
10 | UserProfileSchema, |
---|
11 | ChangePasswordSchema, |
---|
12 | SignUpSchema, |
---|
13 | ) |
---|
14 | from ..models.root import OpenWorkouts |
---|
15 | from ..views.renderers import OWFormRenderer |
---|
16 | |
---|
17 | _ = TranslationStringFactory('OpenWorkouts') |
---|
18 | |
---|
19 | |
---|
20 | @view_config(context=OpenWorkouts) |
---|
21 | def dashboard_redirect(context, request): |
---|
22 | """ |
---|
23 | Send the user to his dashboard when accesing the root object, |
---|
24 | send to the login page if the user is not logged in. |
---|
25 | """ |
---|
26 | if request.authenticated_userid: |
---|
27 | user = request.root.get_user(request.authenticated_userid) |
---|
28 | return HTTPFound(location=request.resource_url(user)) |
---|
29 | return HTTPFound(location=request.resource_url(context, 'login')) |
---|
30 | |
---|
31 | |
---|
32 | @view_config( |
---|
33 | context=OpenWorkouts, |
---|
34 | name='login', |
---|
35 | renderer='ow:templates/login.pt') |
---|
36 | def login(context, request): |
---|
37 | message = '' |
---|
38 | username = '' |
---|
39 | password = '' |
---|
40 | return_to = request.params.get('return_to') |
---|
41 | redirect_url = return_to or request.resource_url(request.root) |
---|
42 | |
---|
43 | if 'submit' in request.POST: |
---|
44 | username = request.POST.get('username', None) |
---|
45 | if username in request.root.all_usernames(): |
---|
46 | user = request.root[username] |
---|
47 | password = request.POST.get('password', None) |
---|
48 | if password is not None and user.check_password(password): |
---|
49 | headers = remember(request, username) |
---|
50 | return HTTPFound(location=redirect_url, headers=headers) |
---|
51 | else: |
---|
52 | message = u'Bad password' |
---|
53 | else: |
---|
54 | message = u'Bad username' |
---|
55 | |
---|
56 | return { |
---|
57 | 'message': message, |
---|
58 | 'username': username, |
---|
59 | 'password': password, |
---|
60 | 'redirect_url': redirect_url |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | @view_config(context=OpenWorkouts, name='logout') |
---|
65 | def logout(context, request): |
---|
66 | headers = forget(request) |
---|
67 | return HTTPFound(location=request.resource_url(context), headers=headers) |
---|
68 | |
---|
69 | |
---|
70 | @view_config( |
---|
71 | context=OpenWorkouts, |
---|
72 | name='signup', |
---|
73 | renderer='ow:templates/signup.pt') |
---|
74 | def signup(context, request): |
---|
75 | state = State(emails=context.lowercase_emails(), |
---|
76 | names=context.lowercase_usernames()) |
---|
77 | form = Form(request, schema=SignUpSchema(), state=state) |
---|
78 | |
---|
79 | if 'submit' in request.POST and form.validate(): |
---|
80 | username = request.POST['username'] |
---|
81 | user = form.bind(User(), exclude=['username', 'password_confirm']) |
---|
82 | context[username] = user |
---|
83 | # Send to login |
---|
84 | return HTTPFound(location=request.resource_url(context)) |
---|
85 | |
---|
86 | return { |
---|
87 | 'form': OWFormRenderer(form) |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | @view_config( |
---|
92 | context=OpenWorkouts, |
---|
93 | name='forgot-password', |
---|
94 | renderer='ow:templates/forgot_password.pt') |
---|
95 | def recover_password(context, request): |
---|
96 | # WIP |
---|
97 | Form(request) |
---|
98 | |
---|
99 | |
---|
100 | @view_config( |
---|
101 | context=User, |
---|
102 | permission='view', |
---|
103 | renderer='ow:templates/dashboard.pt') |
---|
104 | def dashboard(context, request): |
---|
105 | """ |
---|
106 | Render a dashboard for the current user |
---|
107 | """ |
---|
108 | # Add here some logic |
---|
109 | return {} |
---|
110 | |
---|
111 | |
---|
112 | @view_config( |
---|
113 | context=User, |
---|
114 | permission='view', |
---|
115 | name='profile', |
---|
116 | renderer='ow:templates/profile.pt') |
---|
117 | def profile(context, request): |
---|
118 | """ |
---|
119 | "public" profile view, showing some workouts from this user, her |
---|
120 | basic info, stats, etc |
---|
121 | """ |
---|
122 | return {} |
---|
123 | |
---|
124 | |
---|
125 | @view_config( |
---|
126 | context=User, |
---|
127 | name='picture', |
---|
128 | permission='view') |
---|
129 | def profile_picture(context, request): |
---|
130 | return Response( |
---|
131 | content_type='image', |
---|
132 | body_file=context.picture.open()) |
---|
133 | |
---|
134 | |
---|
135 | @view_config( |
---|
136 | context=User, |
---|
137 | permission='edit', |
---|
138 | name='edit', |
---|
139 | renderer='ow:templates/edit_profile.pt') |
---|
140 | def edit_profile(context, request): |
---|
141 | form = Form(request, schema=UserProfileSchema(), obj=context) |
---|
142 | if 'submit' in request.POST and form.validate(): |
---|
143 | # No picture? do not override it |
---|
144 | if not form.data['picture']: |
---|
145 | del form.data['picture'] |
---|
146 | form.bind(context) |
---|
147 | # Saved, send the user to the public view of her profile |
---|
148 | return HTTPFound(location=request.resource_url(context, 'profile')) |
---|
149 | # prevent crashes on the form |
---|
150 | if 'picture' in form.data: |
---|
151 | del form.data['picture'] |
---|
152 | return {'form': OWFormRenderer(form)} |
---|
153 | |
---|
154 | |
---|
155 | @view_config( |
---|
156 | context=User, |
---|
157 | permission='edit', |
---|
158 | name='passwd', |
---|
159 | renderer='ow:templates/change_password.pt') |
---|
160 | def change_password(context, request): |
---|
161 | form = Form(request, schema=ChangePasswordSchema(), |
---|
162 | state=State(user=context)) |
---|
163 | if 'submit' in request.POST and form.validate(): |
---|
164 | context.password = form.data['password'] |
---|
165 | return HTTPFound(location=request.resource_url(context, 'profile')) |
---|
166 | return {'form': OWFormRenderer(form)} |
---|