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 pyramid.httpexceptions import HTTPFound, HTTPNotFound
2 from pyramid.view import view_config, view_defaults
3 from wowf.forms import (
4 AddBuddyForm, ConfirmNotificationsForm, LoginForm, RegistrationForm,
5 RemoveBuddyForm, SendInviteForm, AddRemoveBuddyForm)
6 from wowf.lib.auth import Auth
7 from wowf.models import User
8 from wowf.views import (
9 BaseView, get_all_notification_stream, get_buddies_stream,
10 get_challenge_stream, get_unconfirmed_notification_stream)
11
12
13 notification_view_streams = {'user.notifications.unconfirmed': get_unconfirmed_notification_stream,
14 'user.notifications.all': get_all_notification_stream}
15
16 profile_view_streams = {'user.view.challenges': get_challenge_stream,
17 'user.view.buddies': get_buddies_stream}
18
19
20 @view_defaults(permission='member')
21 class NotificationView(BaseView):
22
23 @view_config(route_name='user.notifications.unconfirmed', renderer='user/notifications/unconfirmed.html')
24 @view_config(route_name='user.notifications.all', renderer='user/notifications/all.html')
25 def main(self):
26 form = ConfirmNotificationsForm(self.request.POST)
27 if 'confirm_notifications' in self.request.POST and form.validate():
28 form.confirm_notifications(self.request.user)
29 return HTTPFound(location=self.request.route_url('user.notifications.unconfirmed'))
30 stream = notification_view_streams[self.request.matched_route.name](self.request.user)
31 return dict(form=form,
32 stream=stream)
33
34
35 @view_defaults(permission='member')
36 class ProfileView(BaseView):
37
38 @view_config(route_name='user.view.challenges', renderer='user/view/challenges.html')
39 @view_config(route_name='user.view.buddies', renderer='user/view/buddies.html')
40 def main(self):
41 user = User.get_by_id(self.request.matchdict['id'])
42 if not user:
43 raise HTTPNotFound()
44 add_remove_buddy_form = None
45 if not self.request.user.is_user(user):
46 add_remove_buddy_form = AddRemoveBuddyForm(self.request.POST)
47 if 'add' in self.request.POST:
48 add_remove_buddy_form.add_buddy(self.request.user, user)
49 return HTTPFound(location=self.request.path)
50 elif 'remove' in self.request.POST:
51 add_remove_buddy_form.remove_buddy(self.request.user, user)
52 return HTTPFound(location=self.request.path)
53 stream = profile_view_streams[self.request.matched_route.name](user)
54 return dict(user=user,
55 add_remove_buddy_form=add_remove_buddy_form,
56 stream=stream)
57
58
59 @view_defaults(route_name='user.logout')
60 class LogoutView(BaseView):
61
62 @view_config()
63 def main(self):
64 headers = Auth.logout()
65 return HTTPFound(location=self.request.route_url('user.login'), headers=headers)
66
67
68 @view_defaults(route_name='user.login', permission='guest')
69 class LoginView(BaseView):
70
71 @view_config(renderer='user/login.html')
72 def main(self):
73 form = LoginForm(self.request.POST)
74 if 'login' in self.request.POST and form.validate():
75 headers = form.login_user()
76 location = self.request.GET.get(
77 'return_to', self.request.route_url('home'))
78 return HTTPFound(location=location, headers=headers)
79 return dict(form=form)
80
81
82 @view_defaults(route_name='user.register', permission='guest')
83 class RegisterView(BaseView):
84
85 @view_config(renderer='user/register.html')
86 def main(self):
87 form = RegistrationForm(self.request.POST)
88 if 'registration' in self.request.POST and form.validate():
89 headers = form.register_user()
90 self.request.session.flash("Welcome to Workout With Friends!", self.consts.SUCCESS)
91 return HTTPFound(location=self.request.route_url('home'), headers=headers)
92 return dict(form=form)
93
94
95 @view_defaults(route_name='user.invite', permission='member')
96 class SendInviteView(BaseView):
97
98 @view_config(renderer='user/invite.html')
99 def main(self):
100 form = SendInviteForm(self.request.POST)
101 if 'send_invite' in self.request.POST and form.validate():
102 form.send_invite_email(self.request.user)
103 self.request.session.flash('An invitation has been sent out on your behalf.', self.consts.SUCCESS)
104 return HTTPFound(location=self.request.path)
105 return dict(form=form)
106