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 pyramid.httpexceptions import HTTPFound, HTTPNotFound
3 from pyramid.view import view_config, view_defaults
4 from wowf.forms import (
5 AcceptDenyChallengeForm, CreateBenchPressChallengeForm, CreateEnduranceChallengeForm,
6 CreateSpeedChallengeForm, CreateSquatChallengeForm, UploadBenchPressWorkoutForm,
7 UploadEnduranceWorkoutForm, UploadSpeedWorkoutForm, UploadSquatWorkoutForm)
8 from wowf.models import Challenge
9 from wowf.views import BaseView
10
11
12 create_challenge_forms = {'challenge.create.speed': CreateSpeedChallengeForm,
13 'challenge.create.endurance': CreateEnduranceChallengeForm,
14 'challenge.create.bench': CreateBenchPressChallengeForm,
15 'challenge.create.squat': CreateSquatChallengeForm}
16
17
18 @view_defaults(route_name='challenge.view', permission='member')
19 class ChallengeView(BaseView):
20
21 @view_config(renderer='challenge/view.html')
22 def main(self):
23 challenge = Challenge.get_by_id(self.request.matchdict['id'])
24 if not challenge:
25 raise HTTPNotFound()
26 accept_deny_challenge_form = None
27 upload_workout_form = None
28 if self.request.user.in_challenge(challenge):
29 if not (self.request.user.accepted_challenge(challenge) or self.request.user.denied_challenge(challenge)):
30 accept_deny_challenge_form = AcceptDenyChallengeForm(self.request.POST)
31 if 'accept' in self.request.POST:
32 accept_deny_challenge_form.accept_challenge(self.request.user, challenge)
33 return HTTPFound(location=self.request.path)
34 elif 'deny' in self.request.POST:
35 accept_deny_challenge_form.deny_challenge(self.request.user, challenge)
36 return HTTPFound(location=self.request.path)
37 elif self.request.user.accepted_challenge(challenge):
38 if challenge.is_speed_challenge():
39 upload_workout_form = UploadSpeedWorkoutForm(self.request.POST)
40 elif challenge.is_endurance_challenge():
41 upload_workout_form = UploadEnduranceWorkoutForm(self.request.POST)
42 elif challenge.is_bench_press_challenge():
43 upload_workout_form = UploadBenchPressWorkoutForm(self.request.POST)
44 elif challenge.is_squat_challenge():
45 upload_workout_form = UploadSquatWorkoutForm(self.request.POST)
46 if upload_workout_form and 'upload_workout' in self.request.POST and upload_workout_form.validate():
47 upload_workout_form.upload_workout(self.request.user, challenge)
48 return HTTPFound(location=self.request.path)
49 return dict(challenge=challenge,
50 accept_deny_challenge_form=accept_deny_challenge_form,
51 upload_workout_form=upload_workout_form)
52
53
54 @view_defaults(permission='member')
55 class CreateChallengeView(BaseView):
56
57 @view_config(route_name='challenge.create.speed', renderer='challenge/create/speed.html')
58 @view_config(route_name='challenge.create.endurance', renderer='challenge/create/endurance.html')
59 @view_config(route_name='challenge.create.bench', renderer='challenge/create/bench.html')
60 @view_config(route_name='challenge.create.squat', renderer='challenge/create/squat.html')
61 def main(self):
62 form = create_challenge_forms[self.request.matched_route.name](self.request.POST)
63 if 'create_challenge' in self.request.POST and form.validate():
64 challenge = form.create_challenge(self.request.user)
65 self.request.session.flash('%s has been notified of this challenge.' % form.competitor.data, self.consts.SUCCESS)
66 return HTTPFound(location=self.request.route_url('challenge.view', id=challenge.id))
67 return dict(form=form)
68