def get_todos(): """ Get all unfinished tasks assigned to current user. """ current_user = persons_service.get_current_user_raw() projects = related_projects() return tasks_service.get_person_tasks(current_user.id, projects)
def build_team_filter(): """ Query filter for task to retrieve only models from project for which the user is part of the team. """ current_user = persons_service.get_current_user_raw() return Project.team.contains(current_user)
def get_done_tasks(): """ Get all finished tasks assigned to current user for open projects. """ current_user = persons_service.get_current_user_raw() projects = related_projects() return tasks_service.get_person_done_tasks(current_user.id, projects)
def get_filters(): """ Retrieve search filters used by current user. It groups them by list type and project_id. If the filter is not related to a project, the project_id is all. """ result = {} current_user = persons_service.get_current_user_raw() filters = (SearchFilter.query.join(Project, ProjectStatus).filter( SearchFilter.person_id == current_user.id).filter( build_open_project_filter()).all()) filters = (filters + SearchFilter.query.filter( SearchFilter.person_id == current_user.id).filter( SearchFilter.project_id == None).all()) for search_filter in filters: if search_filter.list_type not in result: result[search_filter.list_type] = {} subresult = result[search_filter.list_type] if search_filter.project_id is None: project_id = "all" else: project_id = str(search_filter.project_id) if project_id not in subresult: subresult[project_id] = [] subresult[project_id].append(search_filter.serialize()) return result
def get_filters(): """ Retrieve search filters used by current user. It groups them by list type and project_id. If the filter is not related to a project, the project_id is all. """ current_user = persons_service.get_current_user_raw() return get_user_filters(str(current_user.id))
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) \ .join(Task, Project, Comment) \ .add_columns( Project.id, Project.name, Task.task_type_id, Comment.preview_file_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, preview_file_id, task_status_id, comment_text, task_entity_id ) in notifications: full_entity_name = notifications_service.get_full_entity_name( task_entity_id ) result.append(fields.serialize_dict({ "id": notification.id, "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, "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 })) return result
def remove_filter(search_filter_id): """ Remove given filter from database. """ current_user = persons_service.get_current_user_raw() search_filter = SearchFilter.get_by(id=search_filter_id, person_id=current_user.id) if search_filter is None: raise SearchFilterNotFoundException search_filter.delete() return search_filter.serialize()
def check_working_on_entity(entity_id): """ Return True if user has task assigned which is related to given entity. """ current_user = persons_service.get_current_user_raw() query = Task.query.filter(Task.assignees.contains(current_user)).filter( Task.entity_id == entity_id) if query.first() is None: raise permissions.PermissionDenied return True
def create_filter(list_type, name, query, project_id=None): """ Add a new search filter to the database. """ current_user = persons_service.get_current_user_raw() search_filter = SearchFilter.create(list_type=list_type, name=name, search_query=query, project_id=project_id, person_id=current_user.id) search_filter.serialize() return search_filter.serialize()
def check_working_on_task(task_id): """ Return True if user has task assigned. """ current_user = persons_service.get_current_user_raw() query = Task.query.filter( Task.assignees.contains(current_user)).filter(Task.id == task_id) if query.first() is None: raise permissions.PermissionDenied return True
def save_event(event, data): """ Store event information in the database. """ try: from zou.app.services.persons_service import get_current_user_raw person = get_current_user_raw() person_id = person.id except: person_id = None return ApiEvent.create(name=event, data=data, user_id=person_id)
def mark_notifications_as_read(): """ Mark all recent notifications for current_user as read. It is useful to mark a list of notifications as read after an user retrieved them. """ current_user = persons_service.get_current_user_raw() notifications = (Notification.query.filter_by( person_id=current_user.id, read=False).order_by(Notification.created_at).limit(100).all()) for notification in notifications: notification.update({"read": True}) return fields.serialize_list(notifications)
def acknowledge_comment(comment_id): """ Add current user to the list of people who acknowledged given comment. If he's already present, remove it. """ comment = tasks_service.get_comment_raw(comment_id) task = tasks_service.get_task(str(comment.object_id)) project_id = task["project_id"] current_user = persons_service.get_current_user_raw() current_user_id = str(current_user.id) acknowledgements = fields.serialize_orm_arrays(comment.acknowledgements) is_already_ack = current_user_id in acknowledgements if is_already_ack: _unack_comment(project_id, comment, current_user) else: _ack_comment(project_id, comment, current_user) comment.save() return comment.serialize(relations=True)
def build_assignee_filter(): """ Query filter for task to retrieve only tasks assigned to current user. """ current_user = persons_service.get_current_user_raw() return Task.assignees.contains(current_user)
def get_done_tasks(): current_user = persons_service.get_current_user_raw() projects = related_projects() return tasks_service.get_person_done_tasks(current_user.id, projects)
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
def assignee_filter(): current_user = persons_service.get_current_user_raw() return Task.assignees.contains(current_user)