comparison rhodecode/model/notification.py @ 1703:f23828b00b21 beta

notification fixes and improvements
author Marcin Kuzminski <marcin@python-works.com>
date Sun, 20 Nov 2011 22:26:55 +0200
parents 8cb7f5c4d494
children cac5109ac3b6
comparison
equal deleted inserted replaced
1702:8cb7f5c4d494 1703:f23828b00b21
36 from rhodecode.model.db import Notification, User, UserNotification 36 from rhodecode.model.db import Notification, User, UserNotification
37 37
38 38
39 class NotificationModel(BaseModel): 39 class NotificationModel(BaseModel):
40 40
41 def create(self, subject, body, recipients): 41 def create(self, created_by, subject, body, recipients,
42 type_=Notification.TYPE_MESSAGE):
43 """
44
45 Creates notification of given type
46
47 :param created_by: int, str or User instance. User who created this
48 notification
49 :param subject:
50 :param body:
51 :param recipients: list of int, str or User objects
52 :param type_: type of notification
53 """
42 54
43 if not getattr(recipients, '__iter__', False): 55 if not getattr(recipients, '__iter__', False):
44 raise Exception('recipients must be a list of iterable') 56 raise Exception('recipients must be a list of iterable')
45 57
46 for x in recipients: 58 created_by_obj = created_by
47 if not isinstance(x, User): 59 if not isinstance(created_by, User):
48 raise Exception('recipient is not instance of %s got %s' % \ 60 created_by_obj = User.get(created_by)
49 (User, type(x)))
50 61
51 62
52 Notification.create(subject, body, recipients) 63 recipients_objs = []
64 for u in recipients:
65 if isinstance(u, User):
66 recipients_objs.append(u)
67 elif isinstance(u, basestring):
68 recipients_objs.append(User.get_by_username(username=u))
69 elif isinstance(u, int):
70 recipients_objs.append(User.get(u))
71 else:
72 raise Exception('Unsupported recipient must be one of int,'
73 'str or User object')
74
75 Notification.create(created_by=created_by_obj, subject=subject,
76 body = body, recipients = recipients_objs,
77 type_=type_)
53 78
54 79
55 def get_for_user(self, user_id): 80 def get_for_user(self, user_id):
56 return User.get(user_id).notifications 81 return User.get(user_id).notifications
57 82