Mercurial > kallithea
comparison kallithea/controllers/admin/notifications.py @ 6452:3dcf1f82311a
controllers: avoid setting request state in controller instances - set it in the thread global request variable
In TurboGears, controllers are singletons and we should avoid using instance
variables for any volatile data. Instead, use the "global thread local" request
context.
With everything in request, some use of c is dropped.
Note: kallithea/controllers/api/__init__.py still use instance variables that
will cause problems with TurboGears.
author | Mads Kiilerich <mads@kiilerich.com> |
---|---|
date | Sat, 24 Dec 2016 01:27:47 +0100 |
parents | e33b17db388d |
children | e9ac5698281d |
comparison
equal
deleted
inserted
replaced
6451:3fcb60a152f3 | 6452:3dcf1f82311a |
---|---|
56 @NotAnonymous() | 56 @NotAnonymous() |
57 def __before__(self): | 57 def __before__(self): |
58 super(NotificationsController, self).__before__() | 58 super(NotificationsController, self).__before__() |
59 | 59 |
60 def index(self, format='html'): | 60 def index(self, format='html'): |
61 c.user = self.authuser | 61 c.user = request.authuser |
62 notif = NotificationModel().query_for_user(self.authuser.user_id, | 62 notif = NotificationModel().query_for_user(request.authuser.user_id, |
63 filter_=request.GET.getall('type')) | 63 filter_=request.GET.getall('type')) |
64 | 64 |
65 p = safe_int(request.GET.get('page'), 1) | 65 p = safe_int(request.GET.get('page'), 1) |
66 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 | 67 c.pull_request_type = Notification.TYPE_PULL_REQUEST |
79 | 79 |
80 def mark_all_read(self): | 80 def mark_all_read(self): |
81 if request.environ.get('HTTP_X_PARTIAL_XHR'): | 81 if request.environ.get('HTTP_X_PARTIAL_XHR'): |
82 nm = NotificationModel() | 82 nm = NotificationModel() |
83 # mark all read | 83 # mark all read |
84 nm.mark_all_read_for_user(self.authuser.user_id, | 84 nm.mark_all_read_for_user(request.authuser.user_id, |
85 filter_=request.GET.getall('type')) | 85 filter_=request.GET.getall('type')) |
86 Session().commit() | 86 Session().commit() |
87 c.user = self.authuser | 87 c.user = request.authuser |
88 notif = nm.query_for_user(self.authuser.user_id, | 88 notif = nm.query_for_user(request.authuser.user_id, |
89 filter_=request.GET.getall('type')) | 89 filter_=request.GET.getall('type')) |
90 c.notifications = Page(notif, page=1, items_per_page=10) | 90 c.notifications = Page(notif, page=1, items_per_page=10) |
91 return render('admin/notifications/notifications_data.html') | 91 return render('admin/notifications/notifications_data.html') |
92 | 92 |
93 def update(self, notification_id): | 93 def update(self, notification_id): |
94 try: | 94 try: |
95 no = Notification.get(notification_id) | 95 no = Notification.get(notification_id) |
96 owner = all(un.user_id == c.authuser.user_id | 96 owner = all(un.user_id == request.authuser.user_id |
97 for un in no.notifications_to_users) | 97 for un in no.notifications_to_users) |
98 if h.HasPermissionAny('hg.admin')() or owner: | 98 if h.HasPermissionAny('hg.admin')() or owner: |
99 # deletes only notification2user | 99 # deletes only notification2user |
100 NotificationModel().mark_read(c.authuser.user_id, no) | 100 NotificationModel().mark_read(request.authuser.user_id, no) |
101 Session().commit() | 101 Session().commit() |
102 return 'ok' | 102 return 'ok' |
103 except Exception: | 103 except Exception: |
104 Session().rollback() | 104 Session().rollback() |
105 log.error(traceback.format_exc()) | 105 log.error(traceback.format_exc()) |
106 raise HTTPBadRequest() | 106 raise HTTPBadRequest() |
107 | 107 |
108 def delete(self, notification_id): | 108 def delete(self, notification_id): |
109 try: | 109 try: |
110 no = Notification.get(notification_id) | 110 no = Notification.get(notification_id) |
111 owner = any(un.user_id == c.authuser.user_id | 111 owner = any(un.user_id == request.authuser.user_id |
112 for un in no.notifications_to_users) | 112 for un in no.notifications_to_users) |
113 if h.HasPermissionAny('hg.admin')() or owner: | 113 if h.HasPermissionAny('hg.admin')() or owner: |
114 # deletes only notification2user | 114 # deletes only notification2user |
115 NotificationModel().delete(c.authuser.user_id, no) | 115 NotificationModel().delete(request.authuser.user_id, no) |
116 Session().commit() | 116 Session().commit() |
117 return 'ok' | 117 return 'ok' |
118 except Exception: | 118 except Exception: |
119 Session().rollback() | 119 Session().rollback() |
120 log.error(traceback.format_exc()) | 120 log.error(traceback.format_exc()) |
122 | 122 |
123 def show(self, notification_id, format='html'): | 123 def show(self, notification_id, format='html'): |
124 notification = Notification.get_or_404(notification_id) | 124 notification = Notification.get_or_404(notification_id) |
125 | 125 |
126 unotification = NotificationModel() \ | 126 unotification = NotificationModel() \ |
127 .get_user_notification(self.authuser.user_id, notification) | 127 .get_user_notification(request.authuser.user_id, notification) |
128 | 128 |
129 # if this association to user is not valid, we don't want to show | 129 # if this association to user is not valid, we don't want to show |
130 # this message | 130 # this message |
131 if unotification is None: | 131 if unotification is None: |
132 raise HTTPForbidden() | 132 raise HTTPForbidden() |
134 if not unotification.read: | 134 if not unotification.read: |
135 unotification.mark_as_read() | 135 unotification.mark_as_read() |
136 Session().commit() | 136 Session().commit() |
137 | 137 |
138 c.notification = notification | 138 c.notification = notification |
139 c.user = self.authuser | 139 c.user = request.authuser |
140 return render('admin/notifications/show_notification.html') | 140 return render('admin/notifications/show_notification.html') |