comparison rhodecode/controllers/admin/notifications.py @ 1712:cac5109ac3b6 beta

Notification system improvements - deleting - tests - ui - moved to separate controller
author Marcin Kuzminski <marcin@python-works.com>
date Wed, 23 Nov 2011 00:55:05 +0200
parents
children 54687aa00724
comparison
equal deleted inserted replaced
1711:b369bec5d468 1712:cac5109ac3b6
1 import logging
2
3 from pylons import tmpl_context as c
4
5 from rhodecode.lib.base import BaseController, render
6 from rhodecode.model.db import Notification
7
8 from rhodecode.model.notification import NotificationModel
9 from rhodecode.lib.auth import LoginRequired
10 from rhodecode.lib import helpers as h
11
12 log = logging.getLogger(__name__)
13
14 class NotificationsController(BaseController):
15 """REST Controller styled on the Atom Publishing Protocol"""
16 # To properly map this controller, ensure your config/routing.py
17 # file has a resource setup:
18 # map.resource('notification', 'notifications', controller='_admin/notifications',
19 # path_prefix='/_admin', name_prefix='_admin_')
20
21 @LoginRequired()
22 def __before__(self):
23 super(NotificationsController, self).__before__()
24
25
26 def index(self, format='html'):
27 """GET /_admin/notifications: All items in the collection"""
28 # url('notifications')
29 c.user = self.rhodecode_user
30 c.notifications = NotificationModel()\
31 .get_for_user(self.rhodecode_user.user_id)
32 return render('admin/notifications/notifications.html')
33
34 def create(self):
35 """POST /_admin/notifications: Create a new item"""
36 # url('notifications')
37
38 def new(self, format='html'):
39 """GET /_admin/notifications/new: Form to create a new item"""
40 # url('new_notification')
41
42 def update(self, notification_id):
43 """PUT /_admin/notifications/id: Update an existing item"""
44 # Forms posted to this method should contain a hidden field:
45 # <input type="hidden" name="_method" value="PUT" />
46 # Or using helpers:
47 # h.form(url('notification', notification_id=ID),
48 # method='put')
49 # url('notification', notification_id=ID)
50
51 def delete(self, notification_id):
52 """DELETE /_admin/notifications/id: Delete an existing item"""
53 # Forms posted to this method should contain a hidden field:
54 # <input type="hidden" name="_method" value="DELETE" />
55 # Or using helpers:
56 # h.form(url('notification', notification_id=ID),
57 # method='delete')
58 # url('notification', notification_id=ID)
59
60 no = Notification.get(notification_id)
61 owner = lambda: no.notifications_to_users.user.user_id == c.rhodecode_user.user_id
62 if h.HasPermissionAny('hg.admin', 'repository.admin')() or owner:
63 NotificationModel().delete(notification_id)
64 return 'ok'
65 return 'fail'
66
67 def show(self, notification_id, format='html'):
68 """GET /_admin/notifications/id: Show a specific item"""
69 # url('notification', notification_id=ID)
70 c.user = self.rhodecode_user
71 c.notification = Notification.get(notification_id)
72
73 unotification = NotificationModel()\
74 .get_user_notification(c.user.user_id,
75 c.notification)
76
77 if unotification.read is False:
78 unotification.mark_as_read()
79
80 return render('admin/notifications/show_notification.html')
81
82 def edit(self, notification_id, format='html'):
83 """GET /_admin/notifications/id/edit: Form to edit an existing item"""
84 # url('edit_notification', notification_id=ID)