[5ec3a0b] | 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: |
---|
[1d92bf2] | 27 | user = request.root.get_user_by_uid(request.authenticated_userid) |
---|
| 28 | if user: |
---|
| 29 | return HTTPFound(location=request.resource_url(user)) |
---|
| 30 | else: |
---|
| 31 | # an authenticated user session, for an user that does not exist |
---|
| 32 | # anymore, logout! |
---|
| 33 | return HTTPFound(location=request.resource_url(context, 'logout')) |
---|
[5ec3a0b] | 34 | return HTTPFound(location=request.resource_url(context, 'login')) |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | @view_config( |
---|
| 38 | context=OpenWorkouts, |
---|
| 39 | name='login', |
---|
| 40 | renderer='ow:templates/login.pt') |
---|
| 41 | def login(context, request): |
---|
| 42 | message = '' |
---|
[1d92bf2] | 43 | email = '' |
---|
[5ec3a0b] | 44 | password = '' |
---|
| 45 | return_to = request.params.get('return_to') |
---|
| 46 | redirect_url = return_to or request.resource_url(request.root) |
---|
| 47 | |
---|
| 48 | if 'submit' in request.POST: |
---|
[1d92bf2] | 49 | email = request.POST.get('email', None) |
---|
| 50 | user = context.get_user_by_email(email) |
---|
| 51 | if user: |
---|
[5ec3a0b] | 52 | password = request.POST.get('password', None) |
---|
| 53 | if password is not None and user.check_password(password): |
---|
[1d92bf2] | 54 | headers = remember(request, str(user.uid)) |
---|
| 55 | redirect_url = return_to or request.resource_url(user) |
---|
[5ec3a0b] | 56 | return HTTPFound(location=redirect_url, headers=headers) |
---|
| 57 | else: |
---|
[1d92bf2] | 58 | message = _('Wrong password') |
---|
[5ec3a0b] | 59 | else: |
---|
[1d92bf2] | 60 | message = _('Wrong email address') |
---|
[5ec3a0b] | 61 | |
---|
| 62 | return { |
---|
| 63 | 'message': message, |
---|
[1d92bf2] | 64 | 'email': email, |
---|
[5ec3a0b] | 65 | 'password': password, |
---|
| 66 | 'redirect_url': redirect_url |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | @view_config(context=OpenWorkouts, name='logout') |
---|
| 71 | def logout(context, request): |
---|
| 72 | headers = forget(request) |
---|
| 73 | return HTTPFound(location=request.resource_url(context), headers=headers) |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | @view_config( |
---|
| 77 | context=OpenWorkouts, |
---|
| 78 | name='signup', |
---|
| 79 | renderer='ow:templates/signup.pt') |
---|
| 80 | def signup(context, request): |
---|
[1d92bf2] | 81 | state = State(emails=context.lowercase_emails, |
---|
| 82 | names=context.lowercase_nicknames) |
---|
[5ec3a0b] | 83 | form = Form(request, schema=SignUpSchema(), state=state) |
---|
| 84 | |
---|
| 85 | if 'submit' in request.POST and form.validate(): |
---|
[1d92bf2] | 86 | user = form.bind(User(), exclude=['password_confirm']) |
---|
| 87 | context.add_user(user) |
---|
[5ec3a0b] | 88 | # Send to login |
---|
| 89 | return HTTPFound(location=request.resource_url(context)) |
---|
| 90 | |
---|
| 91 | return { |
---|
| 92 | 'form': OWFormRenderer(form) |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | @view_config( |
---|
| 97 | context=OpenWorkouts, |
---|
| 98 | name='forgot-password', |
---|
| 99 | renderer='ow:templates/forgot_password.pt') |
---|
| 100 | def recover_password(context, request): |
---|
| 101 | # WIP |
---|
| 102 | Form(request) |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | @view_config( |
---|
| 106 | context=User, |
---|
| 107 | permission='view', |
---|
| 108 | renderer='ow:templates/dashboard.pt') |
---|
| 109 | def dashboard(context, request): |
---|
| 110 | """ |
---|
| 111 | Render a dashboard for the current user |
---|
| 112 | """ |
---|
| 113 | # Add here some logic |
---|
| 114 | return {} |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | @view_config( |
---|
| 118 | context=User, |
---|
| 119 | permission='view', |
---|
| 120 | name='profile', |
---|
| 121 | renderer='ow:templates/profile.pt') |
---|
| 122 | def profile(context, request): |
---|
| 123 | """ |
---|
| 124 | "public" profile view, showing some workouts from this user, her |
---|
| 125 | basic info, stats, etc |
---|
| 126 | """ |
---|
| 127 | return {} |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | @view_config( |
---|
| 131 | context=User, |
---|
| 132 | name='picture', |
---|
| 133 | permission='view') |
---|
| 134 | def profile_picture(context, request): |
---|
| 135 | return Response( |
---|
| 136 | content_type='image', |
---|
| 137 | body_file=context.picture.open()) |
---|
| 138 | |
---|
| 139 | |
---|
| 140 | @view_config( |
---|
| 141 | context=User, |
---|
| 142 | permission='edit', |
---|
| 143 | name='edit', |
---|
| 144 | renderer='ow:templates/edit_profile.pt') |
---|
| 145 | def edit_profile(context, request): |
---|
| 146 | form = Form(request, schema=UserProfileSchema(), obj=context) |
---|
| 147 | if 'submit' in request.POST and form.validate(): |
---|
| 148 | # No picture? do not override it |
---|
| 149 | if not form.data['picture']: |
---|
| 150 | del form.data['picture'] |
---|
| 151 | form.bind(context) |
---|
| 152 | # Saved, send the user to the public view of her profile |
---|
| 153 | return HTTPFound(location=request.resource_url(context, 'profile')) |
---|
| 154 | # prevent crashes on the form |
---|
| 155 | if 'picture' in form.data: |
---|
| 156 | del form.data['picture'] |
---|
| 157 | return {'form': OWFormRenderer(form)} |
---|
| 158 | |
---|
| 159 | |
---|
| 160 | @view_config( |
---|
| 161 | context=User, |
---|
| 162 | permission='edit', |
---|
| 163 | name='passwd', |
---|
| 164 | renderer='ow:templates/change_password.pt') |
---|
| 165 | def change_password(context, request): |
---|
| 166 | form = Form(request, schema=ChangePasswordSchema(), |
---|
| 167 | state=State(user=context)) |
---|
| 168 | if 'submit' in request.POST and form.validate(): |
---|
| 169 | context.password = form.data['password'] |
---|
| 170 | return HTTPFound(location=request.resource_url(context, 'profile')) |
---|
| 171 | return {'form': OWFormRenderer(form)} |
---|