##// END OF EJS Templates
notifications: fixed problem with 500 errors on non-numeric...
marcink -
r1812:7d0f908d default
parent child Browse files
Show More
@@ -1,178 +1,157 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2017 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21
22 22 """
23 23 notifications controller for RhodeCode
24 24 """
25 25
26 26 import logging
27 27 import traceback
28 28
29 29 from pylons import request
30 30 from pylons import tmpl_context as c, url
31 31 from pylons.controllers.util import redirect, abort
32 32 import webhelpers.paginate
33 33 from webob.exc import HTTPBadRequest
34 34
35 35 from rhodecode.lib import auth
36 36 from rhodecode.lib.auth import LoginRequired, NotAnonymous
37 37 from rhodecode.lib.base import BaseController, render
38 38 from rhodecode.lib import helpers as h
39 39 from rhodecode.lib.helpers import Page
40 40 from rhodecode.lib.utils2 import safe_int
41 41 from rhodecode.model.db import Notification
42 42 from rhodecode.model.notification import NotificationModel
43 43 from rhodecode.model.meta import Session
44 44
45 45
46 46 log = logging.getLogger(__name__)
47 47
48 48
49 49 class NotificationsController(BaseController):
50 50 """REST Controller styled on the Atom Publishing Protocol"""
51 # To properly map this controller, ensure your config/routing.py
52 # file has a resource setup:
53 # map.resource('notification', 'notifications', controller='_admin/notifications',
54 # path_prefix='/_admin', name_prefix='_admin_')
55 51
56 52 @LoginRequired()
57 53 @NotAnonymous()
58 54 def __before__(self):
59 55 super(NotificationsController, self).__before__()
60 56
61 57 def index(self):
62 58 """GET /_admin/notifications: All items in the collection"""
63 59 # url('notifications')
64 60 c.user = c.rhodecode_user
65 notif = NotificationModel().get_for_user(c.rhodecode_user.user_id,
66 filter_=request.GET.getall('type'))
61 notif = NotificationModel().get_for_user(
62 c.rhodecode_user.user_id, filter_=request.GET.getall('type'))
67 63
68 64 p = safe_int(request.GET.get('page', 1), 1)
69 65 notifications_url = webhelpers.paginate.PageURL(
70 66 url('notifications'), request.GET)
71 67 c.notifications = Page(notif, page=p, items_per_page=10,
72 68 url=notifications_url)
73 69 c.pull_request_type = Notification.TYPE_PULL_REQUEST
74 70 c.comment_type = [Notification.TYPE_CHANGESET_COMMENT,
75 71 Notification.TYPE_PULL_REQUEST_COMMENT]
76 72
77 73 _current_filter = request.GET.getall('type')
78 74 c.current_filter = 'all'
79 75 if _current_filter == [c.pull_request_type]:
80 76 c.current_filter = 'pull_request'
81 77 elif _current_filter == c.comment_type:
82 78 c.current_filter = 'comment'
83 79
84 80 if request.is_xhr:
85 81 return render('admin/notifications/notifications_data.mako')
86 82
87 83 return render('admin/notifications/notifications.mako')
88 84
89
90 85 @auth.CSRFRequired()
91 86 def mark_all_read(self):
92 87 if request.is_xhr:
93 88 nm = NotificationModel()
94 89 # mark all read
95 90 nm.mark_all_read_for_user(c.rhodecode_user.user_id,
96 91 filter_=request.GET.getall('type'))
97 92 Session().commit()
98 93 c.user = c.rhodecode_user
99 94 notif = nm.get_for_user(c.rhodecode_user.user_id,
100 95 filter_=request.GET.getall('type'))
101 96 notifications_url = webhelpers.paginate.PageURL(
102 97 url('notifications'), request.GET)
103 98 c.notifications = Page(notif, page=1, items_per_page=10,
104 99 url=notifications_url)
105 100 return render('admin/notifications/notifications_data.mako')
106 101
107 102 def _has_permissions(self, notification):
108 103 def is_owner():
109 104 user_id = c.rhodecode_user.user_id
110 105 for user_notification in notification.notifications_to_users:
111 106 if user_notification.user.user_id == user_id:
112 107 return True
113 108 return False
114 109 return h.HasPermissionAny('hg.admin')() or is_owner()
115 110
116 111 @auth.CSRFRequired()
117 112 def update(self, notification_id):
118 """PUT /_admin/notifications/id: Update an existing item"""
119 # Forms posted to this method should contain a hidden field:
120 # <input type="hidden" name="_method" value="PUT" />
121 # Or using helpers:
122 # h.form(url('notification', notification_id=ID),
123 # method='put')
124 # url('notification', notification_id=ID)
113 no = Notification.get_or_404(notification_id)
125 114 try:
126 no = Notification.get(notification_id)
127 115 if self._has_permissions(no):
128 116 # deletes only notification2user
129 117 NotificationModel().mark_read(c.rhodecode_user.user_id, no)
130 118 Session().commit()
131 119 return 'ok'
132 120 except Exception:
133 121 Session().rollback()
134 122 log.exception("Exception updating a notification item")
135 123 raise HTTPBadRequest()
136 124
137 125 @auth.CSRFRequired()
138 126 def delete(self, notification_id):
139 """DELETE /_admin/notifications/id: Delete an existing item"""
140 # Forms posted to this method should contain a hidden field:
141 # <input type="hidden" name="_method" value="DELETE" />
142 # Or using helpers:
143 # h.form(url('notification', notification_id=ID),
144 # method='delete')
145 # url('notification', notification_id=ID)
127 no = Notification.get_or_404(notification_id)
146 128 try:
147 no = Notification.get(notification_id)
148 129 if self._has_permissions(no):
149 130 # deletes only notification2user
150 131 NotificationModel().delete(c.rhodecode_user.user_id, no)
151 132 Session().commit()
152 133 return 'ok'
153 134 except Exception:
154 135 Session().rollback()
155 136 log.exception("Exception deleting a notification item")
156 137 raise HTTPBadRequest()
157 138
158 139 def show(self, notification_id):
159 """GET /_admin/notifications/id: Show a specific item"""
160 # url('notification', notification_id=ID)
161 140 c.user = c.rhodecode_user
162 no = Notification.get(notification_id)
141 no = Notification.get_or_404(notification_id)
163 142
164 143 if no and self._has_permissions(no):
165 144 unotification = NotificationModel()\
166 145 .get_user_notification(c.user.user_id, no)
167 146
168 147 # if this association to user is not valid, we don't want to show
169 148 # this message
170 149 if unotification:
171 150 if not unotification.read:
172 151 unotification.mark_as_read()
173 152 Session().commit()
174 153 c.notification = no
175 154
176 155 return render('admin/notifications/show_notification.mako')
177 156
178 157 return abort(403)
General Comments 0
You need to be logged in to leave comments. Login now