comparison rhodecode/model/notification.py @ 1731:31e6eb2fb4b2 beta

implements #222 registration feedback - a notification message is created for admins - email template with registartion
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 26 Nov 2011 21:13:33 +0200
parents 64e91067b996
children 8321b3d19b1f
comparison
equal deleted inserted replaced
1730:ce0b47534c36 1731:31e6eb2fb4b2
55 else: 55 else:
56 if notification: 56 if notification:
57 raise Exception('notification must be int or Instance' 57 raise Exception('notification must be int or Instance'
58 ' of Notification got %s' % type(notification)) 58 ' of Notification got %s' % type(notification))
59 59
60 def create(self, created_by, subject, body, recipients, 60 def create(self, created_by, subject, body, recipients=None,
61 type_=Notification.TYPE_MESSAGE): 61 type_=Notification.TYPE_MESSAGE, with_email=True,
62 email_kwargs={}):
62 """ 63 """
63 64
64 Creates notification of given type 65 Creates notification of given type
65 66
66 :param created_by: int, str or User instance. User who created this 67 :param created_by: int, str or User instance. User who created this
67 notification 68 notification
68 :param subject: 69 :param subject:
69 :param body: 70 :param body:
70 :param recipients: list of int, str or User objects 71 :param recipients: list of int, str or User objects, when None
72 is given send to all admins
71 :param type_: type of notification 73 :param type_: type of notification
74 :param with_email: send email with this notification
75 :param email_kwargs: additional dict to pass as args to email template
72 """ 76 """
73 from rhodecode.lib.celerylib import tasks, run_task 77 from rhodecode.lib.celerylib import tasks, run_task
74 78
75 if not getattr(recipients, '__iter__', False): 79 if recipients and not getattr(recipients, '__iter__', False):
76 raise Exception('recipients must be a list of iterable') 80 raise Exception('recipients must be a list of iterable')
77 81
78 created_by_obj = self.__get_user(created_by) 82 created_by_obj = self.__get_user(created_by)
79 83
80 recipients_objs = [] 84 if recipients:
81 for u in recipients: 85 recipients_objs = []
82 obj = self.__get_user(u) 86 for u in recipients:
83 if obj: 87 obj = self.__get_user(u)
84 recipients_objs.append(obj) 88 if obj:
85 recipients_objs = set(recipients_objs) 89 recipients_objs.append(obj)
90 recipients_objs = set(recipients_objs)
91 else:
92 # empty recipients means to all admins
93 recipients_objs = User.query().filter(User.admin == True).all()
86 94
87 notif = Notification.create(created_by=created_by_obj, subject=subject, 95 notif = Notification.create(created_by=created_by_obj, subject=subject,
88 body=body, recipients=recipients_objs, 96 body=body, recipients=recipients_objs,
89 type_=type_) 97 type_=type_)
90 98
99 if with_email is False:
100 return notif
101
91 # send email with notification 102 # send email with notification
92 for rec in recipients_objs: 103 for rec in recipients_objs:
93 email_subject = NotificationModel().make_description(notif, False) 104 email_subject = NotificationModel().make_description(notif, False)
94 type_ = EmailNotificationModel.TYPE_CHANGESET_COMMENT 105 type_ = type_
95 email_body = body 106 email_body = body
107 kwargs = {'subject':subject, 'body':h.rst(body)}
108 kwargs.update(email_kwargs)
96 email_body_html = EmailNotificationModel()\ 109 email_body_html = EmailNotificationModel()\
97 .get_email_tmpl(type_, **{'subject':subject, 110 .get_email_tmpl(type_, **kwargs)
98 'body':h.rst(body)})
99 run_task(tasks.send_email, rec.email, email_subject, email_body, 111 run_task(tasks.send_email, rec.email, email_subject, email_body,
100 email_body_html) 112 email_body_html)
101 113
102 return notif 114 return notif
103 115
148 of notification object 160 of notification object
149 """ 161 """
150 162
151 _map = {notification.TYPE_CHANGESET_COMMENT:_('commented on commit'), 163 _map = {notification.TYPE_CHANGESET_COMMENT:_('commented on commit'),
152 notification.TYPE_MESSAGE:_('sent message'), 164 notification.TYPE_MESSAGE:_('sent message'),
153 notification.TYPE_MENTION:_('mentioned you')} 165 notification.TYPE_MENTION:_('mentioned you'),
166 notification.TYPE_REGISTRATION:_('registered in RhodeCode')}
167
154 DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S" 168 DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
155 169
156 tmpl = "%(user)s %(action)s %(when)s" 170 tmpl = "%(user)s %(action)s %(when)s"
157 if show_age: 171 if show_age:
158 when = h.age(notification.created_on) 172 when = h.age(notification.created_on)