comparison rhodecode/model/notification.py @ 1723:64e91067b996 beta

- refactoring to overcome poor usage of global pylons config - db transaction fixes - fixed tests - garden
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 26 Nov 2011 02:16:21 +0200
parents e7eef7a1db6a
children 31e6eb2fb4b2
comparison
equal deleted inserted replaced
1722:e7eef7a1db6a 1723:64e91067b996
27 import os 27 import os
28 import logging 28 import logging
29 import traceback 29 import traceback
30 import datetime 30 import datetime
31 31
32 from pylons import config
33 from pylons.i18n.translation import _ 32 from pylons.i18n.translation import _
34 33
34 import rhodecode
35 from rhodecode.lib import helpers as h 35 from rhodecode.lib import helpers as h
36 from rhodecode.model import BaseModel 36 from rhodecode.model import BaseModel
37 from rhodecode.model.db import Notification, User, UserNotification 37 from rhodecode.model.db import Notification, User, UserNotification
38 38
39 log = logging.getLogger(__name__) 39 log = logging.getLogger(__name__)
40 40
41 41
42 class NotificationModel(BaseModel): 42 class NotificationModel(BaseModel):
43
44 43
45 def __get_user(self, user): 44 def __get_user(self, user):
46 if isinstance(user, basestring): 45 if isinstance(user, basestring):
47 return User.get_by_username(username=user) 46 return User.get_by_username(username=user)
48 else: 47 else:
56 else: 55 else:
57 if notification: 56 if notification:
58 raise Exception('notification must be int or Instance' 57 raise Exception('notification must be int or Instance'
59 ' of Notification got %s' % type(notification)) 58 ' of Notification got %s' % type(notification))
60 59
61
62 def create(self, created_by, subject, body, recipients, 60 def create(self, created_by, subject, body, recipients,
63 type_=Notification.TYPE_MESSAGE): 61 type_=Notification.TYPE_MESSAGE):
64 """ 62 """
65 63
66 Creates notification of given type 64 Creates notification of given type
87 recipients_objs = set(recipients_objs) 85 recipients_objs = set(recipients_objs)
88 86
89 notif = Notification.create(created_by=created_by_obj, subject=subject, 87 notif = Notification.create(created_by=created_by_obj, subject=subject,
90 body=body, recipients=recipients_objs, 88 body=body, recipients=recipients_objs,
91 type_=type_) 89 type_=type_)
92
93 90
94 # send email with notification 91 # send email with notification
95 for rec in recipients_objs: 92 for rec in recipients_objs:
96 email_subject = NotificationModel().make_description(notif, False) 93 email_subject = NotificationModel().make_description(notif, False)
97 type_ = EmailNotificationModel.TYPE_CHANGESET_COMMENT 94 type_ = EmailNotificationModel.TYPE_CHANGESET_COMMENT
174 TYPE_PASSWORD_RESET = 'passoword_link' 171 TYPE_PASSWORD_RESET = 'passoword_link'
175 TYPE_REGISTRATION = 'registration' 172 TYPE_REGISTRATION = 'registration'
176 TYPE_DEFAULT = 'default' 173 TYPE_DEFAULT = 'default'
177 174
178 def __init__(self): 175 def __init__(self):
179 self._template_root = config['pylons.paths']['templates'][0] 176 self._template_root = rhodecode.CONFIG['pylons.paths']['templates'][0]
177 self._tmpl_lookup = rhodecode.CONFIG['pylons.app_globals'].mako_lookup
180 178
181 self.email_types = { 179 self.email_types = {
182 self.TYPE_CHANGESET_COMMENT:'email_templates/changeset_comment.html', 180 self.TYPE_CHANGESET_COMMENT:'email_templates/changeset_comment.html',
183 self.TYPE_PASSWORD_RESET:'email_templates/password_reset.html', 181 self.TYPE_PASSWORD_RESET:'email_templates/password_reset.html',
184 self.TYPE_REGISTRATION:'email_templates/registration.html', 182 self.TYPE_REGISTRATION:'email_templates/registration.html',
189 """ 187 """
190 return generated template for email based on given type 188 return generated template for email based on given type
191 189
192 :param type_: 190 :param type_:
193 """ 191 """
192
194 base = self.email_types.get(type_, self.TYPE_DEFAULT) 193 base = self.email_types.get(type_, self.TYPE_DEFAULT)
195 194 email_template = self._tmpl_lookup.get_template(base)
196 lookup = config['pylons.app_globals'].mako_lookup
197 email_template = lookup.get_template(base)
198 # translator inject 195 # translator inject
199 _kwargs = {'_':_} 196 _kwargs = {'_':_}
200 _kwargs.update(kwargs) 197 _kwargs.update(kwargs)
201 log.debug('rendering tmpl %s with kwargs %s' % (base, _kwargs)) 198 log.debug('rendering tmpl %s with kwargs %s' % (base, _kwargs))
202 return email_template.render(**_kwargs) 199 return email_template.render(**_kwargs)