def read_all(request): ''' mark all ''' user = request.user if user: Notification.mark_all_as_read(user.name) return request.redirect('/hub/notification') else: return request.redirect("/hub/teams")
def test_add_notification(self): data = {'data': 'some data here'} for i, uid in enumerate(self.uids[:10], start=1): Notification(uid, self.receivers, data).send() assert len(Notification.get_data(self.test_user1)) == i assert Notification.unread_count(self.test_user1) == i assert len(Notification.get_data(self.test_user2)) == i assert Notification.unread_count(self.test_user2) == i
def _q_index(self, request): uid = '/'.join(self.parts) if uid: user = request.user Notification.mark_as_read(user.name, uid) url = request.get_form_var('url') # TODO fix request.query.url return request.redirect(url)
def mark_as_read(request): if request.method == "POST": uid = request.data.get('uid') if uid: Notification.mark_as_read(request.user.name, uid) return {"status": 1} else: raise api_errors.MissingFieldError('uid') raise api_errors.MethodNotAllowedError()
def test_get_data(self): for i, uid in enumerate(self.uids[:10], start=1): data = {'data': 'test' + uid} Notification(uid, self.receivers, data).send() data = Notification.get_data(self.test_user1) assert len(data) == 10 for i, d in enumerate(reversed(data)): assert d.get('uid') == self.uids[i] assert d.get('data') == 'test'+self.uids[i]
def mark(request): ''' mark by uids ''' user = request.user if user: uids = request.get_form_var('uids', []) if isinstance(uids, basestring): uids = [uids] for uid in uids: Notification.mark_as_read(user.name, uid) return dict(r=0) else: return dict(r=1)
def _q_index(request): user = request.user if user: all = request.get_form_var('all') scope = request.get_form_var('scope') unread = not all or all != '1' scope = ActionScope.getScope(scope) or '' # 不带scope则默认所有 actions = Notification.get_data(user.name) # 迁移数据 all_actions = [migrate_notif_data(action, user.name) for action in actions] if scope: actions = [action for action in all_actions if action.get('scope') == scope] else: actions = all_actions if unread: actions = [action for action in actions if not action.get('read')] count_dict = {s: len([a for a in all_actions if a.get('scope') == s and not a.get('read')]) for s in ActionScope.all_scopes} else: count_dict = {s: len([a for a in all_actions if a.get('scope') == s]) for s in ActionScope.all_scopes} count_dict['all'] = sum(count_dict.values()) return st('notifications.html', **locals()) else: return request.redirect("/hub/teams")
def _q_index(self, request): url = '/'.join(self.parts) parts = url.split('.') if len(parts) == 2 and parts[1] == 'gif': uid, ext = parts username = request.get_form_var('user') user = User(username) if username else request.user else: raise TraversalError if user and uid: tokens = uid.split('-') if tokens[0] == 'pullrequest': project_name = '-'.join(tokens[1:-2]) pull_number = tokens[-2] Notification.mark_as_read_by_pull( user.name, project_name, pull_number) else: Notification.mark_as_read(user.name, uid) request.response.set_content_type('image/gif') return EMAIL_HOOK_GIF.decode('base64')
def _q_index(self, request): url = '/'.join(self.parts) parts = url.split('.') if len(parts) == 2 and parts[1] == 'gif': uid, ext = parts username = request.get_form_var('user') user = User(username) if username else request.user else: raise TraversalError if user and uid: tokens = uid.split('-') if tokens[0] == 'pullrequest': project_name = '-'.join(tokens[1:-2]) pull_number = tokens[-2] Notification.mark_as_read_by_pull(user.name, project_name, pull_number) else: Notification.mark_as_read(user.name, uid) request.response.set_content_type('image/gif') return EMAIL_HOOK_GIF.decode('base64')
def _q_index(request): start = request.get_form_var('start', '0') limit = request.get_form_var('count', '20') if start.isdigit() and limit.isdigit(): start = int(start) limit = int(limit) else: start = 0 limit = 20 user = request.user notifications = Notification.get_data(user.name) return notifications[start:(start+limit)]
def _q_index(request): start = request.get_form_var('start', '0') limit = request.get_form_var('count', '20') if start.isdigit() and limit.isdigit(): start = int(start) limit = int(limit) else: start = 0 limit = 20 user = request.user notifications = Notification.get_data(user.name) return notifications[start:(start + limit)]
def test_mark_as_read(self): data = {'data': 'some data here'} for uid in self.uids: Notification(uid, self.receivers, data).send() to_mark_as_read = self.uids[:5] + self.uids[20:30] + self.uids[40:50] for i, uid in enumerate(to_mark_as_read, start=1): Notification.mark_as_read(self.test_user1, uid) assert Notification.unread_count(self.test_user1) == 50-i assert Notification.unread_count(self.test_user2) == 50 Notification.mark_all_as_read(self.test_user2) assert Notification.unread_count(self.test_user2) == 0
def migrate_notif_data(data, receiver): from vilya.models.notification import Notification from vilya.models.actions.commit_comment import migrate_commit_comment from vilya.models.actions.issue import migrate_issue from vilya.models.actions.issue_comment import migrate_issue_comment from vilya.models.actions.pull import migrate_pull_request from vilya.models.actions.pull_comment import migrate_pull_comment from vilya.models.actions.recommend import migrate_recommend from vilya.models.actions.team_add_member import migrate_team_add_member type_migrate_dict = { 'commit_comment': migrate_commit_comment, 'issue': migrate_issue, 'issue_comment': migrate_issue_comment, 'pull_request': migrate_pull_request, 'pull_comment': migrate_pull_comment, # 'pull_commit': migrate_pull_commit, 'recommend': migrate_recommend, 'team_add_member': migrate_team_add_member, } assert data.get('uid') is not None assert data.get('type') is not None uid = data['uid'] action_type = data['type'] if action_type in migrate_type_name: Notification.set_data(receiver, 'type', migrate_type_name[action_type], uid) action_type = migrate_type_name[action_type] migrate_data = type_migrate_dict[action_type] for new, old in migrate_data.iteritems(): if data.get(new) is None: assert type(old) in (str, tuple, list) value = data[old] if isinstance(old, str) else old[1](data.get(old[0])) # noqa Notification.set_data(receiver, new, value, uid) data[new] = value elif type(old) is list: # force update value = data[old] if isinstance(old, str) else old[1](data.get(old[0])) # noqa Notification.set_data(receiver, new, value, uid) data[new] = value data['notif_template'] = action_type + '_notif' data['feed_template'] = action_type + '_feed' return data
def unread_notification_count(self): from vilya.models.notification import Notification return Notification.unread_count(self.name)
def mark_all_as_read(request): if request.method == "POST": Notification.mark_all_as_read(request.user.name) return {"status": 1} else: raise api_errors.MethodNotAllowedError()
def noti(self): return Notification(self._uid, self.noti_receivers, self.noti_data)