@@ -19,15 +19,15 @@ class Notification(Model):
1919 type = Property () # favorite, save, subscriber
2020 deleted = Property (default = 0 )
2121 created_at = Property ()
22-
22+
2323 def save (self , * args , ** kwargs ):
2424 if options .readonly :
2525 self .add_error ('_' , 'Site is read-only.' )
2626 return False
2727
2828 self ._set_dates ()
2929 return super (Notification , self ).save (* args , ** kwargs )
30-
30+
3131 def _set_dates (self ):
3232 """
3333 Sets the created_at and updated_at fields. This should be something
@@ -39,13 +39,13 @@ def _set_dates(self):
3939 def delete (self ):
4040 self .deleted = 1
4141 self .save ()
42-
42+
4343 def sender (self ):
4444 return user .User .get ("id=%s" , self .sender_id )
45-
45+
4646 def receiver (self ):
4747 return user .User .get ("id=%s" , self .receiver_id )
48-
48+
4949 def related_object (self ):
5050 """
5151 Return the object this notification relates to. In the case of a favorite
@@ -63,7 +63,7 @@ def related_object(self):
6363 return subscription_ and subscription_ .shake ()
6464 else :
6565 return user .User .get ("id = %s and deleted=0" , self .sender_id )
66-
66+
6767 @classmethod
6868 def invitation_to_shake_for_user (self , shake , user ):
6969 """
@@ -117,51 +117,63 @@ def display_for_user(cls, user):
117117 'invitation' :[],
118118 'invitations' : [], # TODO: kill this ambiguity - IK
119119 'invitation_request' : [],
120- 'invitation_approved' : []
120+ 'invitation_approved' : [],
121+ 'total' : 0 ,
121122 }
123+ total = 0
122124 for notification in cls .for_user (user ):
123125 sender = notification .sender ()
124126 related_object = notification .related_object ()
125127 if not related_object :
126128 continue
127129
128130 _notification = {'sender' : sender , 'related_object' : related_object , 'id' : notification .id }
129-
131+
130132 if notification .type == 'favorite' :
131133 if related_object .id not in notifications ['like' ]['items' ]:
132134 notifications ['like' ]['items' ][related_object .id ] = []
133135 notifications ['like' ]['items' ][related_object .id ].append (_notification )
134136 notifications ['like' ]['count' ] += 1
135-
137+ total += 1
138+
136139 elif notification .type == 'subscriber' :
137140 _notification ['post_name_text' ] = " is now following " + related_object .display_name (user )
138141 notifications ['follow' ].append (_notification )
139-
142+ total += 1
143+
140144 elif notification .type == 'save' :
141145 if related_object .id not in notifications ['save' ]['items' ]:
142146 notifications ['save' ]['items' ][related_object .id ] = []
143147 notifications ['save' ]['items' ][related_object .id ].append (_notification )
144148 notifications ['save' ]['count' ] += 1
149+ total += 1
145150
146151 elif notification .type == 'comment' :
147152 notifications ['comment' ].append (_notification )
153+ total += 1
148154
149155 elif notification .type == 'mention' :
150156 notifications ['mention' ].append (_notification )
157+ total += 1
151158
152159 elif notification .type == 'invitation' :
153160 notifications ['invitation' ].append (_notification )
154-
161+ total += 1
162+
155163 elif notification .type == 'invitation_request' :
156164 notifications ['invitation_request' ].append (_notification )
157-
165+ total += 1
166+
158167 elif notification .type == 'invitation_approved' :
159168 notifications ['invitation_approved' ].append (_notification )
160-
169+ total += 1
170+
171+ notifications ['total' ] = total
172+
161173 #for invitation_ in invitation.Invitation.by_user(user):
162174 # notifications['invitations'].append(invitation_.email_address)
163175 return notifications
164-
176+
165177 @classmethod
166178 def for_user (cls , user , deleted = False ):
167179 """
@@ -192,7 +204,7 @@ def new_favorite(sender, sharedfile):
192204 n = Notification (sender_id = sender .id , receiver_id = sharedfile .user_id , action_id = sharedfile .id , type = 'favorite' )
193205 n .save ()
194206 return n
195-
207+
196208 @staticmethod
197209 def new_save (sender , sharedfile ):
198210 """
@@ -215,7 +227,7 @@ def new_comment(comment):
215227 n = Notification (sender_id = comment .user_id , receiver_id = sf .user_id , action_id = comment .id , type = 'comment' )
216228 n .save ()
217229 return n
218-
230+
219231 @staticmethod
220232 def new_comment_like (comment , sender ):
221233 """
@@ -237,7 +249,7 @@ def new_mention(receiver, comment):
237249 n = Notification (sender_id = comment .user_id , receiver_id = receiver .id , action_id = comment .id , type = 'mention' )
238250 n .save ()
239251 return n
240-
252+
241253 @staticmethod
242254 def new_invitation_request_accepted (sender , receiver , shake ):
243255 """
@@ -256,7 +268,7 @@ def new_invitation_to_shake(sender, receiver, action_id):
256268 action_id - the shake_id
257269 """
258270 the_shake = shake .Shake .get ('id=%s' , action_id )
259-
271+
260272 n = Notification (sender_id = sender .id , receiver_id = receiver .id , action_id = action_id , type = 'invitation_request' )
261273 text_message = """Hi, %s.
262274%s has requested to join "%s". This means they will be able to put files into the "%s" shake.
@@ -325,7 +337,7 @@ def new_invitation(sender, receiver, action_id):
325337""" % (receiver .name , options .app_host , sender .name , sender .display_name (), options .app_host ,
326338 new_shake .name , new_shake .display_name (), options .app_host , new_shake .name ,
327339 new_shake .display_name (), options .app_host , new_shake .name , options .app_host , new_shake .name )
328-
340+
329341 n .save ()
330342
331343 if not receiver .disable_notifications and not options .debug :
@@ -336,7 +348,7 @@ def new_invitation(sender, receiver, action_id):
336348 html_body = html_message )
337349 pm .send ()
338350 return n
339-
351+
340352 @staticmethod
341353 def new_subscriber (sender , receiver , action_id ):
342354 """
@@ -349,7 +361,7 @@ def new_subscriber(sender, receiver, action_id):
349361 subscription_line = ""
350362 if target_shake .type == 'group' :
351363 subscription_line = " called '%s'" % (target_shake .name )
352-
364+
353365 n = Notification (sender_id = sender .id , receiver_id = receiver .id , action_id = action_id , type = 'subscriber' )
354366
355367 text_message = """Hi, %s.
@@ -381,7 +393,7 @@ def new_subscriber(sender, receiver, action_id):
381393 options .app_host )
382394
383395 n .save ()
384-
396+
385397 if not receiver .disable_notifications and not options .debug :
386398 pm = postmark .PMMail (api_key = options .postmark_api_key ,
387399 sender = "hello@mltshp.com" , to = receiver .email ,
@@ -390,7 +402,7 @@ def new_subscriber(sender, receiver, action_id):
390402 html_body = html_message )
391403 pm .send ()
392404 return n
393-
405+
394406 @staticmethod
395407 def send_shake_member_removal (former_shake , former_member ):
396408 """
0 commit comments