Mercurial > kallithea
annotate rhodecode/controllers/admin/notifications.py @ 1713:54687aa00724 beta
Tests updates, Session refactoring
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Wed, 23 Nov 2011 15:36:57 +0200 |
parents | cac5109ac3b6 |
children | 64e91067b996 |
rev | line source |
---|---|
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
1 import logging |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
2 import traceback |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
3 |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
4 from pylons import tmpl_context as c, url |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
5 |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
6 from rhodecode.lib.base import BaseController, render |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
7 from rhodecode.model.db import Notification |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
8 |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
9 from rhodecode.model.notification import NotificationModel |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
10 from rhodecode.lib.auth import LoginRequired |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
11 from rhodecode.lib import helpers as h |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
12 from rhodecode.model.meta import Session |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
13 from pylons.controllers.util import redirect |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
14 |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
15 log = logging.getLogger(__name__) |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
16 |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
17 class NotificationsController(BaseController): |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
18 """REST Controller styled on the Atom Publishing Protocol""" |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
19 # To properly map this controller, ensure your config/routing.py |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
20 # file has a resource setup: |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
21 # map.resource('notification', 'notifications', controller='_admin/notifications', |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
22 # path_prefix='/_admin', name_prefix='_admin_') |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
23 |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
24 @LoginRequired() |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
25 def __before__(self): |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
26 super(NotificationsController, self).__before__() |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
27 |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
28 |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
29 def index(self, format='html'): |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
30 """GET /_admin/notifications: All items in the collection""" |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
31 # url('notifications') |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
32 c.user = self.rhodecode_user |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
33 c.notifications = NotificationModel()\ |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
34 .get_for_user(self.rhodecode_user.user_id) |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
35 return render('admin/notifications/notifications.html') |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
36 |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
37 def create(self): |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
38 """POST /_admin/notifications: Create a new item""" |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
39 # url('notifications') |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
40 |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
41 def new(self, format='html'): |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
42 """GET /_admin/notifications/new: Form to create a new item""" |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
43 # url('new_notification') |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
44 |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
45 def update(self, notification_id): |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
46 """PUT /_admin/notifications/id: Update an existing item""" |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
47 # Forms posted to this method should contain a hidden field: |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
48 # <input type="hidden" name="_method" value="PUT" /> |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
49 # Or using helpers: |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
50 # h.form(url('notification', notification_id=ID), |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
51 # method='put') |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
52 # url('notification', notification_id=ID) |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
53 |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
54 def delete(self, notification_id): |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
55 """DELETE /_admin/notifications/id: Delete an existing item""" |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
56 # Forms posted to this method should contain a hidden field: |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
57 # <input type="hidden" name="_method" value="DELETE" /> |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
58 # Or using helpers: |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
59 # h.form(url('notification', notification_id=ID), |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
60 # method='delete') |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
61 # url('notification', notification_id=ID) |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
62 |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
63 try: |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
64 no = Notification.get(notification_id) |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
65 owner = lambda: (no.notifications_to_users.user.user_id |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
66 == c.rhodecode_user.user_id) |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
67 if h.HasPermissionAny('hg.admin', 'repository.admin')() or owner: |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
68 NotificationModel().delete(c.rhodecode_user.user_id, no) |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
69 Session.commit() |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
70 return 'ok' |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
71 except Exception: |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
72 Session.rollback() |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
73 log.error(traceback.format_exc()) |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
74 return 'fail' |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
75 |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
76 def show(self, notification_id, format='html'): |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
77 """GET /_admin/notifications/id: Show a specific item""" |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
78 # url('notification', notification_id=ID) |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
79 c.user = self.rhodecode_user |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
80 no = Notification.get(notification_id) |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
81 |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
82 owner = lambda: (no.notifications_to_users.user.user_id |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
83 == c.user.user_id) |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
84 if no and (h.HasPermissionAny('hg.admin', 'repository.admin')() or owner): |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
85 unotification = NotificationModel()\ |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
86 .get_user_notification(c.user.user_id, no) |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
87 |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
88 # if this association to user is not valid, we don't want to show |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
89 # this message |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
90 if unotification: |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
91 if unotification.read is False: |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
92 unotification.mark_as_read() |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
93 Session.commit() |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
94 c.notification = no |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
95 |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
96 return render('admin/notifications/show_notification.html') |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
97 |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
98 return redirect(url('notifications')) |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
99 |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
100 def edit(self, notification_id, format='html'): |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
101 """GET /_admin/notifications/id/edit: Form to edit an existing item""" |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
102 # url('edit_notification', notification_id=ID) |