comparison rhodecode/controllers/admin/notifications.py @ 2031:82a88013a3fd

merge 1.3 into stable
author Marcin Kuzminski <marcin@python-works.com>
date Sun, 26 Feb 2012 17:25:09 +0200
parents 89efedac4e6c
children 64f7cf8f6a33
comparison
equal deleted inserted replaced
2005:ab0e122b38a7 2031:82a88013a3fd
1 # -*- coding: utf-8 -*-
2 """
3 rhodecode.controllers.admin.notifications
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5
6 notifications controller for RhodeCode
7
8 :created_on: Nov 23, 2010
9 :author: marcink
10 :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com>
11 :license: GPLv3, see COPYING for more details.
12 """
13 # This program is free software: you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation, either version 3 of the License, or
16 # (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25
26 import logging
27 import traceback
28
29 from pylons import request
30 from pylons import tmpl_context as c, url
31 from pylons.controllers.util import redirect
32
33 from rhodecode.lib.base import BaseController, render
34 from rhodecode.model.db import Notification
35
36 from rhodecode.model.notification import NotificationModel
37 from rhodecode.lib.auth import LoginRequired, NotAnonymous
38 from rhodecode.lib import helpers as h
39 from rhodecode.model.meta import Session
40
41
42 log = logging.getLogger(__name__)
43
44
45 class NotificationsController(BaseController):
46 """REST Controller styled on the Atom Publishing Protocol"""
47 # To properly map this controller, ensure your config/routing.py
48 # file has a resource setup:
49 # map.resource('notification', 'notifications', controller='_admin/notifications',
50 # path_prefix='/_admin', name_prefix='_admin_')
51
52 @LoginRequired()
53 @NotAnonymous()
54 def __before__(self):
55 super(NotificationsController, self).__before__()
56
57 def index(self, format='html'):
58 """GET /_admin/notifications: All items in the collection"""
59 # url('notifications')
60 c.user = self.rhodecode_user
61 c.notifications = NotificationModel()\
62 .get_for_user(self.rhodecode_user.user_id)
63 return render('admin/notifications/notifications.html')
64
65 def mark_all_read(self):
66 if request.environ.get('HTTP_X_PARTIAL_XHR'):
67 nm = NotificationModel()
68 # mark all read
69 nm.mark_all_read_for_user(self.rhodecode_user.user_id)
70 Session.commit()
71 c.user = self.rhodecode_user
72 c.notifications = nm.get_for_user(self.rhodecode_user.user_id)
73 return render('admin/notifications/notifications_data.html')
74
75 def create(self):
76 """POST /_admin/notifications: Create a new item"""
77 # url('notifications')
78
79 def new(self, format='html'):
80 """GET /_admin/notifications/new: Form to create a new item"""
81 # url('new_notification')
82
83 def update(self, notification_id):
84 """PUT /_admin/notifications/id: Update an existing item"""
85 # Forms posted to this method should contain a hidden field:
86 # <input type="hidden" name="_method" value="PUT" />
87 # Or using helpers:
88 # h.form(url('notification', notification_id=ID),
89 # method='put')
90 # url('notification', notification_id=ID)
91
92 def delete(self, notification_id):
93 """DELETE /_admin/notifications/id: Delete an existing item"""
94 # Forms posted to this method should contain a hidden field:
95 # <input type="hidden" name="_method" value="DELETE" />
96 # Or using helpers:
97 # h.form(url('notification', notification_id=ID),
98 # method='delete')
99 # url('notification', notification_id=ID)
100
101 try:
102 no = Notification.get(notification_id)
103 owner = lambda: (no.notifications_to_users.user.user_id
104 == c.rhodecode_user.user_id)
105 if h.HasPermissionAny('hg.admin', 'repository.admin')() or owner:
106 NotificationModel().delete(c.rhodecode_user.user_id, no)
107 Session.commit()
108 return 'ok'
109 except Exception:
110 Session.rollback()
111 log.error(traceback.format_exc())
112 return 'fail'
113
114 def show(self, notification_id, format='html'):
115 """GET /_admin/notifications/id: Show a specific item"""
116 # url('notification', notification_id=ID)
117 c.user = self.rhodecode_user
118 no = Notification.get(notification_id)
119
120 owner = lambda: (no.notifications_to_users.user.user_id
121 == c.user.user_id)
122 if no and (h.HasPermissionAny('hg.admin', 'repository.admin')() or owner):
123 unotification = NotificationModel()\
124 .get_user_notification(c.user.user_id, no)
125
126 # if this association to user is not valid, we don't want to show
127 # this message
128 if unotification:
129 if unotification.read is False:
130 unotification.mark_as_read()
131 Session.commit()
132 c.notification = no
133
134 return render('admin/notifications/show_notification.html')
135
136 return redirect(url('notifications'))
137
138 def edit(self, notification_id, format='html'):
139 """GET /_admin/notifications/id/edit: Form to edit an existing item"""
140 # url('edit_notification', notification_id=ID)