src/cm/models.py
changeset 12 f69ff46d3240
parent 5 c3594e4df7c1
child 16 036705244cab
equal deleted inserted replaced
11:7b8167c4aa6f 12:f69ff46d3240
   380     text_version = models.ForeignKey(TextVersion)
   380     text_version = models.ForeignKey(TextVersion)
   381 
   381 
   382     objects = AttachmentManager()
   382     objects = AttachmentManager()
   383     
   383     
   384 class NotificationManager(KeyManager):
   384 class NotificationManager(KeyManager):
   385     def create_notification(self, text, type, email_or_user):
   385     def create_notification(self, text, type, active, email_or_user):
   386         prev_notification = self.get_notification_to_own_discussions(text, type, email_or_user)
   386         notification = self.create(text=text, type=type, active=active)
   387         if not prev_notification:
   387         notification.set_email_or_user(email_or_user)
   388             notification = self.create(text=text, type=type)
   388         return notification
   389             notification.set_email_or_user(email_or_user)
   389 
   390             return notification
   390     def get_notifications(self, text, type, email_or_user):
   391         else:
       
   392             return prev_notification 
       
   393 
       
   394     def get_notification_to_own_discussions(self, text, type, email_or_user):
       
   395         if isinstance(email_or_user,unicode):
   391         if isinstance(email_or_user,unicode):
   396             prev_notifications = Notification.objects.filter(text=text, type=type, email=email_or_user)
   392             prev_notifications = Notification.objects.filter(text=text, type=type, email=email_or_user)
   397         else:
   393         else:
   398             prev_notifications = Notification.objects.filter(text=text, type=type, user=email_or_user)
   394             prev_notifications = Notification.objects.filter(text=text, type=type, user=email_or_user)
       
   395             
   399         if prev_notifications:
   396         if prev_notifications:
   400             return prev_notifications[0]
   397             return prev_notifications[0]
   401         else:
   398         else:
   402             return None
   399             return None
   403      
   400      
   404     def set_notification_to_own_discussions(self, text, email_or_user, active=True):
   401     def set_notification(self, text, type, active, email_or_user):
   405         if active:
   402         notification = self.get_notifications(text, type, email_or_user)
   406             notification = self.create_notification(text, 'own', email_or_user)
   403         if notification == None :
   407             if not notification.active:
   404             self.create_notification(text, type, active, email_or_user)
   408                 notification.active = True
   405         else : 
   409                 notification.save()                
   406             notification.active = active
   410         else:
       
   411             notification = self.create_notification(text, 'own', email_or_user)
       
   412             notification.active = False
       
   413             notification.save()                
   407             notification.save()                
   414     
       
   415     def subscribe_to_own_text(self, text, user):
       
   416         return self.create_notification(text, None, user)
       
   417     
   408     
   418 class Notification(KeyModel, AuthorModel):
   409 class Notification(KeyModel, AuthorModel):
   419     text = models.ForeignKey(Text, null=True, blank=True)
   410     text = models.ForeignKey(Text, null=True, blank=True)
   420     type = models.CharField(max_length=30, null=True, blank=True)
   411     type = models.CharField(max_length=30, null=True, blank=True)
   421     active = models.BooleanField(default=True) # active = False means user desactivation
   412     active = models.BooleanField(default=True) # active = False means user desactivation