Mercurial > kallithea
annotate rhodecode/model/notification.py @ 2375:bc2d8c03c050 beta
Implemented #467 Journal logs comments on changesets
- added created/updated user action to journal
- added created/updated users group action journal
- added ip adresses to most of action_log calls to track IP changes
- rewrote action_parser to simpler and more efficient solution
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Sun, 03 Jun 2012 00:40:14 +0200 |
parents | a27e4d44237e |
children | 76947224bf27 7d517a35b6c9 |
rev | line source |
---|---|
1702
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
1 # -*- coding: utf-8 -*- |
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
2 """ |
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
3 rhodecode.model.notification |
1839 | 4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
1702
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
5 |
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
6 Model for notifications |
1800 | 7 |
8 | |
1702
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
9 :created_on: Nov 20, 2011 |
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
10 :author: marcink |
1824
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1818
diff
changeset
|
11 :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com> |
1702
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
12 :license: GPLv3, see COPYING for more details. |
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
13 """ |
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
14 # This program is free software: you can redistribute it and/or modify |
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
15 # it under the terms of the GNU General Public License as published by |
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
16 # the Free Software Foundation, either version 3 of the License, or |
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
17 # (at your option) any later version. |
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
18 # |
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
19 # This program is distributed in the hope that it will be useful, |
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
22 # GNU General Public License for more details. |
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
23 # |
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
24 # You should have received a copy of the GNU General Public License |
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
25 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
26 |
1717
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
27 import os |
1702
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
28 import logging |
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
29 import traceback |
1717
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
30 import datetime |
1702
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
31 |
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
32 from pylons.i18n.translation import _ |
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
33 |
1723
64e91067b996
- refactoring to overcome poor usage of global pylons config
Marcin Kuzminski <marcin@python-works.com>
parents:
1722
diff
changeset
|
34 import rhodecode |
2109 | 35 from rhodecode.config.conf import DATETIME_FORMAT |
1717
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
36 from rhodecode.lib import helpers as h |
1702
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
37 from rhodecode.model import BaseModel |
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
38 from rhodecode.model.db import Notification, User, UserNotification |
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
39 |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
40 log = logging.getLogger(__name__) |
1702
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
41 |
1716
7d1fc253549e
notification to commit author + gardening
Marcin Kuzminski <marcin@python-works.com>
parents:
1713
diff
changeset
|
42 |
1702
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
43 class NotificationModel(BaseModel): |
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
44 |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
45 def __get_user(self, user): |
1982
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1839
diff
changeset
|
46 return self._get_instance(User, user, callback=User.get_by_username) |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
47 |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
48 def __get_notification(self, notification): |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
49 if isinstance(notification, Notification): |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
50 return notification |
2149 | 51 elif isinstance(notification, (int, long)): |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
52 return Notification.get(notification) |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
53 else: |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
54 if notification: |
2149 | 55 raise Exception('notification must be int, long or Instance' |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
56 ' of Notification got %s' % type(notification)) |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
57 |
1731
31e6eb2fb4b2
implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents:
1723
diff
changeset
|
58 def create(self, created_by, subject, body, recipients=None, |
31e6eb2fb4b2
implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents:
1723
diff
changeset
|
59 type_=Notification.TYPE_MESSAGE, with_email=True, |
31e6eb2fb4b2
implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents:
1723
diff
changeset
|
60 email_kwargs={}): |
1703
f23828b00b21
notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1702
diff
changeset
|
61 """ |
1800 | 62 |
1703
f23828b00b21
notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1702
diff
changeset
|
63 Creates notification of given type |
1800 | 64 |
1703
f23828b00b21
notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1702
diff
changeset
|
65 :param created_by: int, str or User instance. User who created this |
f23828b00b21
notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1702
diff
changeset
|
66 notification |
f23828b00b21
notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1702
diff
changeset
|
67 :param subject: |
f23828b00b21
notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1702
diff
changeset
|
68 :param body: |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1800
diff
changeset
|
69 :param recipients: list of int, str or User objects, when None |
1731
31e6eb2fb4b2
implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents:
1723
diff
changeset
|
70 is given send to all admins |
1703
f23828b00b21
notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1702
diff
changeset
|
71 :param type_: type of notification |
1731
31e6eb2fb4b2
implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents:
1723
diff
changeset
|
72 :param with_email: send email with this notification |
31e6eb2fb4b2
implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents:
1723
diff
changeset
|
73 :param email_kwargs: additional dict to pass as args to email template |
1703
f23828b00b21
notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1702
diff
changeset
|
74 """ |
1722
e7eef7a1db6a
#235 forking page repo group selection
Marcin Kuzminski <marcin@python-works.com>
parents:
1717
diff
changeset
|
75 from rhodecode.lib.celerylib import tasks, run_task |
1702
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
76 |
1731
31e6eb2fb4b2
implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents:
1723
diff
changeset
|
77 if recipients and not getattr(recipients, '__iter__', False): |
1702
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
78 raise Exception('recipients must be a list of iterable') |
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
79 |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
80 created_by_obj = self.__get_user(created_by) |
1702
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
81 |
1731
31e6eb2fb4b2
implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents:
1723
diff
changeset
|
82 if recipients: |
31e6eb2fb4b2
implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents:
1723
diff
changeset
|
83 recipients_objs = [] |
31e6eb2fb4b2
implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents:
1723
diff
changeset
|
84 for u in recipients: |
31e6eb2fb4b2
implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents:
1723
diff
changeset
|
85 obj = self.__get_user(u) |
31e6eb2fb4b2
implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents:
1723
diff
changeset
|
86 if obj: |
31e6eb2fb4b2
implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents:
1723
diff
changeset
|
87 recipients_objs.append(obj) |
31e6eb2fb4b2
implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents:
1723
diff
changeset
|
88 recipients_objs = set(recipients_objs) |
2077 | 89 log.debug('sending notifications %s to %s' % ( |
90 type_, recipients_objs) | |
91 ) | |
1731
31e6eb2fb4b2
implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents:
1723
diff
changeset
|
92 else: |
31e6eb2fb4b2
implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents:
1723
diff
changeset
|
93 # empty recipients means to all admins |
31e6eb2fb4b2
implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents:
1723
diff
changeset
|
94 recipients_objs = User.query().filter(User.admin == True).all() |
2077 | 95 log.debug('sending notifications %s to admins: %s' % ( |
96 type_, recipients_objs) | |
97 ) | |
98 notif = Notification.create( | |
99 created_by=created_by_obj, subject=subject, | |
100 body=body, recipients=recipients_objs, type_=type_ | |
101 ) | |
1717
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
102 |
1731
31e6eb2fb4b2
implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents:
1723
diff
changeset
|
103 if with_email is False: |
31e6eb2fb4b2
implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents:
1723
diff
changeset
|
104 return notif |
31e6eb2fb4b2
implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents:
1723
diff
changeset
|
105 |
1717
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
106 # send email with notification |
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
107 for rec in recipients_objs: |
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
108 email_subject = NotificationModel().make_description(notif, False) |
1731
31e6eb2fb4b2
implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents:
1723
diff
changeset
|
109 type_ = type_ |
1717
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
110 email_body = body |
1800 | 111 kwargs = {'subject': subject, 'body': h.rst_w_mentions(body)} |
1731
31e6eb2fb4b2
implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents:
1723
diff
changeset
|
112 kwargs.update(email_kwargs) |
1717
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
113 email_body_html = EmailNotificationModel()\ |
1731
31e6eb2fb4b2
implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents:
1723
diff
changeset
|
114 .get_email_tmpl(type_, **kwargs) |
2156
a27e4d44237e
Use __unicode__ instead of __repr__ in models.
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
115 |
1722
e7eef7a1db6a
#235 forking page repo group selection
Marcin Kuzminski <marcin@python-works.com>
parents:
1717
diff
changeset
|
116 run_task(tasks.send_email, rec.email, email_subject, email_body, |
1717
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
117 email_body_html) |
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
118 |
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
119 return notif |
1702
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
120 |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
121 def delete(self, user, notification): |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
122 # we don't want to remove actual notification just the assignment |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
123 try: |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
124 notification = self.__get_notification(notification) |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
125 user = self.__get_user(user) |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
126 if notification and user: |
1717
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
127 obj = UserNotification.query()\ |
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
128 .filter(UserNotification.user == user)\ |
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
129 .filter(UserNotification.notification |
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
130 == notification)\ |
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
131 .one() |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
132 self.sa.delete(obj) |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
133 return True |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
134 except Exception: |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
135 log.error(traceback.format_exc()) |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
136 raise |
1702
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
137 |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
138 def get_for_user(self, user): |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
139 user = self.__get_user(user) |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
140 return user.notifications |
1702
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
141 |
1791
2aee0dc1784e
mark all read button for notifications
Marcin Kuzminski <marcin@python-works.com>
parents:
1769
diff
changeset
|
142 def mark_all_read_for_user(self, user): |
2aee0dc1784e
mark all read button for notifications
Marcin Kuzminski <marcin@python-works.com>
parents:
1769
diff
changeset
|
143 user = self.__get_user(user) |
2aee0dc1784e
mark all read button for notifications
Marcin Kuzminski <marcin@python-works.com>
parents:
1769
diff
changeset
|
144 UserNotification.query()\ |
2aee0dc1784e
mark all read button for notifications
Marcin Kuzminski <marcin@python-works.com>
parents:
1769
diff
changeset
|
145 .filter(UserNotification.read==False)\ |
2aee0dc1784e
mark all read button for notifications
Marcin Kuzminski <marcin@python-works.com>
parents:
1769
diff
changeset
|
146 .update({'read': True}) |
2aee0dc1784e
mark all read button for notifications
Marcin Kuzminski <marcin@python-works.com>
parents:
1769
diff
changeset
|
147 |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
148 def get_unread_cnt_for_user(self, user): |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
149 user = self.__get_user(user) |
1702
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
150 return UserNotification.query()\ |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
151 .filter(UserNotification.read == False)\ |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
152 .filter(UserNotification.user == user).count() |
1702
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
153 |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
154 def get_unread_for_user(self, user): |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
155 user = self.__get_user(user) |
1702
8cb7f5c4d494
#302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
156 return [x.notification for x in UserNotification.query()\ |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
157 .filter(UserNotification.read == False)\ |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
158 .filter(UserNotification.user == user).all()] |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
159 |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
160 def get_user_notification(self, user, notification): |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
161 user = self.__get_user(user) |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
162 notification = self.__get_notification(notification) |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
163 |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
164 return UserNotification.query()\ |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
165 .filter(UserNotification.notification == notification)\ |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
166 .filter(UserNotification.user == user).scalar() |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
167 |
1717
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
168 def make_description(self, notification, show_age=True): |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
169 """ |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
170 Creates a human readable description based on properties |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
171 of notification object |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
172 """ |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
173 |
2082 | 174 _map = { |
175 notification.TYPE_CHANGESET_COMMENT: _('commented on commit'), | |
176 notification.TYPE_MESSAGE: _('sent message'), | |
177 notification.TYPE_MENTION: _('mentioned you'), | |
178 notification.TYPE_REGISTRATION: _('registered in RhodeCode') | |
179 } | |
1731
31e6eb2fb4b2
implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents:
1723
diff
changeset
|
180 |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
181 tmpl = "%(user)s %(action)s %(when)s" |
1717
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
182 if show_age: |
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
183 when = h.age(notification.created_on) |
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
184 else: |
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
185 DTF = lambda d: datetime.datetime.strftime(d, DATETIME_FORMAT) |
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
186 when = DTF(notification.created_on) |
2156
a27e4d44237e
Use __unicode__ instead of __repr__ in models.
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
187 |
2082 | 188 data = dict( |
189 user=notification.created_by_user.username, | |
190 action=_map[notification.type_], when=when, | |
191 ) | |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
192 return tmpl % data |
1717
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
193 |
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
194 |
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
195 class EmailNotificationModel(BaseModel): |
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
196 |
1732 | 197 TYPE_CHANGESET_COMMENT = Notification.TYPE_CHANGESET_COMMENT |
1717
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
198 TYPE_PASSWORD_RESET = 'passoword_link' |
1732 | 199 TYPE_REGISTRATION = Notification.TYPE_REGISTRATION |
1717
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
200 TYPE_DEFAULT = 'default' |
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
201 |
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
202 def __init__(self): |
1723
64e91067b996
- refactoring to overcome poor usage of global pylons config
Marcin Kuzminski <marcin@python-works.com>
parents:
1722
diff
changeset
|
203 self._template_root = rhodecode.CONFIG['pylons.paths']['templates'][0] |
64e91067b996
- refactoring to overcome poor usage of global pylons config
Marcin Kuzminski <marcin@python-works.com>
parents:
1722
diff
changeset
|
204 self._tmpl_lookup = rhodecode.CONFIG['pylons.app_globals'].mako_lookup |
1717
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
205 |
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
206 self.email_types = { |
2082 | 207 self.TYPE_CHANGESET_COMMENT: 'email_templates/changeset_comment.html', |
208 self.TYPE_PASSWORD_RESET: 'email_templates/password_reset.html', | |
209 self.TYPE_REGISTRATION: 'email_templates/registration.html', | |
210 self.TYPE_DEFAULT: 'email_templates/default.html' | |
1717
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
211 } |
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
212 |
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
213 def get_email_tmpl(self, type_, **kwargs): |
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
214 """ |
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
215 return generated template for email based on given type |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1800
diff
changeset
|
216 |
1717
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
217 :param type_: |
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
218 """ |
1723
64e91067b996
- refactoring to overcome poor usage of global pylons config
Marcin Kuzminski <marcin@python-works.com>
parents:
1722
diff
changeset
|
219 |
1732 | 220 base = self.email_types.get(type_, self.email_types[self.TYPE_DEFAULT]) |
1723
64e91067b996
- refactoring to overcome poor usage of global pylons config
Marcin Kuzminski <marcin@python-works.com>
parents:
1722
diff
changeset
|
221 email_template = self._tmpl_lookup.get_template(base) |
1717
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
222 # translator inject |
2082 | 223 _kwargs = {'_': _} |
1717
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
224 _kwargs.update(kwargs) |
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
225 log.debug('rendering tmpl %s with kwargs %s' % (base, _kwargs)) |
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
226 return email_template.render(**_kwargs) |