Esempio n. 1
0
def reset_mentions(comment):
    task = tasks_service.get_task(comment["object_id"])
    mentions = get_comment_mentions(task["id"], comment["text"])
    comment_to_update = Comment.get(comment["id"])
    comment_to_update.mentions = mentions
    comment_to_update.save()
    comment_dict = comment_to_update.serialize()
    comment_dict["mentions"] = [str(mention.id) for mention in mentions]
    return comment_dict
Esempio n. 2
0
def reset_mentions(comment):
    task = get_task(comment["object_id"])
    mentions = get_comment_mentions(
        task["id"],
        comment["text"]
    )
    comment_to_update = Comment.get(comment["id"])
    comment_to_update.mentions = mentions
    comment_to_update.save()
    return comment_to_update.serialize()
Esempio n. 3
0
def get_comment_raw(comment_id):
    """
    Return comment matching give id as an active record.
    """
    try:
        comment = Comment.get(comment_id)
    except StatementError:
        raise CommentNotFoundException()

    if comment is None:
        raise CommentNotFoundException()
    return comment
Esempio n. 4
0
def remove_comment(comment_id):
    """
    Remove a comment from database and everything related (notifs, news, and
    preview files)
    """
    comment = Comment.get(comment_id)
    if comment is not None:
        task = Task.get(comment.object_id)
        notifications = Notification.query.filter_by(comment_id=comment.id)
        for notification in notifications:
            notification.delete()

        news_list = News.query.filter_by(comment_id=comment.id)
        for news in news_list:
            news.delete()

        if comment.preview_file_id is not None:
            preview_file = PreviewFile.get(comment.preview_file_id)
            comment.preview_file_id = None
            comment.save()
            remove_preview_file(preview_file)

        previews = [preview for preview in comment.previews]
        comment.delete()

        for preview in previews:
            remove_preview_file(preview)

        if task is not None:
            events.emit(
                "comment:delete",
                {"comment_id": comment.id},
                project_id=str(task.project_id),
            )
            return comment.serialize()
    else:
        raise CommentNotFoundException
Esempio n. 5
0
def remove_comment(comment_id):
    comment = Comment.get(comment_id)
    if comment is not None:
        notifications = Notification.query.filter_by(comment_id=comment.id)
        for notification in notifications:
            notification.delete()

        if comment.preview_file_id is not None:
            preview_file = PreviewFile.get(comment.preview_file_id)
            comment.preview_file_id = None
            comment.save()
            remove_preview_file(preview_file)

        previews = [preview for preview in comment.previews]
        comment.delete()

        for preview in previews:
            remove_preview_file(preview)

        reset_task_data(comment.object_id)
        events.emit("comment:delete", {"comment_id": comment.id})
        return comment.serialize()
    else:
        raise CommentNotFoundException
Esempio n. 6
0
 def test_repr(self):
     self.assertEqual(str(Comment.get(self.comments[0]["id"])),
                      "<Comment of %s>" % self.comments[0]["object_id"])
Esempio n. 7
0
def get_last_notifications(notification_id=None, after=None, before=None):
    """
    Return last 100 user notifications.
    """
    current_user = persons_service.get_current_user()
    Author = aliased(Person, name="author")
    is_current_user_artist = current_user["role"] == "user"
    result = []
    query = (
        Notification.query.filter_by(person_id=current_user["id"])
        .order_by(Notification.created_at.desc())
        .join(Author, Author.id == Notification.author_id)
        .join(Task, Task.id == Notification.task_id)
        .join(Project, Project.id == Task.project_id)
        .outerjoin(Comment, Comment.id == Notification.comment_id)
        .add_columns(
            Project.id,
            Project.name,
            Task.task_type_id,
            Comment.id,
            Comment.task_status_id,
            Comment.text,
            Comment.replies,
            Task.entity_id,
            Author.role,
        )
    )

    if notification_id is not None:
        query = query.filter(Notification.id == notification_id)

    if after is not None:
        query = query.filter(Notification.created_at > after)

    if before is not None:
        query = query.filter(Notification.created_at < before)

    notifications = query.limit(100).all()

    for (
        notification,
        project_id,
        project_name,
        task_type_id,
        comment_id,
        task_status_id,
        comment_text,
        comment_replies,
        task_entity_id,
        role,
    ) in notifications:
        (full_entity_name, episode_id) = names_service.get_full_entity_name(
            task_entity_id
        )
        preview_file_id = None
        mentions = []
        if comment_id is not None:
            comment = Comment.get(comment_id)
            if len(comment.previews) > 0:
                preview_file_id = comment.previews[0].id
            mentions = comment.mentions or []

        reply_text = ""
        if notification.type == "reply":
            reply = next(
                (
                    reply
                    for reply in comment_replies
                    if reply["id"] == str(notification.reply_id)
                ),
                None,
            )
            if reply is not None:
                reply_text = reply["text"]

        if role == "client" and is_current_user_artist:
            comment_text = ""
            reply_text = ""

        result.append(
            fields.serialize_dict(
                {
                    "id": notification.id,
                    "type": "Notification",
                    "notification_type": notification.type,
                    "author_id": notification.author_id,
                    "comment_id": notification.comment_id,
                    "task_id": notification.task_id,
                    "task_type_id": task_type_id,
                    "task_status_id": task_status_id,
                    "mentions": mentions,
                    "preview_file_id": preview_file_id,
                    "project_id": project_id,
                    "project_name": project_name,
                    "comment_text": comment_text,
                    "reply_text": reply_text,
                    "created_at": notification.created_at,
                    "read": notification.read,
                    "change": notification.change,
                    "full_entity_name": full_entity_name,
                    "episode_id": episode_id,
                }
            )
        )

    return result
Esempio n. 8
0
def get_last_notifications(notification_id=None):
    """
    Return last 100 user notifications.
    """
    current_user = persons_service.get_current_user_raw()
    result = []
    query = (Notification.query.filter_by(person_id=current_user.id).order_by(
        Notification.created_at.desc()).join(
            Task, Project).outerjoin(Comment).add_columns(
                Project.id,
                Project.name,
                Task.task_type_id,
                Comment.id,
                Comment.task_status_id,
                Comment.text,
                Task.entity_id,
            ))

    if notification_id is not None:
        query = query.filter(Notification.id == notification_id)

    notifications = query.limit(100).all()

    for (
            notification,
            project_id,
            project_name,
            task_type_id,
            comment_id,
            task_status_id,
            comment_text,
            task_entity_id,
    ) in notifications:
        (full_entity_name,
         episode_id) = names_service.get_full_entity_name(task_entity_id)
        preview_file_id = None
        mentions = []
        if comment_id is not None:
            comment = Comment.get(comment_id)
            if len(comment.previews) > 0:
                preview_file_id = comment.previews[0].id
            mentions = comment.mentions or []

        result.append(
            fields.serialize_dict({
                "id": notification.id,
                "type": "Notification",
                "notification_type": notification.type,
                "author_id": notification.author_id,
                "comment_id": notification.comment_id,
                "task_id": notification.task_id,
                "task_type_id": task_type_id,
                "task_status_id": task_status_id,
                "mentions": mentions,
                "preview_file_id": preview_file_id,
                "project_id": project_id,
                "project_name": project_name,
                "comment_text": comment_text,
                "created_at": notification.created_at,
                "read": notification.read,
                "change": notification.change,
                "full_entity_name": full_entity_name,
                "episode_id": episode_id,
            }))

    return result