changeset 4749:3bf88f142f3d

usernotifications: explicitly sort by date and join notifications and users - don't depend on lazy join
author Mads Kiilerich <madski@unity3d.com>
date Tue, 06 Jan 2015 00:54:36 +0100
parents b91c5a6327bd
children 3752cf4972a7
files kallithea/model/db.py kallithea/model/notification.py
diffstat 2 files changed, 6 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/model/db.py	Tue Jan 06 00:54:36 2015 +0100
+++ b/kallithea/model/db.py	Tue Jan 06 00:54:36 2015 +0100
@@ -2388,8 +2388,7 @@
     sent_on = Column('sent_on', DateTime(timezone=False), nullable=True, unique=None)
 
     user = relationship('User', lazy="joined")
-    notification = relationship('Notification', lazy="joined",
-                                order_by=lambda: Notification.created_on.desc(),)
+    notification = relationship('Notification', lazy="joined")
 
     def mark_as_read(self):
         self.read = True
--- a/kallithea/model/notification.py	Tue Jan 06 00:54:36 2015 +0100
+++ b/kallithea/model/notification.py	Tue Jan 06 00:54:36 2015 +0100
@@ -31,6 +31,7 @@
 
 from pylons import tmpl_context as c
 from pylons.i18n.translation import _
+from sqlalchemy.orm import joinedload, subqueryload
 
 import kallithea
 from kallithea.lib import helpers as h
@@ -167,7 +168,10 @@
         q = UserNotification.query()\
             .filter(UserNotification.user == user)\
             .join((Notification, UserNotification.notification_id ==
-                                 Notification.notification_id))
+                                 Notification.notification_id))\
+            .options(joinedload('notification'))\
+            .options(subqueryload('notification.created_by_user'))\
+            .order_by(Notification.created_on.desc())
 
         if filter_:
             q = q.filter(Notification.type_.in_(filter_))