Esempio n. 1
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()

        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)

        reset_task_data(comment.object_id)
        events.emit("comment:delete", {"comment_id": comment.id})
        return comment.serialize()
    else:
        raise CommentNotFoundException
Esempio n. 2
0
def create_comment(object_id,
                   task_status_id,
                   person_id,
                   text,
                   object_type="Task",
                   files={},
                   checklist=[]):
    """
    Create a new comment for given object (by default, it considers this object
    as a Task).
    """
    comment = Comment.create(
        object_id=object_id,
        object_type=object_type,
        task_status_id=task_status_id,
        person_id=person_id,
        mentions=get_comment_mentions(object_id, text),
        checklist=checklist,
        text=text,
    )
    comment = comment.serialize(relations=True)
    if "file" in files:
        uploaded_file = files["file"]
        attachment_file = create_attachment(comment, uploaded_file)
        comment["attachment_files"] = [attachment_file]

    events.emit("comment:new", {"comment_id": comment["id"]})
    return comment
Esempio n. 3
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(relations=True)
Esempio n. 4
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. 5
0
def get_comment(comment_id):
    try:
        comment = Comment.get(comment_id)
    except StatementError:
        raise CommentNotFoundException()

    if comment is None:
        raise CommentNotFoundException()
    return comment
Esempio n. 6
0
def get_comment_by_preview_file_id(preview_file_id):
    """
    Return comment related to given preview file as a dict.
    """
    comment = Comment.get_by(preview_file_id=preview_file_id)
    if comment is not None:
        return comment.serialize()
    else:
        return None
Esempio n. 7
0
def create_comment(object_id,
                   task_status_id,
                   person_id,
                   text,
                   object_type="Task"):
    comment = Comment.create(object_id=object_id,
                             object_type=object_type,
                             task_status_id=task_status_id,
                             person_id=person_id,
                             text=text)
    return comment.serialize()
Esempio n. 8
0
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()
Esempio n. 9
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. 10
0
    def import_entry(self, data):
        comment = Comment.get_by(shotgun_id=data["shotgun_id"])
        if comment is None:
            comment = Comment(**data)
            comment.save()
            current_app.logger.info("Comment created: %s" % comment)

        else:
            comment.update(data)
            current_app.logger.info("Comment updated: %s" % comment)
        return comment
Esempio n. 11
0
def new_comment(
    task_id,
    task_status_id,
    person_id,
    text,
    object_type="Task",
    files={},
    checklist=[],
    created_at="",
):
    """
    Create a new comment for given object (by default, it considers this object
    as a Task).
    """
    created_at_date = None
    task = tasks_service.get_task(task_id)
    if created_at is not None and len(created_at) > 0:
        try:
            created_at_date = fields.get_date_object(
                created_at, date_format="%Y-%m-%d %H:%M:%S")
        except ValueError:
            pass

    comment = Comment.create(
        object_id=task_id,
        object_type=object_type,
        task_status_id=task_status_id,
        person_id=person_id,
        mentions=get_comment_mentions(task_id, text),
        checklist=checklist,
        text=text,
        created_at=created_at_date,
    )

    comment = comment.serialize(relations=True)
    comment["attachment_files"] = []
    for uploaded_file in files.values():
        attachment_file = create_attachment(comment, uploaded_file)
        comment["attachment_files"].append(attachment_file)

    events.emit(
        "comment:new",
        {
            "comment_id": comment["id"],
            "task_id": task_id
        },
        project_id=task["project_id"],
    )
    return comment
Esempio n. 12
0
def create_comment(
    object_id, task_status_id, person_id, text, object_type="Task"
):
    """
    Create a new comment for given object (by default, it considers this object
    as a Task).
    """
    comment = Comment.create(
        object_id=object_id,
        object_type=object_type,
        task_status_id=task_status_id,
        person_id=person_id,
        mentions=get_comment_mentions(object_id, text),
        text=text,
    )
    events.emit("comment:new", {"comment_id": comment.id})
    return comment.serialize(relations=True)
Esempio n. 13
0
def create_comment(
    object_id,
    task_status_id,
    person_id,
    text,
    object_type="Task"
):
    """
    Create a new comment for given object (by default, it considers this object
    as a Task).
    """
    comment = Comment.create(
        object_id=object_id,
        object_type=object_type,
        task_status_id=task_status_id,
        person_id=person_id,
        text=text
    )
    return comment.serialize()
Esempio n. 14
0
def get_notification_recipients(task):
    """
    Get the list of notification recipients for given task: assignees and
    every people who commented the task.
    """
    recipients = set()
    comments = Comment.get_all_by(object_id=task["id"])
    task_subscriptions = get_task_subscriptions(task)
    sequence_subscriptions = get_sequence_subscriptions(task)

    for assignee_id in task["assignees"]:
        recipients.add(assignee_id)

    for subscription in task_subscriptions:
        recipients.add(str(subscription.person_id))

    for subscription in sequence_subscriptions:
        recipients.add(str(subscription.person_id))

    return recipients
Esempio n. 15
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. 16
0
 def test_repr(self):
     self.assertEqual(str(Comment.get(self.comments[0]["id"])),
                      "<Comment of %s>" % self.comments[0]["object_id"])
Esempio n. 17
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. 18
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