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 remove_all_notifications(): Notification.query().delete() # Because query().delete() does not (by default) trigger cascades. # http://docs.sqlalchemy.org/en/rel_0_7/orm/collections.html#passive-deletes UserNotification.query().delete() Session().commit()
def remove_all_notifications(self): # query().delete() does not (by default) trigger cascades # ( http://docs.sqlalchemy.org/en/rel_0_7/orm/collections.html#passive-deletes ) # so delete the UserNotification first to ensure referential integrity. UserNotification.query().delete() Notification.query().delete() Session().commit()
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_create_with_mention(self): self.log_user() rev = '27cd5cce30c96924232dffcd24178a07ffeb5dfc' text = u'@test_regular check CommentOnRevision' params = {'text': text, '_authentication_token': self.authentication_token()} 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">''' ''' 1 comment (0 inline, 1 general)''' ) self.assertEqual(Notification.query().count(), 2) users = [x.user.username for x in UserNotification.query().all()] # test_regular gets notification by @mention self.assertEqual(sorted(users), [u'test_admin', u'test_regular'])
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">''' ''' 1 comment (0 inline, 1 general)''' ) self.assertEqual(Notification.query().count(), 2) users = [x.user.username for x in UserNotification.query().all()] # test_regular gets notification by @mention self.assertEqual(sorted(users), [u'test_admin', u'test_regular'])
def get_user_notification(self, user, notification): user = User.guess_instance(user) notification = Notification.guess_instance(notification) return UserNotification.query() \ .filter(UserNotification.notification == notification) \ .filter(UserNotification.user == user).scalar()
def test_create_notification(self): with test_context(self.app): usrs = [self.u1, self.u2] def send_email(recipients, subject, body='', html_body='', headers=None, author=None): assert recipients == ['*****@*****.**'] assert subject == 'Test Message' assert body == u"hi there" assert '>hi there<' in html_body assert author.username == 'u1' with mock.patch.object(kallithea.lib.celerylib.tasks, 'send_email', send_email): 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() 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_id for x in unotification]) == set(usrs)
def setup_method(self, method): Session.remove() u1 = UserModel().create_or_update(username=u'u1', password=u'qweqwe', email=u'*****@*****.**', firstname=u'u1', lastname=u'u1') Session().commit() self.u1 = u1.user_id u2 = UserModel().create_or_update(username=u'u2', password=u'qweqwe', email=u'*****@*****.**', firstname=u'u2', lastname=u'u3') Session().commit() self.u2 = u2.user_id u3 = UserModel().create_or_update(username=u'u3', password=u'qweqwe', email=u'*****@*****.**', firstname=u'u3', lastname=u'u3') Session().commit() self.u3 = u3.user_id self.remove_all_notifications() assert [] == Notification.query().all() assert [] == UserNotification.query().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 test_create_with_mention(self): self.log_user() rev = '27cd5cce30c96924232dffcd24178a07ffeb5dfc' text = u'@%s check CommentOnRevision' % TEST_USER_REGULAR_LOGIN params = {'text': text, '_authentication_token': self.authentication_token()} response = self.app.post(url(controller='changeset', action='comment', repo_name=HG_REPO, revision=rev), params=params, extra_environ={'HTTP_X_PARTIAL_XHR': '1'}) # Test response... self.assertEqual(response.status, '200 OK') 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">''' ''' 1 comment (0 inline, 1 general)''' ) self.assertEqual(Notification.query().count(), 2) users = [x.user.username for x in UserNotification.query().all()] # test_regular gets notification by @mention self.assertEqual(sorted(users), [TEST_USER_ADMIN_LOGIN, TEST_USER_REGULAR_LOGIN])
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 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_association(self): with test_context(self.app): 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() 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 == 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 != None u2notification = UserNotification.query() \ .filter(UserNotification.notification == notification) \ .filter(UserNotification.user_id == self.u2) \ .scalar() assert 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 mark_all_read_for_user(self, user, filter_=None): user = User.guess_instance(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: obj.read = True
def mark_read(self, user, notification): try: notification = Notification.guess_instance(notification) user = User.guess_instance(user) if notification and user: obj = UserNotification.query() \ .filter(UserNotification.user == user) \ .filter(UserNotification.notification == notification) \ .one() obj.read = True return True except Exception: log.error(traceback.format_exc()) raise
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_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 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_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 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): with test_context(self.app): 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() assert notification in notifications Notification.delete(notification.notification_id) Session().commit() notifications = Notification.query().all() assert not notification in notifications un = UserNotification.query().filter(UserNotification.notification == notification).all() assert un == []
def get_for_user(self, user, filter_=None): """ Get notifications 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)) \ .options(joinedload('notification')) \ .options(subqueryload('notification.created_by_user')) \ .order_by(Notification.created_on.desc()) if filter_: q = q.filter(Notification.type_.in_(filter_)) return q.all()
def get_for_user(self, user, filter_=None): """ Get notifications 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))\ .options(joinedload('notification'))\ .options(subqueryload('notification.created_by_user'))\ .order_by(Notification.created_on.desc()) if filter_: q = q.filter(Notification.type_.in_(filter_)) return q.all()
def test_create_with_mention(self): self.log_user() rev = '27cd5cce30c96924232dffcd24178a07ffeb5dfc' text = u'@%s check CommentOnRevision' % TEST_USER_REGULAR_LOGIN params = { 'text': text, '_authentication_token': self.authentication_token() } response = self.app.post(url(controller='changeset', action='comment', repo_name=HG_REPO, revision=rev), params=params, extra_environ={'HTTP_X_PARTIAL_XHR': '1'}) # Test response... assert response.status == '200 OK' response = self.app.get( url(controller='changeset', action='index', repo_name=HG_REPO, revision=rev)) # test DB assert ChangesetComment.query().count() == 1 response.mustcontain('''<div class="comments-number">''' ''' 1 comment (0 inline, 1 general)''') assert Notification.query().count() == 2 users = [x.user.username for x in UserNotification.query().all()] # test_regular gets notification by @mention assert sorted(users) == [ TEST_USER_ADMIN_LOGIN, TEST_USER_REGULAR_LOGIN ]
def get_unread_cnt_for_user(self, user): user = User.guess_instance(user) return UserNotification.query() \ .filter(UserNotification.read == False) \ .filter(UserNotification.user == user).count()
def setUp(self): remove_all_notifications() self.assertEqual([], Notification.query().all()) self.assertEqual([], UserNotification.query().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()
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_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()