diff rhodecode/model/db.py @ 1712:cac5109ac3b6 beta

Notification system improvements - deleting - tests - ui - moved to separate controller
author Marcin Kuzminski <marcin@python-works.com>
date Wed, 23 Nov 2011 00:55:05 +0200
parents f23828b00b21
children 54687aa00724
line wrap: on
line diff
--- a/rhodecode/model/db.py	Tue Nov 22 14:10:33 2011 +0200
+++ b/rhodecode/model/db.py	Wed Nov 23 00:55:05 2011 +0200
@@ -49,7 +49,6 @@
 from rhodecode.model.meta import Base, Session
 
 
-
 log = logging.getLogger(__name__)
 
 #==============================================================================
@@ -286,7 +285,9 @@
 
     group_member = relationship('UsersGroupMember', cascade='all')
 
-    notifications = relationship('Notification', secondary='user_to_notification')
+    notifications = relationship('Notification',
+                            secondary='user_to_notification',
+                            order_by=lambda :Notification.created_on.desc())
 
     @property
     def full_contact(self):
@@ -301,11 +302,9 @@
         return self.admin
 
     def __repr__(self):
-        try:
-            return "<%s('id:%s:%s')>" % (self.__class__.__name__,
-                                             self.user_id, self.username)
-        except:
-            return self.__class__.__name__
+        return "<%s('id:%s:%s')>" % (self.__class__.__name__,
+                                     self.user_id, self.username)
+
 
     @classmethod
     def get_by_username(cls, username, case_insensitive=False, cache=False):
@@ -336,6 +335,7 @@
         Session.commit()
         log.debug('updated user %s lastlogin', self.username)
 
+
 class UserLog(Base, BaseModel):
     __tablename__ = 'user_logs'
     __table_args__ = {'extend_existing':True}
@@ -1131,9 +1131,9 @@
     __tablename__ = 'notifications'
     __table_args__ = ({'extend_existing':True})
 
-    TYPE_CHANGESET_COMMENT = 'cs_comment'
-    TYPE_MESSAGE = 'message'
-    TYPE_MENTION = 'mention'
+    TYPE_CHANGESET_COMMENT = u'cs_comment'
+    TYPE_MESSAGE = u'message'
+    TYPE_MENTION = u'mention'
 
     notification_id = Column('notification_id', Integer(), nullable=False, primary_key=True)
     subject = Column('subject', Unicode(512), nullable=True)
@@ -1142,9 +1142,10 @@
     created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
     type_ = Column('type', Unicode(256))
 
-    create_by_user = relationship('User')
-    user_notifications = relationship('UserNotification',
-        primaryjoin = 'Notification.notification_id==UserNotification.notification_id',
+    created_by_user = relationship('User')
+    notifications_to_users = relationship('UserNotification',
+        primaryjoin='Notification.notification_id==UserNotification.notification_id',
+        lazy='joined',
         cascade = "all, delete, delete-orphan")
 
     @property
@@ -1158,16 +1159,20 @@
             type_ = Notification.TYPE_MESSAGE
 
         notification = cls()
-        notification.create_by_user = created_by
+        notification.created_by_user = created_by
         notification.subject = subject
         notification.body = body
         notification.type_ = type_
         Session.add(notification)
         for u in recipients:
             u.notifications.append(notification)
-        Session.commit()
         return notification
 
+    @property
+    def description(self):
+        from rhodecode.model.notification import NotificationModel
+        return NotificationModel().make_description(self)
+
 class UserNotification(Base, BaseModel):
     __tablename__ = 'user_to_notification'
     __table_args__ = (UniqueConstraint('user_id', 'notification_id'),
@@ -1179,9 +1184,12 @@
     sent_on = Column('sent_on', DateTime(timezone=False), nullable=True, unique=None)
 
     user = relationship('User', single_parent=True, lazy="joined")
-    notification = relationship('Notification',single_parent=True,
-                                cascade="all, delete, delete-orphan")
+    notification = relationship('Notification', single_parent=True,)
 
+    def mark_as_read(self):
+        self.read = True
+        Session.add(self)
+        Session.commit()
 
 class DbMigrateVersion(Base, BaseModel):
     __tablename__ = 'db_migrate_version'