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
notification.py
Go to the documentation of this file.
1 from __future__ import unicode_literals
2 from pyramid.threadlocal import get_current_request
3 from sqlalchemy.ext.declarative import declared_attr
4 from sqlalchemy.orm import backref, relationship
5 from sqlalchemy.schema import Column, ForeignKey
6 from sqlalchemy.types import DateTime, Integer
7 from webhelpers.html.tags import link_to
8 from wowf.lib.utils import current_timestamp
9 from wowf.models.lookup_tables import NotificationType
10 from wowf.models.meta import Base
11
12
13 class Notification(Base):
14
15 __tablename__ = 'notifications'
16 __mapper_args__ = {'polymorphic_on': 'notification_type_id'}
17 id = Column(Integer(unsigned=True), primary_key=True)
18 notification_type_id = Column(
19 Integer(unsigned=True), ForeignKey('notification_types.id', ondelete='cascade'),
20 nullable=False)
21 created_at = Column(DateTime, nullable=False, default=current_timestamp)
22
23 users = relationship(
24 'User', backref=backref('notifications', lazy='dynamic'),
25 secondary='users_notifications')
26
27 def __unicode__(self):
28 return self.message
29
30 def add_recipients(self, users):
31 for user in users:
32 if user not in self.users:
33 self.users.append(user)
34
35
36 class _Notification(object):
37
38 @declared_attr
39 def id(cls):
40 return Column(
41 Integer(unsigned=True), ForeignKey('notifications.id', ondelete='cascade'),
42 primary_key=True)
43
44
45 class RequestedChallengeNotification(_Notification, Notification):
46
47 __tablename__ = 'requested_challenge_notifications'
48 __mapper_args__ = {'polymorphic_identity': NotificationType.lookup_data.index('requested_challenge') + 1}
49 user_id = Column(Integer(unsigned=True), ForeignKey('users.id', ondelete='cascade'), nullable=False)
50 challenge_id = Column(Integer(unsigned=True), ForeignKey('challenges.id', ondelete='cascade'), nullable=False)
51
52 user = relationship('User', lazy='joined')
53 challenge = relationship('Challenge', lazy='joined')
54
55 @property
56 def message(self):
57 request = get_current_request()
58 user = link_to(self.user,
59 url=request.route_url('user.view.challenges', id=self.user.id))
60 challenge = link_to(self.challenge,
61 url=request.route_url('challenge.view', id=self.challenge.id))
62 return '%(user)s has challenged you to a %(challenge)s.' % dict(user=user, challenge=challenge)
63
64
65 class AcceptedChallengeNotification(_Notification, Notification):
66
67 __tablename__ = 'accepted_challenge_notifications'
68 __mapper_args__ = {'polymorphic_identity': NotificationType.lookup_data.index('accepted_challenge') + 1}
69 user_id = Column(Integer(unsigned=True), ForeignKey('users.id', ondelete='cascade'), nullable=False)
70 challenge_id = Column(Integer(unsigned=True), ForeignKey('challenges.id', ondelete='cascade'), nullable=False)
71
72 user = relationship('User', lazy='joined')
73 challenge = relationship('Challenge', lazy='joined')
74
75 @property
76 def message(self):
77 request = get_current_request()
78 user = link_to(self.user,
79 url=request.route_url('user.view.challenges', id=self.user.id))
80 challenge = link_to(self.challenge,
81 url=request.route_url('challenge.view', id=self.challenge.id))
82 return '%(user)s has accepted your %(challenge)s.' % dict(user=user, challenge=challenge)
83
84
85 class DeniedChallengeNotification(_Notification, Notification):
86
87 __tablename__ = 'denied_challenge_notifications'
88 __mapper_args__ = {'polymorphic_identity': NotificationType.lookup_data.index('denied_challenge') + 1}
89 user_id = Column(Integer(unsigned=True), ForeignKey('users.id', ondelete='cascade'), nullable=False)
90 challenge_id = Column(Integer(unsigned=True), ForeignKey('challenges.id', ondelete='cascade'), nullable=False)
91
92 user = relationship('User', lazy='joined')
93 challenge = relationship('Challenge', lazy='joined')
94
95 @property
96 def message(self):
97 request = get_current_request()
98 user = link_to(self.user,
99 url=request.route_url('user.view.challenges', id=self.user.id))
100 challenge = link_to(self.challenge,
101 url=request.route_url('challenge.view', id=self.challenge.id))
102 return '%(user)s has denied your %(challenge)s.' % dict(user=user, challenge=challenge)
103
104 class NewBuddyNotification(_Notification, Notification):
105
106 __tablename__ = 'new_buddy_notifications'
107 __mapper_args__ = {'polymorphic_identity': NotificationType.lookup_data.index('new_buddy') + 1}
108 user_id = Column(Integer(unsigned=True), ForeignKey('users.id', ondelete='cascade'), nullable=False)
109
110 user = relationship('User', lazy='joined')
111
112 @property
113 def message(self):
114 request = get_current_request()
115 user = link_to(self.user,
116 url=request.route_url('user.view.challenges', id=self.user.id))
117 return '%(user)s has added you as a buddy.' % dict(user=user)
118
119
120 class UploadedWorkoutNotification(_Notification, Notification):
121
122 __tablename__ = 'uploaded_workout_notifications'
123 __mapper_args__ = {'polymorphic_identity': NotificationType.lookup_data.index('uploaded_workout') + 1}
124 user_id = Column(Integer(unsigned=True), ForeignKey('users.id', ondelete='cascade'), nullable=False)
125 challenge_id = Column(Integer(unsigned=True), ForeignKey('challenges.id', ondelete='cascade'), nullable=False)
126
127 user = relationship('User', lazy='joined')
128 challenge = relationship('Challenge', lazy='joined')
129
130 @property
131 def message(self):
132 request = get_current_request()
133 user = link_to(self.user,
134 url=request.route_url('user.view.challenges', id=self.user.id))
135 challenge = link_to(self.challenge,
136 url=request.route_url('challenge.view', id=self.challenge.id))
137 return '%(user)s has completed the %(challenge)s.' % dict(user=user, challenge=challenge)
138