Workout With Friends Stay fit with a little motivation

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Properties
user.py
Go to the documentation of this file.
1 from __future__ import unicode_literals
2 from cgi import FieldStorage
3 from timezones import zones
4 from wowf.forms import Form
5 from wowf.lib.auth import Auth
6 from wowf.lib.fields import EmailField
7 from wowf.lib.utils import years_ago
8 from wowf.lib.validators import FileType, Unique, ValidLogin
9 from wowf.models import User
10 from wtforms import validators
11 from wtforms.fields import (
12 BooleanField, DateField, DecimalField, FieldList, FileField,
13 PasswordField, SelectField, SubmitField, TextField)
14
15
16 MIN_AGE = 13
17
18
19 def get_genders():
20 return [('', 'Select Gender'), ('F', 'Female'), ('M', 'Male')]
21
22
23 def get_timezones():
24 timezones = [('', 'Select Timezone')]
25 for _, name, formatted_name in zones.get_timezones(only_us=True):
26 timezones.append([name, formatted_name])
27 return timezones
28
29
30 class UserForm(Form):
31
32 username = TextField('Username', [
33 validators.Required(message='Username cannot be blank'),
34 validators.Length(min=5, max=10, message='Limit username to between %(min)d and %(max)d characters'),
35 Unique(User, User.username, message='Username is already in use')])
36 email = EmailField('Email', [
37 validators.Required(message='Email cannot be blank'),
38 validators.Email(message='Valid email address is required'),
39 Unique(User, User.email, message='Email is already in use')])
40 gender = SelectField('Gender', [
41 validators.Required(message='Must select a gender')],
42 choices=get_genders())
43 dob = DateField('Date of Birth', [
44 validators.Required('Date of Birth cannot be blank')],
45 format='%m/%d/%y', description='mm/dd/yy')
46 weight = DecimalField('Weight (kg)', [
47 validators.Required(message='Weight must be a positive number'),
48 validators.NumberRange(min=0, message='Weight must be at least %(min)d kilograms')])
49 height = DecimalField('Height (m)', [
50 validators.Required(message='Height must be a positive number'),
51 validators.NumberRange(min=0, message='Height must be at least %(min)d meters')])
52
53 def validate_dob(self, field):
54 if years_ago(field.data) < MIN_AGE:
55 raise validators.ValidationError('You must be %d or older to join' % MIN_AGE)
56
57
58 class RegistrationForm(UserForm):
59
60 password = PasswordField('Password', [
61 validators.Required('Password cannot be blank'),
62 validators.Length(min=6, max=15, message='Password must be between %(min)d and %(max)d characters')])
63
64 ##
65 #
66 # @return Login response headers
67 #
68 def register_user(self):
69 user = User.create(
70 self.username.data, self.email.data, self.password.data, self.gender.data,
71 self.dob.data, self.weight.data, self.height.data)
72 return Auth.register(user, login=True)
73
74
75 class UpdateProfileForm(UserForm):
76
77 timezone = SelectField('Timezone', choices=get_timezones())
78 avatar = FileField('Profile image', [
79 FileType(['gif', 'jpg', 'jpeg', 'png'], message='Profile image must be a %(allowed)s')])
80
81 def update_profile(self, user):
82 user.update_profile(
83 self.username.data, self.email.data, self.gender.data, self.dob.data,
84 self.weight.data, self.height.data, self.timezone.data)
85 if isinstance(self.avatar.data, FieldStorage):
86 user.update_avatar(self.avatar.data)
87
88
89 class LoginForm(Form):
90
91 email = EmailField('Email', [
92 validators.Required('Email cannot be blank'),
93 ValidLogin(message='Invalid email/password combo')])
94 password = PasswordField('Password', [
95 validators.Required('Password cannot be blank')])
96 remember_me = BooleanField('Remember me', default=True)
97
98 ##
99 #
100 # @return Login response headers
101 #
102 def login_user(self):
103 user = User.get_by_email(self.email.data)
104 return Auth.login(user, self.remember_me.data)
105
106
107 class ChangePasswordForm(Form):
108
109 current_password = PasswordField('Current password', [
110 validators.Required(message='Current password cannot be blank')])
111 new_password = PasswordField('New password', [
112 validators.Required(message='New password cannot be blank'),
113 validators.Length(min=6, max=15, message='New password must be between %(min)d and %(max)d characters')])
114 confirm_new_password = PasswordField('Verify password', [
115 validators.EqualTo('new_password', message='Passwords must match')])
116
117 ##
118 #
119 # User must know current password.
120 #
121 def validate_current_password(self, field):
122 if self.state.password != Auth.hash_password(field.data, self.state.password):
123 raise validators.ValidationError('Invalid current password')
124
125 ##
126 #
127 # New password must be different from current password.
128 #
129 def validate_new_password(self, field):
130 if field.data == self.current_password.data:
131 raise validators.ValidationError('New password must be different from current password')
132
133 def update_password(self, user):
134 user.update_password(self.new_password.data)
135
136
137 class AddBuddyForm(Form):
138
139 change_status = SubmitField('Add Buddy')
140
141 def add_buddy(self, user, other_user):
142 user.add_buddy(other_user)
143
144
145 class RemoveBuddyForm(Form):
146
147 change_status = SubmitField('Remove Buddy')
148
149 def remove_buddy(self, user, other_user):
150 user.remove_buddy(other_user)
151
152
153 class AddRemoveBuddyForm(Form):
154
155 add = SubmitField('Add Buddy')
156 remove = SubmitField('Remove Buddy')
157
158 def add_buddy(self, user, other_user):
159 user.add_buddy(other_user)
160
161 def remove_buddy(self, user, other_user):
162 user.remove_buddy(other_user)
163
164
165 class ResetPasswordForm(Form):
166
167 email = EmailField('Account email', [
168 validators.Required(message='Email cannot be blank')])
169
170 ##
171 #
172 # Email must exist.
173 #
174 def validate_email(self, field):
175 if not User.get_by_email(field.data):
176 raise validators.ValidationError('Email could not be found')
177
178 ##
179 #
180 # Send instructions to email on how to reset password.
181 #
182 def request_reset_password(self):
183 Auth.request_reset_password(self.email.data)
184
185
186 class ConfirmNotificationsForm(Form):
187
188 def confirm_notifications(self, user):
189 user.confirm_all_notifications()
190
191
192 class SendInviteForm(Form):
193
194 emails = FieldList(
195 EmailField('Email', [
196 validators.Email(message='Valid email address is required')]),
197 min_entries=1, max_entries=10)
198
199 ##
200 #
201 # Send the invite emails, from the user.
202 #
203 def send_invite_email(self, user):
204 Auth.send_invite(self.emails.data, user)
205