def remove_old_notifications(days_old=90): """ Remove notifications older than *days_old*. """ limit_date = datetime.datetime.now() - datetime.timedelta(days=90) Notification.query.filter(Notification.created_at < limit_date).delete() Notification.commit()
def reset_notifications_for_mentions(comment): """ For given task and comment, delete all mention notifications related to the comment and recreate notifications for the mentions listed in the comment. """ Notification.delete_all_by(type="mention", comment_id=comment["id"]) notifications = [] task = tasks_service.get_task(comment["object_id"]) author_id = comment["person_id"] for recipient_id in comment["mentions"]: notification = create_notification( recipient_id, comment_id=comment["id"], author_id=author_id, task_id=comment["object_id"], type="mention", ) emails_service.send_mention_notification( recipient_id, author_id, comment, task ) notifications.append(notification) events.emit( "notification:new", {"notification_id": notification["id"], "person_id": recipient_id}, persist=False, ) return notifications
def remove_person(person_id, force=True): person = Person.get(person_id) if force: for comment in Comment.get_all_by(person_id=person_id): remove_comment(comment.id) comments = Comment.query.filter( Comment.acknowledgements.contains(person) ) for comment in comments: comment.acknowledgements = [ member for member in comment.acknowledgements if str(member.id) != person_id ] comment.save() ApiEvent.delete_all_by(user_id=person_id) Notification.delete_all_by(person_id=person_id) SearchFilter.delete_all_by(person_id=person_id) DesktopLoginLog.delete_all_by(person_id=person_id) LoginLog.delete_all_by(person_id=person_id) Subscription.delete_all_by(person_id=person_id) TimeSpent.delete_all_by(person_id=person_id) for project in Project.query.filter(Project.team.contains(person)): project.team = [ member for member in project.team if str(member.id) != person_id ] project.save() for task in Task.query.filter(Task.assignees.contains(person)): task.assignees = [ assignee for assignee in task.assignees if str(assignee.id) != person_id ] task.save() for task in Task.get_all_by(assigner_id=person_id): task.update({"assigner_id": None}) for output_file in OutputFile.get_all_by(person_id=person_id): output_file.update({"person_id": None}) for working_file in WorkingFile.get_all_by(person_id=person_id): output_file.update({"person_id": None}) for task in WorkingFile.get_all_by(person_id=person_id): output_file.update({"person_id": None}) try: person.delete() events.emit("person:delete", {"person_id": person.id}) except IntegrityError: raise ModelWithRelationsDeletionException( "Some data are still linked to given person." ) return person.serialize_safe()
def generate_fixture_notification(self): self.notification = Notification.create(type="comment", person_id=self.user["id"], author_id=self.person.id, comment_id=self.comment["id"], task_id=self.task.id) return self.notification.serialize()
def test_create_notifications_for_task_and_comment_with_mentions(self): self.generate_fixture_comment() self.comment["mentions"] = [self.person.id] notifications_service.create_notifications_for_task_and_comment( self.task_dict, self.comment) notifications = Notification.get_all() self.assertEqual(len(notifications), 2)
def test_create_notifications_for_task_and_comment(self): self.generate_fixture_comment() notifications_service.create_notifications_for_task_and_comment( self.task_dict, self.comment) notifications = Notification.get_all() self.assertEqual(len(notifications), 1) self.assertEqual(str(notifications[0].author_id), self.user["id"])
def create_notification( person_id, comment_id=None, author_id=None, task_id=None, read=False, change=False, type="comment", created_at=None, ): """ Create a new notification for given person and comment. """ creation_date = fields.get_default_date_object(created_at) notification = Notification.create( read=read, change=change, person_id=person_id, author_id=author_id, comment_id=comment_id, task_id=task_id, type=type, created_at=creation_date, ) return notification.serialize()
def test_create_assignation_notification(self): self.generate_fixture_comment() notifications_service.create_assignation_notification( self.task_dict["id"], self.person.id) notifications = Notification.get_all() self.assertEqual(len(notifications), 1) self.assertEqual(notifications[0].type, "assignation") self.assertEqual(str(notifications[0].author_id), self.task_dict["assigner_id"])
def test_create_notification(self): self.generate_fixture_comment() notification = notifications_service.create_notification( self.person.id, comment_id=self.comment["id"], author_id=self.comment["person_id"], task_id=self.comment["object_id"]) notification_again = Notification.get(notification["id"]) self.assertIsNotNone(notification_again)
def delete_reply(comment_id, reply_id): comment = tasks_service.get_comment_raw(comment_id) task = tasks_service.get_task(comment.object_id) if comment.replies is None: comment.replies = [] comment.replies = [ reply for reply in comment.replies if reply["id"] != reply_id ] comment.save() Notification.delete_all_by(reply_id=reply_id) events.emit( "comment:delete-reply", { "task_id": task["id"], "comment_id": str(comment.id), "reply_id": reply_id, }, project_id=task["project_id"], persist=False, ) return comment.serialize()
def create_notification(person_id, comment_id=None, author_id=None, task_id=None, read=False, change=False, type="comment"): """ Create a new notification for given person and comment. """ notification = Notification.create(read=read, change=change, person_id=person_id, author_id=author_id, comment_id=comment_id, task_id=task_id, type=type) return notification.serialize()
def delete_notifications_for_comment(comment_id): notifications = Notification.get_all_by(comment_id=comment_id) for notification in notifications: notification.delete() return fields.serialize_list(notifications)
def pre_delete(self, instance_dict): Notification.delete_all_by(person_id=instance_dict["id"]) SearchFilter.delete_all_by(person_id=instance_dict["id"]) return instance_dict