comparison rhodecode/model/notification.py @ 2432:d3ac7491a5c8 codereview

Share common getter functions in base model, and remove duplicated functions from other models
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 09 Jun 2012 20:23:48 +0200
parents 91fae60bf2b6
children 74f2910f7ad9
comparison
equal deleted inserted replaced
2431:60dfc369df1d 2432:d3ac7491a5c8
40 log = logging.getLogger(__name__) 40 log = logging.getLogger(__name__)
41 41
42 42
43 class NotificationModel(BaseModel): 43 class NotificationModel(BaseModel):
44 44
45 def __get_user(self, user):
46 return self._get_instance(User, user, callback=User.get_by_username)
47
48 def __get_notification(self, notification): 45 def __get_notification(self, notification):
49 if isinstance(notification, Notification): 46 if isinstance(notification, Notification):
50 return notification 47 return notification
51 elif isinstance(notification, (int, long)): 48 elif isinstance(notification, (int, long)):
52 return Notification.get(notification) 49 return Notification.get(notification)
75 from rhodecode.lib.celerylib import tasks, run_task 72 from rhodecode.lib.celerylib import tasks, run_task
76 73
77 if recipients and not getattr(recipients, '__iter__', False): 74 if recipients and not getattr(recipients, '__iter__', False):
78 raise Exception('recipients must be a list of iterable') 75 raise Exception('recipients must be a list of iterable')
79 76
80 created_by_obj = self.__get_user(created_by) 77 created_by_obj = self._get_user(created_by)
81 78
82 if recipients: 79 if recipients:
83 recipients_objs = [] 80 recipients_objs = []
84 for u in recipients: 81 for u in recipients:
85 obj = self.__get_user(u) 82 obj = self._get_user(u)
86 if obj: 83 if obj:
87 recipients_objs.append(obj) 84 recipients_objs.append(obj)
88 recipients_objs = set(recipients_objs) 85 recipients_objs = set(recipients_objs)
89 log.debug('sending notifications %s to %s' % ( 86 log.debug('sending notifications %s to %s' % (
90 type_, recipients_objs) 87 type_, recipients_objs)
124 121
125 def delete(self, user, notification): 122 def delete(self, user, notification):
126 # we don't want to remove actual notification just the assignment 123 # we don't want to remove actual notification just the assignment
127 try: 124 try:
128 notification = self.__get_notification(notification) 125 notification = self.__get_notification(notification)
129 user = self.__get_user(user) 126 user = self._get_user(user)
130 if notification and user: 127 if notification and user:
131 obj = UserNotification.query()\ 128 obj = UserNotification.query()\
132 .filter(UserNotification.user == user)\ 129 .filter(UserNotification.user == user)\
133 .filter(UserNotification.notification 130 .filter(UserNotification.notification
134 == notification)\ 131 == notification)\
138 except Exception: 135 except Exception:
139 log.error(traceback.format_exc()) 136 log.error(traceback.format_exc())
140 raise 137 raise
141 138
142 def get_for_user(self, user): 139 def get_for_user(self, user):
143 user = self.__get_user(user) 140 user = self._get_user(user)
144 return user.notifications 141 return user.notifications
145 142
146 def mark_all_read_for_user(self, user): 143 def mark_all_read_for_user(self, user):
147 user = self.__get_user(user) 144 user = self._get_user(user)
148 UserNotification.query()\ 145 UserNotification.query()\
149 .filter(UserNotification.read == False)\ 146 .filter(UserNotification.read == False)\
150 .update({'read': True}) 147 .update({'read': True})
151 148
152 def get_unread_cnt_for_user(self, user): 149 def get_unread_cnt_for_user(self, user):
153 user = self.__get_user(user) 150 user = self._get_user(user)
154 return UserNotification.query()\ 151 return UserNotification.query()\
155 .filter(UserNotification.read == False)\ 152 .filter(UserNotification.read == False)\
156 .filter(UserNotification.user == user).count() 153 .filter(UserNotification.user == user).count()
157 154
158 def get_unread_for_user(self, user): 155 def get_unread_for_user(self, user):
159 user = self.__get_user(user) 156 user = self._get_user(user)
160 return [x.notification for x in UserNotification.query()\ 157 return [x.notification for x in UserNotification.query()\
161 .filter(UserNotification.read == False)\ 158 .filter(UserNotification.read == False)\
162 .filter(UserNotification.user == user).all()] 159 .filter(UserNotification.user == user).all()]
163 160
164 def get_user_notification(self, user, notification): 161 def get_user_notification(self, user, notification):
165 user = self.__get_user(user) 162 user = self._get_user(user)
166 notification = self.__get_notification(notification) 163 notification = self.__get_notification(notification)
167 164
168 return UserNotification.query()\ 165 return UserNotification.query()\
169 .filter(UserNotification.notification == notification)\ 166 .filter(UserNotification.notification == notification)\
170 .filter(UserNotification.user == user).scalar() 167 .filter(UserNotification.user == user).scalar()