comparison rhodecode/controllers/admin/notifications.py @ 2776:63e58ef80ef1

Merge beta branch into stable
author Marcin Kuzminski <marcin@python-works.com>
date Sun, 02 Sep 2012 21:19:54 +0200
parents 91c442a489bb
children 6b176c679896
comparison
equal deleted inserted replaced
2301:9d097c2592d3 2776:63e58ef80ef1
58 58
59 def index(self, format='html'): 59 def index(self, format='html'):
60 """GET /_admin/notifications: All items in the collection""" 60 """GET /_admin/notifications: All items in the collection"""
61 # url('notifications') 61 # url('notifications')
62 c.user = self.rhodecode_user 62 c.user = self.rhodecode_user
63 notif = NotificationModel().get_for_user(self.rhodecode_user.user_id) 63 notif = NotificationModel().get_for_user(self.rhodecode_user.user_id,
64 filter_=request.GET.getall('type'))
64 p = int(request.params.get('page', 1)) 65 p = int(request.params.get('page', 1))
65 c.notifications = Page(notif, page=p, items_per_page=10) 66 c.notifications = Page(notif, page=p, items_per_page=10)
67 c.pull_request_type = Notification.TYPE_PULL_REQUEST
68 c.comment_type = [Notification.TYPE_CHANGESET_COMMENT,
69 Notification.TYPE_PULL_REQUEST_COMMENT]
70
71 _current_filter = request.GET.getall('type')
72 c.current_filter = 'all'
73 if _current_filter == [c.pull_request_type]:
74 c.current_filter = 'pull_request'
75 elif _current_filter == c.comment_type:
76 c.current_filter = 'comment'
77
66 return render('admin/notifications/notifications.html') 78 return render('admin/notifications/notifications.html')
67 79
68 def mark_all_read(self): 80 def mark_all_read(self):
69 if request.environ.get('HTTP_X_PARTIAL_XHR'): 81 if request.environ.get('HTTP_X_PARTIAL_XHR'):
70 nm = NotificationModel() 82 nm = NotificationModel()
71 # mark all read 83 # mark all read
72 nm.mark_all_read_for_user(self.rhodecode_user.user_id) 84 nm.mark_all_read_for_user(self.rhodecode_user.user_id,
73 Session.commit() 85 filter_=request.GET.getall('type'))
86 Session().commit()
74 c.user = self.rhodecode_user 87 c.user = self.rhodecode_user
75 notif = nm.get_for_user(self.rhodecode_user.user_id) 88 notif = nm.get_for_user(self.rhodecode_user.user_id,
89 filter_=request.GET.getall('type'))
76 c.notifications = Page(notif, page=1, items_per_page=10) 90 c.notifications = Page(notif, page=1, items_per_page=10)
77 return render('admin/notifications/notifications_data.html') 91 return render('admin/notifications/notifications_data.html')
78 92
79 def create(self): 93 def create(self):
80 """POST /_admin/notifications: Create a new item""" 94 """POST /_admin/notifications: Create a new item"""
90 # <input type="hidden" name="_method" value="PUT" /> 104 # <input type="hidden" name="_method" value="PUT" />
91 # Or using helpers: 105 # Or using helpers:
92 # h.form(url('notification', notification_id=ID), 106 # h.form(url('notification', notification_id=ID),
93 # method='put') 107 # method='put')
94 # url('notification', notification_id=ID) 108 # url('notification', notification_id=ID)
109 try:
110 no = Notification.get(notification_id)
111 owner = lambda: (no.notifications_to_users.user.user_id
112 == c.rhodecode_user.user_id)
113 if h.HasPermissionAny('hg.admin')() or owner:
114 NotificationModel().mark_read(c.rhodecode_user.user_id, no)
115 Session().commit()
116 return 'ok'
117 except Exception:
118 Session.rollback()
119 log.error(traceback.format_exc())
120 return 'fail'
95 121
96 def delete(self, notification_id): 122 def delete(self, notification_id):
97 """DELETE /_admin/notifications/id: Delete an existing item""" 123 """DELETE /_admin/notifications/id: Delete an existing item"""
98 # Forms posted to this method should contain a hidden field: 124 # Forms posted to this method should contain a hidden field:
99 # <input type="hidden" name="_method" value="DELETE" /> 125 # <input type="hidden" name="_method" value="DELETE" />
104 130
105 try: 131 try:
106 no = Notification.get(notification_id) 132 no = Notification.get(notification_id)
107 owner = lambda: (no.notifications_to_users.user.user_id 133 owner = lambda: (no.notifications_to_users.user.user_id
108 == c.rhodecode_user.user_id) 134 == c.rhodecode_user.user_id)
109 if h.HasPermissionAny('hg.admin', 'repository.admin')() or owner: 135 if h.HasPermissionAny('hg.admin')() or owner:
110 NotificationModel().delete(c.rhodecode_user.user_id, no) 136 NotificationModel().delete(c.rhodecode_user.user_id, no)
111 Session.commit() 137 Session().commit()
112 return 'ok' 138 return 'ok'
113 except Exception: 139 except Exception:
114 Session.rollback() 140 Session.rollback()
115 log.error(traceback.format_exc()) 141 log.error(traceback.format_exc())
116 return 'fail' 142 return 'fail'
130 # if this association to user is not valid, we don't want to show 156 # if this association to user is not valid, we don't want to show
131 # this message 157 # this message
132 if unotification: 158 if unotification:
133 if unotification.read is False: 159 if unotification.read is False:
134 unotification.mark_as_read() 160 unotification.mark_as_read()
135 Session.commit() 161 Session().commit()
136 c.notification = no 162 c.notification = no
137 163
138 return render('admin/notifications/show_notification.html') 164 return render('admin/notifications/show_notification.html')
139 165
140 return redirect(url('notifications')) 166 return redirect(url('notifications'))