[2d91474] | 1 | from calendar import month_name |
---|
| 2 | |
---|
[5ec3a0b] | 3 | from pyramid.httpexceptions import HTTPFound |
---|
| 4 | from pyramid.view import view_config |
---|
| 5 | from pyramid.security import remember, forget |
---|
| 6 | from pyramid.response import Response |
---|
| 7 | from pyramid.i18n import TranslationStringFactory |
---|
| 8 | from pyramid_simpleform import Form, State |
---|
[31adfa5] | 9 | from pytz import common_timezones |
---|
[5ec3a0b] | 10 | |
---|
| 11 | from ..models.user import User |
---|
| 12 | from ..schemas.user import ( |
---|
| 13 | UserProfileSchema, |
---|
| 14 | ChangePasswordSchema, |
---|
| 15 | SignUpSchema, |
---|
| 16 | ) |
---|
| 17 | from ..models.root import OpenWorkouts |
---|
| 18 | from ..views.renderers import OWFormRenderer |
---|
| 19 | |
---|
| 20 | _ = TranslationStringFactory('OpenWorkouts') |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | @view_config(context=OpenWorkouts) |
---|
| 24 | def dashboard_redirect(context, request): |
---|
| 25 | """ |
---|
| 26 | Send the user to his dashboard when accesing the root object, |
---|
| 27 | send to the login page if the user is not logged in. |
---|
| 28 | """ |
---|
| 29 | if request.authenticated_userid: |
---|
[1d92bf2] | 30 | user = request.root.get_user_by_uid(request.authenticated_userid) |
---|
| 31 | if user: |
---|
| 32 | return HTTPFound(location=request.resource_url(user)) |
---|
| 33 | else: |
---|
| 34 | # an authenticated user session, for an user that does not exist |
---|
| 35 | # anymore, logout! |
---|
| 36 | return HTTPFound(location=request.resource_url(context, 'logout')) |
---|
[5ec3a0b] | 37 | return HTTPFound(location=request.resource_url(context, 'login')) |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | @view_config( |
---|
| 41 | context=OpenWorkouts, |
---|
| 42 | name='login', |
---|
| 43 | renderer='ow:templates/login.pt') |
---|
| 44 | def login(context, request): |
---|
| 45 | message = '' |
---|
[1d92bf2] | 46 | email = '' |
---|
[5ec3a0b] | 47 | password = '' |
---|
| 48 | return_to = request.params.get('return_to') |
---|
| 49 | redirect_url = return_to or request.resource_url(request.root) |
---|
| 50 | |
---|
| 51 | if 'submit' in request.POST: |
---|
[1d92bf2] | 52 | email = request.POST.get('email', None) |
---|
| 53 | user = context.get_user_by_email(email) |
---|
| 54 | if user: |
---|
[5ec3a0b] | 55 | password = request.POST.get('password', None) |
---|
| 56 | if password is not None and user.check_password(password): |
---|
[1d92bf2] | 57 | headers = remember(request, str(user.uid)) |
---|
| 58 | redirect_url = return_to or request.resource_url(user) |
---|
[5ec3a0b] | 59 | return HTTPFound(location=redirect_url, headers=headers) |
---|
| 60 | else: |
---|
[1d92bf2] | 61 | message = _('Wrong password') |
---|
[5ec3a0b] | 62 | else: |
---|
[1d92bf2] | 63 | message = _('Wrong email address') |
---|
[5ec3a0b] | 64 | |
---|
| 65 | return { |
---|
| 66 | 'message': message, |
---|
[1d92bf2] | 67 | 'email': email, |
---|
[5ec3a0b] | 68 | 'password': password, |
---|
| 69 | 'redirect_url': redirect_url |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | @view_config(context=OpenWorkouts, name='logout') |
---|
| 74 | def logout(context, request): |
---|
| 75 | headers = forget(request) |
---|
| 76 | return HTTPFound(location=request.resource_url(context), headers=headers) |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | @view_config( |
---|
| 80 | context=OpenWorkouts, |
---|
| 81 | name='signup', |
---|
| 82 | renderer='ow:templates/signup.pt') |
---|
| 83 | def signup(context, request): |
---|
[1d92bf2] | 84 | state = State(emails=context.lowercase_emails, |
---|
| 85 | names=context.lowercase_nicknames) |
---|
[5ec3a0b] | 86 | form = Form(request, schema=SignUpSchema(), state=state) |
---|
| 87 | |
---|
| 88 | if 'submit' in request.POST and form.validate(): |
---|
[1d92bf2] | 89 | user = form.bind(User(), exclude=['password_confirm']) |
---|
| 90 | context.add_user(user) |
---|
[5ec3a0b] | 91 | # Send to login |
---|
| 92 | return HTTPFound(location=request.resource_url(context)) |
---|
| 93 | |
---|
| 94 | return { |
---|
| 95 | 'form': OWFormRenderer(form) |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | @view_config( |
---|
| 100 | context=OpenWorkouts, |
---|
| 101 | name='forgot-password', |
---|
| 102 | renderer='ow:templates/forgot_password.pt') |
---|
[0be1b0d] | 103 | def recover_password(context, request): # pragma: no cover |
---|
[5ec3a0b] | 104 | # WIP |
---|
| 105 | Form(request) |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | @view_config( |
---|
| 109 | context=User, |
---|
| 110 | permission='view', |
---|
| 111 | renderer='ow:templates/dashboard.pt') |
---|
| 112 | def dashboard(context, request): |
---|
| 113 | """ |
---|
| 114 | Render a dashboard for the current user |
---|
| 115 | """ |
---|
[2d91474] | 116 | # Look at the year we are viewing, if none is passed in the request, |
---|
| 117 | # pick up the latest/newer available with activity |
---|
| 118 | viewing_year = request.GET.get('year', None) |
---|
| 119 | if viewing_year is None: |
---|
| 120 | available_years = context.activity_years |
---|
| 121 | if available_years: |
---|
| 122 | viewing_year = available_years[0] |
---|
| 123 | else: |
---|
| 124 | # ensure this is an integer |
---|
| 125 | viewing_year = int(viewing_year) |
---|
| 126 | |
---|
| 127 | # Same for the month, if there is a year set |
---|
| 128 | viewing_month = None |
---|
| 129 | if viewing_year: |
---|
| 130 | viewing_month = request.GET.get('month', None) |
---|
| 131 | if viewing_month is None: |
---|
| 132 | available_months = context.activity_months(viewing_year) |
---|
| 133 | if available_months: |
---|
| 134 | # we pick up the latest month available for the year, |
---|
| 135 | # which means the current month in the current year |
---|
| 136 | viewing_month = available_months[-1] |
---|
| 137 | else: |
---|
| 138 | # ensure this is an integer |
---|
| 139 | viewing_month = int(viewing_month) |
---|
| 140 | |
---|
| 141 | # pick up the workouts to be shown in the dashboard |
---|
| 142 | workouts = context.workouts(viewing_year, viewing_month) |
---|
| 143 | |
---|
| 144 | return { |
---|
| 145 | 'month_name': month_name, |
---|
| 146 | 'viewing_year': viewing_year, |
---|
| 147 | 'viewing_month': viewing_month, |
---|
| 148 | 'workouts': workouts |
---|
| 149 | } |
---|
[5ec3a0b] | 150 | |
---|
| 151 | |
---|
| 152 | @view_config( |
---|
| 153 | context=User, |
---|
| 154 | permission='view', |
---|
| 155 | name='profile', |
---|
| 156 | renderer='ow:templates/profile.pt') |
---|
| 157 | def profile(context, request): |
---|
| 158 | """ |
---|
| 159 | "public" profile view, showing some workouts from this user, her |
---|
| 160 | basic info, stats, etc |
---|
| 161 | """ |
---|
| 162 | return {} |
---|
| 163 | |
---|
| 164 | |
---|
| 165 | @view_config( |
---|
| 166 | context=User, |
---|
| 167 | name='picture', |
---|
| 168 | permission='view') |
---|
| 169 | def profile_picture(context, request): |
---|
| 170 | return Response( |
---|
| 171 | content_type='image', |
---|
| 172 | body_file=context.picture.open()) |
---|
| 173 | |
---|
| 174 | |
---|
| 175 | @view_config( |
---|
| 176 | context=User, |
---|
| 177 | permission='edit', |
---|
| 178 | name='edit', |
---|
| 179 | renderer='ow:templates/edit_profile.pt') |
---|
| 180 | def edit_profile(context, request): |
---|
[6560b8f] | 181 | # if not given a file there is an empty byte in POST, which breaks |
---|
| 182 | # our blob storage validator. |
---|
| 183 | # dirty fix until formencode fixes its api.is_empty method |
---|
| 184 | if isinstance(request.POST.get('picture', None), bytes): |
---|
| 185 | request.POST['picture'] = '' |
---|
| 186 | |
---|
[722ae18] | 187 | nicknames = request.root.lowercase_nicknames |
---|
| 188 | if context.nickname: |
---|
| 189 | # remove the current user nickname from the list, preventing form |
---|
| 190 | # validation error |
---|
| 191 | nicknames.remove(context.nickname.lower()) |
---|
| 192 | state = State(emails=request.root.lowercase_emails, names=nicknames) |
---|
| 193 | form = Form(request, schema=UserProfileSchema(), state=state, obj=context) |
---|
[6560b8f] | 194 | |
---|
[5ec3a0b] | 195 | if 'submit' in request.POST and form.validate(): |
---|
| 196 | # No picture? do not override it |
---|
| 197 | if not form.data['picture']: |
---|
| 198 | del form.data['picture'] |
---|
| 199 | form.bind(context) |
---|
[e02c5b5] | 200 | # reindex |
---|
| 201 | request.root.reindex(context) |
---|
[5ec3a0b] | 202 | # Saved, send the user to the public view of her profile |
---|
| 203 | return HTTPFound(location=request.resource_url(context, 'profile')) |
---|
[31adfa5] | 204 | |
---|
[5ec3a0b] | 205 | # prevent crashes on the form |
---|
| 206 | if 'picture' in form.data: |
---|
| 207 | del form.data['picture'] |
---|
[31adfa5] | 208 | |
---|
| 209 | return {'form': OWFormRenderer(form), |
---|
| 210 | 'timezones': common_timezones} |
---|
[5ec3a0b] | 211 | |
---|
| 212 | |
---|
| 213 | @view_config( |
---|
| 214 | context=User, |
---|
| 215 | permission='edit', |
---|
| 216 | name='passwd', |
---|
| 217 | renderer='ow:templates/change_password.pt') |
---|
| 218 | def change_password(context, request): |
---|
| 219 | form = Form(request, schema=ChangePasswordSchema(), |
---|
| 220 | state=State(user=context)) |
---|
| 221 | if 'submit' in request.POST and form.validate(): |
---|
| 222 | context.password = form.data['password'] |
---|
| 223 | return HTTPFound(location=request.resource_url(context, 'profile')) |
---|
| 224 | return {'form': OWFormRenderer(form)} |
---|