def test_delete_association(self): notification = NotificationModel().create( created_by=self.u1, notification_subject=u'title', notification_body=u'hi there3', recipients=[self.u3, self.u1, self.u2]) Session().commit() unotification = (UserNotification.query().filter( UserNotification.notification == notification).filter( UserNotification.user_id == self.u3).scalar()) assert unotification.user_id == self.u3 NotificationModel().delete(self.u3, notification.notification_id) Session().commit() u3notification = (UserNotification.query().filter( UserNotification.notification == notification).filter( UserNotification.user_id == self.u3).scalar()) assert u3notification is None # notification object is still there assert Notification.query().all() == [notification] # u1 and u2 still have assignments u1notification = (UserNotification.query().filter( UserNotification.notification == notification).filter( UserNotification.user_id == self.u1).scalar()) assert u1notification is not None u2notification = (UserNotification.query().filter( UserNotification.notification == notification).filter( UserNotification.user_id == self.u2).scalar()) assert u2notification is not None
def test_create_notification(self): self.assertEqual([], Notification.query().all()) self.assertEqual([], UserNotification.query().all()) usrs = [self.u1, self.u2] notification = NotificationModel().create(created_by=self.u1, subject=u'subj', body=u'hi there', recipients=usrs) Session().commit() u1 = User.get(self.u1) u2 = User.get(self.u2) u3 = User.get(self.u3) notifications = Notification.query().all() self.assertEqual(len(notifications), 1) self.assertEqual(notifications[0].recipients, [u1, u2]) self.assertEqual(notification.notification_id, notifications[0].notification_id) unotification = UserNotification.query()\ .filter(UserNotification.notification == notification).all() self.assertEqual(len(unotification), len(usrs)) self.assertEqual(set([x.user.user_id for x in unotification]), set(usrs))
def test_create_with_mention(self): self.log_user() rev = '27cd5cce30c96924232dffcd24178a07ffeb5dfc' text = u'@test_regular check CommentOnRevision' params = {'text': text} response = self.app.post(url(controller='changeset', action='comment', repo_name=HG_REPO, revision=rev), params=params) # Test response... self.assertEqual(response.status, '302 Found') response.follow() response = self.app.get( url(controller='changeset', action='index', repo_name=HG_REPO, revision=rev)) # test DB self.assertEqual(ChangesetComment.query().count(), 1) response.mustcontain('''<div class="comments-number">%s ''' '''comment (0 inline)</div>''' % 1) self.assertEqual(Notification.query().count(), 2) users = [x.user.username for x in UserNotification.query().all()] # test_regular get's notification by @mention self.assertEqual(sorted(users), [u'test_admin', u'test_regular'])
def get_user_notification(self, user, notification): user = self.__get_user(user) notification = self.__get_notification(notification) return UserNotification.query()\ .filter(UserNotification.notification == notification)\ .filter(UserNotification.user == user).scalar()
def get_user_notification(self, user, notification): user = self._get_user(user) notification = self.__get_notification(notification) return UserNotification.query()\ .filter(UserNotification.notification == notification)\ .filter(UserNotification.user == user).scalar()
def get_unread_for_user(self, user): user = self._get_user(user) return [ x.notification for x in UserNotification.query().filter( UserNotification.read == false()).filter( UserNotification.user == user).all() ]
def test_notification_counter(self): self._clean_notifications() self.assertEqual([], Notification.query().all()) self.assertEqual([], UserNotification.query().all()) NotificationModel().create(created_by=self.u1, subject=u'title', body=u'hi there_delete', recipients=[self.u3, self.u1]) Session().commit() self.assertEqual(NotificationModel() .get_unread_cnt_for_user(self.u1), 1) self.assertEqual(NotificationModel() .get_unread_cnt_for_user(self.u2), 0) self.assertEqual(NotificationModel() .get_unread_cnt_for_user(self.u3), 1) notification = NotificationModel().create(created_by=self.u1, subject=u'title', body=u'hi there3', recipients=[self.u3, self.u1, self.u2]) Session().commit() self.assertEqual(NotificationModel() .get_unread_cnt_for_user(self.u1), 2) self.assertEqual(NotificationModel() .get_unread_cnt_for_user(self.u2), 1) self.assertEqual(NotificationModel() .get_unread_cnt_for_user(self.u3), 2)
def _clean_notifications(self, request, pylonsapp): for n in Notification.query().all(): Session().delete(n) Session().commit() assert [] == Notification.query().all() assert [] == UserNotification.query().all()
def test_create_with_mention(self): self.log_user() rev = '27cd5cce30c96924232dffcd24178a07ffeb5dfc' text = u'@test_regular check CommentOnRevision' params = {'text':text} response = self.app.post(url(controller='changeset', action='comment', repo_name=HG_REPO, revision=rev), params=params) # Test response... self.assertEqual(response.status, '302 Found') response.follow() response = self.app.get(url(controller='changeset', action='index', repo_name=HG_REPO, revision=rev)) # test DB self.assertEqual(ChangesetComment.query().count(), 1) response.mustcontain('''<div class="comments-number">%s ''' '''comment (0 inline)</div>''' % 1) self.assertEqual(Notification.query().count(), 2) users = [x.user.username for x in UserNotification.query().all()] # test_regular get's notification by @mention self.assertEqual(sorted(users), [u'test_admin', u'test_regular'])
def test_notification_counter(self): self._clean_notifications() self.assertEqual([], Notification.query().all()) self.assertEqual([], UserNotification.query().all()) NotificationModel().create(created_by=self.u1, subject=u'title', body=u'hi there_delete', recipients=[self.u3, self.u1]) Session.commit() self.assertEqual(NotificationModel().get_unread_cnt_for_user(self.u1), 1) self.assertEqual(NotificationModel().get_unread_cnt_for_user(self.u2), 0) self.assertEqual(NotificationModel().get_unread_cnt_for_user(self.u3), 1) notification = NotificationModel().create( created_by=self.u1, subject=u'title', body=u'hi there3', recipients=[self.u3, self.u1, self.u2]) Session.commit() self.assertEqual(NotificationModel().get_unread_cnt_for_user(self.u1), 2) self.assertEqual(NotificationModel().get_unread_cnt_for_user(self.u2), 1) self.assertEqual(NotificationModel().get_unread_cnt_for_user(self.u3), 2)
def test_delete_association(self): self.assertEqual([], Notification.query().all()) self.assertEqual([], UserNotification.query().all()) notification = NotificationModel().create( created_by=self.u1, subject=u'title', body=u'hi there3', recipients=[self.u3, self.u1, self.u2]) Session.commit() unotification = UserNotification.query()\ .filter(UserNotification.notification == notification)\ .filter(UserNotification.user_id == self.u3)\ .scalar() self.assertEqual(unotification.user_id, self.u3) NotificationModel().delete(self.u3, notification.notification_id) Session.commit() u3notification = UserNotification.query()\ .filter(UserNotification.notification == notification)\ .filter(UserNotification.user_id == self.u3)\ .scalar() self.assertEqual(u3notification, None) # notification object is still there self.assertEqual(Notification.query().all(), [notification]) #u1 and u2 still have assignments u1notification = UserNotification.query()\ .filter(UserNotification.notification == notification)\ .filter(UserNotification.user_id == self.u1)\ .scalar() self.assertNotEqual(u1notification, None) u2notification = UserNotification.query()\ .filter(UserNotification.notification == notification)\ .filter(UserNotification.user_id == self.u2)\ .scalar() self.assertNotEqual(u2notification, None)
def test_delete_association(self): self.assertEqual([], Notification.query().all()) self.assertEqual([], UserNotification.query().all()) notification = NotificationModel().create(created_by=self.u1, subject=u'title', body=u'hi there3', recipients=[self.u3, self.u1, self.u2]) Session().commit() unotification = UserNotification.query()\ .filter(UserNotification.notification == notification)\ .filter(UserNotification.user_id == self.u3)\ .scalar() self.assertEqual(unotification.user_id, self.u3) NotificationModel().delete(self.u3, notification.notification_id) Session().commit() u3notification = UserNotification.query()\ .filter(UserNotification.notification == notification)\ .filter(UserNotification.user_id == self.u3)\ .scalar() self.assertEqual(u3notification, None) # notification object is still there self.assertEqual(Notification.query().all(), [notification]) #u1 and u2 still have assignments u1notification = UserNotification.query()\ .filter(UserNotification.notification == notification)\ .filter(UserNotification.user_id == self.u1)\ .scalar() self.assertNotEqual(u1notification, None) u2notification = UserNotification.query()\ .filter(UserNotification.notification == notification)\ .filter(UserNotification.user_id == self.u2)\ .scalar() self.assertNotEqual(u2notification, None)
def test_delete_notifications(self): self.assertEqual([], Notification.query().all()) self.assertEqual([], UserNotification.query().all()) notification = NotificationModel().create(created_by=self.u1, subject=u'title', body=u'hi there3', recipients=[self.u3, self.u1, self.u2]) Session().commit() notifications = Notification.query().all() self.assertTrue(notification in notifications) Notification.delete(notification.notification_id) Session().commit() notifications = Notification.query().all() self.assertFalse(notification in notifications) un = UserNotification.query().filter(UserNotification.notification == notification).all() self.assertEqual(un, [])
def test_delete_notifications(self): self.assertEqual([], Notification.query().all()) self.assertEqual([], UserNotification.query().all()) notification = NotificationModel().create( created_by=self.u1, subject=u'title', body=u'hi there3', recipients=[self.u3, self.u1, self.u2]) Session.commit() notifications = Notification.query().all() self.assertTrue(notification in notifications) Notification.delete(notification.notification_id) Session.commit() notifications = Notification.query().all() self.assertFalse(notification in notifications) un = UserNotification.query().filter( UserNotification.notification == notification).all() self.assertEqual(un, [])
def delete(self, user, notification): # we don't want to remove actual notification just the assignment try: notification = self.__get_notification(notification) user = self._get_user(user) if notification and user: obj = UserNotification.query()\ .filter(UserNotification.user == user)\ .filter(UserNotification.notification == notification)\ .one() Session().delete(obj) return True except Exception: log.error(traceback.format_exc()) raise
def mark_all_read_for_user(self, user, filter_=None): user = self._get_user(user) q = UserNotification.query()\ .filter(UserNotification.user == user)\ .filter(UserNotification.read == False)\ .join((Notification, UserNotification.notification_id == Notification.notification_id)) if filter_: q = q.filter(Notification.type_.in_(filter_)) # this is a little inefficient but sqlalchemy doesn't support # update on joined tables :( for obj in q.all(): obj.read = True self.sa.add(obj)
def mark_read(self, user, notification): try: notification = self.__get_notification(notification) user = self._get_user(user) if notification and user: obj = UserNotification.query()\ .filter(UserNotification.user == user)\ .filter(UserNotification.notification == notification)\ .one() obj.read = True Session().add(obj) return True except Exception: log.error(traceback.format_exc()) raise
def mark_all_read_for_user(self, user, filter_=None): user = self._get_user(user) q = UserNotification.query()\ .filter(UserNotification.user == user)\ .filter(UserNotification.read == False)\ .join((Notification, UserNotification.notification_id == Notification.notification_id)) if filter_: q = q.filter(Notification.type_.in_(filter_)) # this is a little inefficient but sqlalchemy doesn't support # update on joined tables :( for obj in q.all(): obj.read = True Session().add(obj)
def mark_read(self, user, notification): try: notification = self.__get_notification(notification) user = self._get_user(user) if notification and user: obj = UserNotification.query()\ .filter(UserNotification.user == user)\ .filter(UserNotification.notification == notification)\ .one() obj.read = True self.sa.add(obj) return True except Exception: log.error(traceback.format_exc()) raise
def test_user_notifications(self): self.assertEqual([], Notification.query().all()) self.assertEqual([], UserNotification.query().all()) notification1 = NotificationModel().create(created_by=self.u1, subject=u'subj', body=u'hi there1', recipients=[self.u3]) Session().commit() notification2 = NotificationModel().create(created_by=self.u1, subject=u'subj', body=u'hi there2', recipients=[self.u3]) Session().commit() u3 = Session().query(User).get(self.u3) self.assertEqual(sorted([x.notification for x in u3.notifications]), sorted([notification2, notification1]))
def delete(self, user, notification): # we don't want to remove actual notification just the assignment try: notification = self.__get_notification(notification) user = self.__get_user(user) if notification and user: obj = UserNotification.query()\ .filter(UserNotification.user == user)\ .filter(UserNotification.notification == notification)\ .one() self.sa.delete(obj) return True except Exception: log.error(traceback.format_exc()) raise
def get_for_user(self, user, filter_=None): """ Get mentions for given user, filter them if filter dict is given :param user: :param filter: """ user = self._get_user(user) q = UserNotification.query()\ .filter(UserNotification.user == user)\ .join((Notification, UserNotification.notification_id == Notification.notification_id)) if filter_: q = q.filter(Notification.type_.in_(filter_)) return q.all()
def test_user_notifications(self): self.assertEqual([], Notification.query().all()) self.assertEqual([], UserNotification.query().all()) notification1 = NotificationModel().create(created_by=self.u1, subject=u'subj', body=u'hi there1', recipients=[self.u3]) Session.commit() notification2 = NotificationModel().create(created_by=self.u1, subject=u'subj', body=u'hi there2', recipients=[self.u3]) Session.commit() u3 = Session.query(User).get(self.u3) self.assertEqual(sorted([x.notification for x in u3.notifications]), sorted([notification2, notification1]))
def test_delete_notifications(self): notification = NotificationModel().create( created_by=self.u1, notification_subject=u'title', notification_body=u'hi there3', recipients=[self.u3, self.u1, self.u2]) Session().commit() notifications = Notification.query().all() assert notification in notifications Notification.delete(notification.notification_id) Session().commit() notifications = Notification.query().all() assert notification not in notifications un = UserNotification.query().filter( UserNotification.notification == notification).all() assert un == []
def test_create_notification(self): usrs = [self.u1, self.u2] notification = NotificationModel().create( created_by=self.u1, notification_subject=u'subj', notification_body=u'hi there', recipients=usrs) Session().commit() u1 = User.get(self.u1) u2 = User.get(self.u2) notifications = Notification.query().all() assert len(notifications) == 1 assert notifications[0].recipients, [u1 == u2] assert notification.notification_id == notifications[0].notification_id unotification = UserNotification.query()\ .filter(UserNotification.notification == notification).all() assert len(unotification) == len(usrs) assert set([x.user.user_id for x in unotification]) == set(usrs)
def test_create_with_mention(self): self.log_user() rev = "27cd5cce30c96924232dffcd24178a07ffeb5dfc" text = u"@test_regular check CommentOnRevision" params = {"text": text} response = self.app.post( url(controller="changeset", action="comment", repo_name=HG_REPO, revision=rev), params=params ) # Test response... self.assertEqual(response.status, "302 Found") response.follow() response = self.app.get(url(controller="changeset", action="index", repo_name=HG_REPO, revision=rev)) # test DB self.assertEqual(ChangesetComment.query().count(), 1) self.assertTrue("""<div class="comments-number">%s """ """comment(s) (0 inline)</div>""" % 1 in response.body) self.assertEqual(Notification.query().count(), 2) users = [x.user.username for x in UserNotification.query().all()] # test_regular get's notification by @mention self.assertEqual(sorted(users), [u"test_admin", u"test_regular"])
def mark_all_read_for_user(self, user): user = self.__get_user(user) UserNotification.query()\ .filter(UserNotification.read==False)\ .update({'read': True})
def get_unread_cnt_for_user(self, user): user = self.__get_user(user) return UserNotification.query()\ .filter(UserNotification.read == False)\ .filter(UserNotification.user == user).count()
def get_unread_for_user(self, user): user = self.__get_user(user) return [x.notification for x in UserNotification.query()\ .filter(UserNotification.read == False)\ .filter(UserNotification.user == user).all()]
def get_unread_cnt_for_user(self, user): user = self._get_user(user) return UserNotification.query()\ .filter(UserNotification.read == false())\ .filter(UserNotification.user == user).count()