comparison rhodecode/model/notification.py @ 1717:7ff304d3028f beta

Notification fixes - email prefix added to .ini files - html templates emails - rewrote email system to use some parts from pyramid_mailer
author Marcin Kuzminski <marcin@python-works.com>
date Fri, 25 Nov 2011 17:41:42 +0200
parents 7d1fc253549e
children e7eef7a1db6a
comparison
equal deleted inserted replaced
1716:7d1fc253549e 1717:7ff304d3028f
22 # GNU General Public License for more details. 22 # GNU General Public License for more details.
23 # 23 #
24 # You should have received a copy of the GNU General Public License 24 # You should have received a copy of the GNU General Public License
25 # along with this program. If not, see <http://www.gnu.org/licenses/>. 25 # along with this program. If not, see <http://www.gnu.org/licenses/>.
26 26
27 import os
27 import logging 28 import logging
28 import traceback 29 import traceback
29 30 import datetime
31
32 from pylons import config
30 from pylons.i18n.translation import _ 33 from pylons.i18n.translation import _
31 34
32 from rhodecode.lib.helpers import age 35 from rhodecode.lib import helpers as h
33
34 from rhodecode.model import BaseModel 36 from rhodecode.model import BaseModel
35 from rhodecode.model.db import Notification, User, UserNotification 37 from rhodecode.model.db import Notification, User, UserNotification
38 from rhodecode.lib.celerylib import run_task
39 from rhodecode.lib.celerylib.tasks import send_email
36 40
37 log = logging.getLogger(__name__) 41 log = logging.getLogger(__name__)
38 42
39 43
40 class NotificationModel(BaseModel): 44 class NotificationModel(BaseModel):
80 for u in recipients: 84 for u in recipients:
81 obj = self.__get_user(u) 85 obj = self.__get_user(u)
82 if obj: 86 if obj:
83 recipients_objs.append(obj) 87 recipients_objs.append(obj)
84 recipients_objs = set(recipients_objs) 88 recipients_objs = set(recipients_objs)
85 return Notification.create(created_by=created_by_obj, subject=subject, 89
86 body=body, recipients=recipients_objs, 90 notif = Notification.create(created_by=created_by_obj, subject=subject,
87 type_=type_) 91 body=body, recipients=recipients_objs,
92 type_=type_)
93
94
95 # send email with notification
96 for rec in recipients_objs:
97 email_subject = NotificationModel().make_description(notif, False)
98 type_ = EmailNotificationModel.TYPE_CHANGESET_COMMENT
99 email_body = body
100 email_body_html = EmailNotificationModel()\
101 .get_email_tmpl(type_, **{'subject':subject,
102 'body':h.rst(body)})
103 run_task(send_email, rec.email, email_subject, email_body,
104 email_body_html)
105
106 return notif
88 107
89 def delete(self, user, notification): 108 def delete(self, user, notification):
90 # we don't want to remove actual notification just the assignment 109 # we don't want to remove actual notification just the assignment
91 try: 110 try:
92 notification = self.__get_notification(notification) 111 notification = self.__get_notification(notification)
93 user = self.__get_user(user) 112 user = self.__get_user(user)
94 if notification and user: 113 if notification and user:
95 obj = UserNotification.query().filter(UserNotification.user == user)\ 114 obj = UserNotification.query()\
96 .filter(UserNotification.notification == notification).one() 115 .filter(UserNotification.user == user)\
116 .filter(UserNotification.notification
117 == notification)\
118 .one()
97 self.sa.delete(obj) 119 self.sa.delete(obj)
98 return True 120 return True
99 except Exception: 121 except Exception:
100 log.error(traceback.format_exc()) 122 log.error(traceback.format_exc())
101 raise 123 raise
122 144
123 return UserNotification.query()\ 145 return UserNotification.query()\
124 .filter(UserNotification.notification == notification)\ 146 .filter(UserNotification.notification == notification)\
125 .filter(UserNotification.user == user).scalar() 147 .filter(UserNotification.user == user).scalar()
126 148
127 def make_description(self, notification): 149 def make_description(self, notification, show_age=True):
128 """ 150 """
129 Creates a human readable description based on properties 151 Creates a human readable description based on properties
130 of notification object 152 of notification object
131 """ 153 """
132 154
133 _map = {notification.TYPE_CHANGESET_COMMENT:_('commented on commit'), 155 _map = {notification.TYPE_CHANGESET_COMMENT:_('commented on commit'),
134 notification.TYPE_MESSAGE:_('sent message'), 156 notification.TYPE_MESSAGE:_('sent message'),
135 notification.TYPE_MENTION:_('mentioned you')} 157 notification.TYPE_MENTION:_('mentioned you')}
158 DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
136 159
137 tmpl = "%(user)s %(action)s %(when)s" 160 tmpl = "%(user)s %(action)s %(when)s"
161 if show_age:
162 when = h.age(notification.created_on)
163 else:
164 DTF = lambda d: datetime.datetime.strftime(d, DATETIME_FORMAT)
165 when = DTF(notification.created_on)
138 data = dict(user=notification.created_by_user.username, 166 data = dict(user=notification.created_by_user.username,
139 action=_map[notification.type_], 167 action=_map[notification.type_],
140 when=age(notification.created_on)) 168 when=when)
141 return tmpl % data 169 return tmpl % data
170
171
172 class EmailNotificationModel(BaseModel):
173
174 TYPE_CHANGESET_COMMENT = 'changeset_comment'
175 TYPE_PASSWORD_RESET = 'passoword_link'
176 TYPE_REGISTRATION = 'registration'
177 TYPE_DEFAULT = 'default'
178
179 def __init__(self):
180 self._template_root = config['pylons.paths']['templates'][0]
181
182 self.email_types = {
183 self.TYPE_CHANGESET_COMMENT:'email_templates/changeset_comment.html',
184 self.TYPE_PASSWORD_RESET:'email_templates/password_reset.html',
185 self.TYPE_REGISTRATION:'email_templates/registration.html',
186 self.TYPE_DEFAULT:'email_templates/default.html'
187 }
188
189 def get_email_tmpl(self, type_, **kwargs):
190 """
191 return generated template for email based on given type
192
193 :param type_:
194 """
195 base = self.email_types.get(type_, self.TYPE_DEFAULT)
196
197 lookup = config['pylons.app_globals'].mako_lookup
198 email_template = lookup.get_template(base)
199 # translator inject
200 _kwargs = {'_':_}
201 _kwargs.update(kwargs)
202 log.debug('rendering tmpl %s with kwargs %s' % (base, _kwargs))
203 return email_template.render(**_kwargs)
204
205