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
challenge.py
Go to the documentation of this file.
1 from __future__ import unicode_literals
2 from wowf.forms import Form
3 from wowf.lib.validators import Exists
4 from wowf.models import User
5 from wtforms import validators
6 from wtforms.fields import SelectField, SubmitField, TextField
7
8
9 def get_distances(start=100, max=3200):
10 distances = []
11 x = start
12 while x <= max:
13 distances.append((x, '%s meters' % x))
14 x *= 2
15 return distances
16
17
18 def get_durations(increments=15, max=180):
19 return [(x, '%s minutes' % x) for x in range(increments, max + 1, increments)]
20
21
22 def get_percentages(increments=10, max=300):
23 return [(x / 100.0, '%s%% of body weight' % x) for x in range(increments, max + 1, increments)]
24
25
26 class CreateChallengeForm(Form):
27
28 competitor = TextField('Competitor', [
29 validators.Required('Competitor cannot be blank'),
30 Exists(User, User.username, message='Competitor does not exist')],
31 description='type in users username')
32
33
34 class CreateDeviceChallengeForm(CreateChallengeForm):
35 pass
36
37
38 class CreateWeightChallengeForm(CreateChallengeForm):
39
40 percentage = SelectField('Percentage', [
41 validators.Required(message='Must select a percentage')],
42 choices=get_percentages(), coerce=float)
43
44
45 class CreateSpeedChallengeForm(CreateDeviceChallengeForm):
46
47 distance = SelectField('Distance', [
48 validators.Required(message='Must select a distance')],
49 choices=get_distances(), coerce=int)
50
51 def create_challenge(self, user):
52 competitor = User.get_by_username(self.competitor.data)
53 challenge = user.create_speed_challenge(competitor, self.distance.data)
54 return challenge
55
56
57 class CreateEnduranceChallengeForm(CreateDeviceChallengeForm):
58
59 duration = SelectField('Duration', [
60 validators.Required(message='Must select a duration')],
61 choices=get_durations(), coerce=int)
62
63 def create_challenge(self, user):
64 competitor = User.get_by_username(self.competitor.data)
65 challenge = user.create_endurance_challenge(competitor, self.duration.data)
66 return challenge
67
68
69 class CreateBenchPressChallengeForm(CreateWeightChallengeForm):
70
71 def create_challenge(self, user):
72 competitor = User.get_by_username(self.competitor.data)
73 challenge = user.create_bench_press_challenge(competitor, self.percentage.data)
74 return challenge
75
76
77 class CreateSquatChallengeForm(CreateWeightChallengeForm):
78
79 def create_challenge(self, user):
80 competitor = User.get_by_username(self.competitor.data)
81 challenge = user.create_squat_challenge(competitor, self.percentage.data)
82 return challenge
83
84
85 class AcceptDenyChallengeForm(Form):
86
87 accept = SubmitField('Accept Challenge')
88 deny = SubmitField('Deny Challenge')
89
90 def accept_challenge(self, user, challenge):
91 user.accept_challenge(challenge)
92
93 def deny_challenge(self, user, challenge):
94 user.deny_challenge(challenge)
95