Exemplo n.º 1
0
 def get_user_task_with_doc_and_doc_type(nlp_task_id,
                                         current_user: CurrentUser, args):
     q = session.query(UserTask, DocType, Doc) \
         .join(MarkTask, MarkTask.mark_task_id == UserTask.mark_task_id) \
         .join(MarkJob, MarkJob.mark_job_id == MarkTask.mark_job_id) \
         .join(DocType, DocType.doc_type_id == MarkJob.doc_type_id) \
         .join(Doc, Doc.doc_id == MarkTask.doc_id) \
         .filter(
         DocType.nlp_task_id == nlp_task_id,
         ~UserTask.is_deleted,
         ~MarkTask.is_deleted,
         ~Doc.is_deleted
     )
     # TODO
     # 权限
     if current_user.user_role in [
             RoleEnum.manager.value, RoleEnum.guest.value
     ]:
         q = q.filter(DocType.group_id.in_(current_user.user_groups))
     elif current_user.user_role in [RoleEnum.reviewer.value]:
         q = q.filter(
             func.json_contains(MarkJob.reviewer_ids,
                                str(current_user.user_id)))
     elif current_user.user_role in [RoleEnum.annotator.value]:
         # q = q.filter(func.json_contains(MarkJob.annotator_ids, str(current_user.user_id)))
         q = q.filter(UserTask.annotator_id == current_user.user_id)
     if args.get('job_id'):
         q = q.filter(MarkTask.mark_job_id == args['job_id'])
     if args.get('doc_type_id'):
         q = q.filter(MarkJob.doc_type_id == args['doc_type_id'])
     if args['task_state']:
         q = q.filter(MarkTask.mark_task_status ==
                      status_str2int_mapper().get(args['task_state']))
     if args['query']:
         q = q.filter(Doc.doc_raw_name.like(f'%{args["query"]}%'))
     q = q.group_by(UserTask)
     count = q.count()
     processing_count = q.filter(
         MarkTask.mark_task_status == int(StatusEnum.processing)).count()
     if args['order_by'] and isinstance(args['order_by'], str):
         if args['order_by'][1:] == 'task_id':
             args['order_by'] = args['order_by'][0] + 'mark_task_id'
         q = Common().order_by_model_fields(q, UserTask, [args['order_by']])
     items = []
     for user_task, doc_type, doc in q.offset(args['offset']).limit(
             args['limit']).all():
         user_task.doc = doc
         user_task.doc_type = doc_type
         items.append(user_task)
     return count, count - processing_count, items
Exemplo n.º 2
0
 def get_mark_task_with_doc_and_doc_type(self, nlp_task_id,
                                         current_user: CurrentUser, args):
     q = session.query(MarkTask, DocType, Doc) \
         .join(MarkJob, MarkJob.mark_job_id == MarkTask.mark_job_id) \
         .join(DocType, DocType.doc_type_id == MarkJob.doc_type_id) \
         .join(Doc, Doc.doc_id == MarkTask.doc_id) \
         .filter(
         DocType.nlp_task_id == nlp_task_id,
         ~DocType.is_deleted,
         ~MarkTask.is_deleted,
         ~Doc.is_deleted
     )
     # TODO
     # 权限
     if current_user.user_role in [
             RoleEnum.manager.value, RoleEnum.guest.value
     ]:
         q = q.filter(DocType.group_id.in_(current_user.user_groups))
     elif current_user.user_role in [RoleEnum.reviewer.value]:
         q = q.filter(
             func.json_contains(MarkJob.reviewer_ids,
                                str(current_user.user_id)))
     elif current_user.user_role in [RoleEnum.annotator.value]:
         q = q.filter(
             func.json_contains(MarkJob.annotator_ids,
                                str(current_user.user_id)))
     if args.get('job_id'):
         q = q.filter(MarkTask.mark_job_id == args['job_id'])
     if args.get('doc_type_id'):
         q = q.filter(MarkJob.doc_type_id == args['doc_type_id'])
     if args['task_state']:
         q = q.filter(MarkTask.mark_task_status ==
                      status_str2int_mapper().get(args['task_state']))
     if args['query']:
         q = q.filter(Doc.doc_raw_name.like(f'%{args["query"]}%'))
     q = q.group_by(MarkTask)
     count = q.count()
     processing_count = q.filter(
         MarkTask.mark_task_status == int(StatusEnum.processing)).count()
     if args['order_by'] and isinstance(args['order_by'], str):
         if args['order_by'][1:] == 'task_id':
             args['order_by'] = args['order_by'][0] + 'mark_task_id'
         q = Common().order_by_model_fields(q, MarkTask, [args['order_by']])
     items = []
     results = q.offset(args['offset']).limit(args['limit']).all()
     mark_task_ids = [mark_task.mark_task_id for mark_task, _, _ in results]
     user_task_map = self._get_user_task_map(
         mark_task_ids,
         select_keys=(UserTask))  # .annotator_id, UserTask.mark_task_id))
     UserTaskPlaceholder = UserTask(
         annotator_id=0,
         is_deleted=False,
         user_task_status=StatusEnum.labeled.value)
     for mark_task, doc_type, doc in results:
         UserTaskPlaceholder.user_task_result = mark_task.mark_task_result
         user_task_list = user_task_map.get(str(mark_task.mark_task_id),
                                            [UserTaskPlaceholder])
         mark_task.user_task_list = user_task_list
         mark_task.doc = doc
         mark_task.doc_type = doc_type
         items.append(mark_task)
     return count, count - processing_count, items