def get(self, project_id): user_id = g.current_user.id # Gets a document not contributed # :: Looks for not completed documents in projects assigned to user subquery = db.session.query(AnnotationSet.document_id, func.count(AnnotationSet.user_id).label('frequency')) \ .group_by(AnnotationSet.document_id) \ .subquery() document = Document.query \ .filter_by(project_id=project_id) \ .join(Project, Project.id == Document.project_id) \ .outerjoin(subquery, Document.id == subquery.c.document_id) \ .filter(or_(subquery.c.frequency.is_(None), Project.redundancy > subquery.c.frequency)) \ .join(UserRole, UserRole.project_id == Project.id) \ .filter(UserRole.user_id == user_id) \ .outerjoin(AnnotationSet, and_(AnnotationSet.document_id == Document.id, AnnotationSet.user_id == UserRole.user_id), ) \ .filter(and_(or_(AnnotationSet.completed.is_(None), AnnotationSet.completed.is_(False)), or_(AnnotationSet.skipped.is_(None), AnnotationSet.skipped.is_(False)))) \ .first() if document is not None: document_data = DocumentSchema().dump(document) document_data['project'] = ProjectSchema().dump(document.project) document_data['project']['tasks'] = TaskSchema().dump( document.project.tasks, many=True) annotation_set = op.get_annotation_set(user_id, document.id) annotation_set_data = AnnotationSetSchema().dump(annotation_set) return js.success({ 'document': document_data, 'annotation_set': annotation_set_data }) return js.success()
def flag_document(self, data): user_id, document_id = data['user_id'], data['document_id'] annotation_set = op.get_annotation_set(user_id, document_id) op.update_object(annotation_set, {'flagged': not annotation_set.flagged}) data = AnnotationSetSchema().dump(annotation_set) return js.success(data)
def get(self, project_id, document_id): document = get_document(document_id) assert document is not None, {'_root': ['Document not found.']} document_data = DocumentSchema().dump(document) document_data['project'] = ProjectSchema().dump(document.project) document_data['project']['tasks'] = TaskSchema().dump( document.project.tasks, many=True) user_id = g.current_user.id annotation_set = op.get_annotation_set(user_id, document.id) annotation_set_data = AnnotationSetSchema().dump(annotation_set) data = { 'document': document_data, 'annotation_set': annotation_set_data } return js.success(data)
def save_document(self, data): user_id, document_id, annotations = data['user_id'], data[ 'document_id'], data['annotations'] # Delete existing annotations annotation_set = op.get_annotation_set(user_id, document_id) query = Annotation.query.filter_by(annotation_set_id=annotation_set.id) op.delete_object(query, True) # Create new annotations schema = AnnotationSchema() for a in annotations: a['annotation_set_id'] = annotation_set.id data = schema.load(annotations, many=True) annotations = [] for a in data: span = None if 'span' in a: s = a.pop('span') span = AnnotationSpan(**s) a = Annotation(**a) a.span = span annotations.append(a) op.create_object(annotations, many=True) op.update_object(annotation_set, {'completed': True}) return js.success()
def skip_document(self, data): user_id, document_id = data['user_id'], data['document_id'] annotation_set = op.get_annotation_set(user_id, document_id) op.update_object(annotation_set, {'skipped': True}) data = AnnotationSetSchema().dump(annotation_set) return js.success(data)