示例#1
0
文件: api.py 项目: ysenarath/lynx
 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()
示例#2
0
文件: api.py 项目: ysenarath/lynx
 def get(self):
     check = bool(request.args.get('check', False))
     if check:
         return js.success()
     token = generate_token(g.current_user)
     user_schema = UserSchema()
     current_user = user_schema.dump(g.current_user)
     return js.success({
         'token': token.decode('ascii'),
         'user': current_user
     })
示例#3
0
文件: api.py 项目: ysenarath/lynx
 def post(self, project_id):
     # authenticate
     project = get_project(project_id)
     # Validate
     assert project is not None, {
         'data': {
             '_root': 'Invalid project. Please try with a valid project ID.'
         }
     }
     data = request.get_json()
     data.update({'project_id': project_id})
     labels = utils.iferror(lambda: data['labels'], [])
     schema = TaskSchema()
     data = schema.load(data, unknown=EXCLUDE)
     task = Task(**data)
     task.labels[:] = []
     for label_data in labels:
         label = None
         if 'id' in label_data:
             label_id = label_data['id']
             label = Label.query.get(label_id)
         if label is None:  # create if label not found
             label_schema = LabelSchema()
             label_data = label_schema.load(label_data)
             label = Label(**label_data)
         task.labels.append(label)
     op.create_object(task)
     data = schema.dump(task)
     return js.success(data)
示例#4
0
文件: api.py 项目: ysenarath/lynx
 def delete(self, project_id, document_id):
     # Authenticate and get
     document = get_document(document_id)
     # delete document
     op.delete_object(document)
     # return success
     return js.success()
示例#5
0
文件: api.py 项目: ysenarath/lynx
 def post(self, project_id):
     # Authenticate & get
     project = get_project(project_id)
     # Validate
     assert project is not None, {
         'data': {
             '_root': 'Invalid project. Please try with a valid project ID.'
         }
     }
     payload = request.get_json()
     # get user id
     assert 'username' in payload, {
         'username': ['Required field \'username\' not found.']
     }
     username = payload['username']
     user = User.query.filter_by(username=username).one_or_none()
     assert user is not None, {'_root': ['No such user in the system.']}
     payload.update({'user_id': user.id, 'project_id': project_id})
     # create object
     schema = UserRoleSchema()
     data = schema.load(payload, unknown=EXCLUDE)
     role_assignment = UserRole(**data)
     op.create_object(role_assignment)
     data = schema.dump(role_assignment)
     return js.success(data)
示例#6
0
文件: api.py 项目: ysenarath/lynx
 def delete(self, project_id):
     # get payload
     payload = request.get_json()
     # validate params
     assert 'username' in payload, {
         'username': ['Required field \'username\' not found.']
     }
     # authenticate
     project = get_project(project_id)
     # validate
     assert project is not None, {
         'data': {
             '_root': ['Invalid project. Please enter a valid project ID.']
         }
     }
     user = User.query.filter_by(username=payload['username']).one_or_none()
     assert user is not None, {'_root': ['No such user in the system.']}
     role = UserRole.query.filter_by(project_id=project_id,
                                     user_id=user.id).one_or_none()
     assert role is not None, {
         'data': {
             '_root': ['Role not assigned to provided user.']
         }
     }
     op.delete_object(role)
     return js.success()
示例#7
0
文件: api.py 项目: ysenarath/lynx
 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)
示例#8
0
文件: api.py 项目: ysenarath/lynx
 def put(self, project_id, task_id):
     # authenticate
     task = get_task(task_id)
     # validate input
     data = request.get_json()
     data.update({'project_id': project_id})
     schema = TaskSchema()
     data_labels = data['labels']
     data = schema.load(data, unknown=EXCLUDE)
     # update object
     task.labels[:] = []
     for data_label in data_labels:
         label = None
         if 'id' in data_label:
             label_id = data_label['id']
             label = Label.query.get(label_id)
         if label is None:  # create if label not found
             label_schema = LabelSchema()
             data_label = label_schema.load(data_label)
             label = Label(**data_label)
         else:
             op.update_object(label, data_label, commit=False)
         task.labels.append(label)
     op.update_object(task, data)
     # return status
     data = schema.dump(task)
     return js.success(data)
示例#9
0
文件: api.py 项目: ysenarath/lynx
 def delete(self, user_id):
     # Assert project authorization for authenticated user
     assert g.current_user.id == user_id, {
         '_root': ['You are not authorized to delete another account.']
     }
     user = User.query.get(user_id)
     op.delete_object(user)
     return js.success()
示例#10
0
文件: api.py 项目: ysenarath/lynx
 def put(self, project_id):
     data = request.get_json()
     schema = ProjectSchema()
     schema.load(data, partial=True, unknown=EXCLUDE)
     project = get_project(project_id)
     op.update_object(project, data)
     data = schema.dump(project)
     return js.success(data)
示例#11
0
文件: api.py 项目: ysenarath/lynx
 def put(self, project_id, document_id):
     # get params
     data = request.get_json()
     # Authenticate and get
     document = get_document(document_id)
     # update object
     op.update_object(document, data)
     # return updated
     data = DocumentSchema().dump(document)
     return js.success(data)
示例#12
0
文件: api.py 项目: ysenarath/lynx
 def get(self):
     search = None
     if 'q' in request.args:
         search = request.args.get('q')
     query = User.query
     if search is not None:
         query = query.filter(User.username.contains(search))
     users = query.all()
     data = UserSchema().dump(users, many=True)
     return js.success(data)
示例#13
0
文件: api.py 项目: ysenarath/lynx
 def post(self):
     data = request.get_json()
     # add user details as owner
     data['owner_id'] = g.current_user.id
     schema = ProjectSchema()
     data = schema.load(data, unknown=EXCLUDE)
     project = Project(**data)
     # Create project (do not commit)
     op.create_object(project)
     data = schema.dump(project)
     return js.success(data)
示例#14
0
文件: api.py 项目: ysenarath/lynx
 def get(self, project_id):
     # Assert project authorization for authenticated user
     user_id = g.current_user.id
     tasks = Task.query \
         .join(Project, Project.id == Task.project_id) \
         .join(UserRole, UserRole.project_id == Project.id, isouter=True) \
         .filter(or_(and_(UserRole.user_id == user_id, UserRole.role == 'admin'),
                     Project.owner_id == user_id)) \
         .filter_by(project_id=project_id) \
         .all()
     data = TaskSchema().dump(tasks, many=True)
     return js.success(data)
示例#15
0
文件: api.py 项目: ysenarath/lynx
 def get(self, project_id):
     user_id = g.current_user.id
     try:
         offset = int(request.args.get('offset', 1))
     except ValueError as _:
         offset = 1
     try:
         limit = int(request.args.get('limit', 20))
     except ValueError as _:
         limit = 20
     query = db.session.query(Document, AnnotationSet) \
         .join(AnnotationSet, AnnotationSet.document_id == Document.id, isouter=True) \
         .join(Project, Project.id == Document.project_id) \
         .join(UserRole, UserRole.project_id == Project.id) \
         .filter(and_(or_(UserRole.user_id == user_id, Project.owner_id == user_id),
                      Document.project_id == project_id))
     # Filter by flagged options
     var = request.args.get('flagged', None)
     if var != 'none':
         if isinstance(var, str):
             var = var == 'true'
         else:
             var = bool(var)
         query = query.filter(AnnotationSet.flagged.is_(var))
     # Filter by status options
     status = request.args.get('status', None)
     if status == 'completed':
         query = query.filter(AnnotationSet.completed.is_(True))
     elif status == 'skipped':
         query = query.filter(AnnotationSet.skipped.is_(True))
     elif status == 'active':
         query = query.filter(
             or_(AnnotationSet.id.is_(None),
                 AnnotationSet.completed.is_(False)))
     paginate = utils.paginate(query, limit, offset)
     # update structure
     items = []
     for doc, annotation_set in paginate.items:
         data = DocumentSchema().dump(doc)
         data.update(
             {'annotation_set': AnnotationSetSchema().dump(annotation_set)})
         items.append(data)
     return js.success({
         'pagination': {
             'page': paginate.page,
             'pages': paginate.pages,
             'has_prev': paginate.has_prev,
             'has_next': paginate.has_next,
             'per_page': paginate.per_page,
             'total': paginate.total,
             'items': items,
         },
     })
示例#16
0
文件: api.py 项目: ysenarath/lynx
 def get(self, project_id):
     # Authenticate & get
     project = get_project(project_id)
     # Validate
     assert project is not None, {
         'data': {
             '_root': 'Invalid project. Please try with a valid project ID.'
         }
     }
     # return
     data = UserRoleSchema().dump(project.user_roles, many=True)
     return js.success(data)
示例#17
0
文件: api.py 项目: ysenarath/lynx
 def put(self, user_id):
     # Assert project authorization for authenticated user
     assert g.current_user.id == user_id, {
         '_root': ['You are not authorized to update another account.']
     }
     payload = request.get_json()
     schema = UserSchema()
     data = schema.load(payload, unknown=EXCLUDE)
     if 'password' in data:
         data['password'] = hash_password(data['password'])
     user = User.query.get(user_id)
     op.update_object(user, data)
     data = schema.dump(user)
     return js.success(data)
示例#18
0
文件: api.py 项目: ysenarath/lynx
 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)
示例#19
0
文件: api.py 项目: ysenarath/lynx
 def post(self):
     payload = request.get_json()
     schema = UserSchema()
     data = schema.load(payload, unknown=EXCLUDE)
     if 'password' in data:
         data['password'] = hash_password(data['password'])
     user = User(**data)
     assert User.query.filter_by(username=user.username).count() == 0, {
         '_root': [
             'Username already exists in the system. Please try another username.'
         ]
     }
     op.create_object(user)
     data = schema.dump(user)
     return js.success(data)
示例#20
0
文件: api.py 项目: ysenarath/lynx
 def get(self):
     offset = request.args.get('offset', None)
     limit = request.args.get('limit', None)
     user_id = g.current_user.id
     query = Project.query \
         .join(UserRole, UserRole.project_id == Project.id, isouter=True) \
         .filter(or_(Project.owner_id == user_id, and_(UserRole.user_id == user_id, UserRole.role == 'admin')))
     paginate = utils.paginate(query, limit, offset)
     data = ProjectSchema().dump(paginate.items, many=True)
     return js.success({
         'pagination': {
             'page': paginate.page,
             'pages': paginate.pages,
             'has_prev': paginate.has_prev,
             'has_next': paginate.has_next,
             'per_page': paginate.per_page,
             'total': paginate.total,
             'items': data,
         },
     })
示例#21
0
文件: api.py 项目: ysenarath/lynx
 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()
示例#22
0
文件: api.py 项目: ysenarath/lynx
 def post(self, project_id):
     # Authorize & Validate
     assert get_project(project_id) is not None, {
         'data': {
             '_root': 'Invalid project. Please try with a valid project ID.'
         }
     }
     if 'file' in request.files:
         file = request.files['file']
         payload = self.process_file(file)
     else:
         payload = request.get_json()
     if isinstance(payload, list):
         for d in payload:
             d.update({'project_id': project_id})
         data = DocumentSchema().load(payload, many=True, unknown=EXCLUDE)
         documents = [Document(**d) for d in data]
         op.create_object(documents, many=True)
     else:
         payload.update({'project_id': project_id})
         data = DocumentSchema().load(payload, unknown=EXCLUDE)
         document = Document(**data)
         op.create_object(document)
     return js.success()
示例#23
0
文件: api.py 项目: ysenarath/lynx
 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)
示例#24
0
文件: api.py 项目: ysenarath/lynx
 def get(self, project_id):
     project = get_project(project_id)
     data = ProjectSchema().dump(project)
     return js.success(data)
示例#25
0
文件: api.py 项目: ysenarath/lynx
 def delete(self, project_id):
     project = get_project(project_id)
     op.delete_object(project)
     return js.success()
示例#26
0
文件: api.py 项目: ysenarath/lynx
 def get(self, project_id, task_id):
     # authorize & get
     task = get_task(task_id)
     data = TaskSchema().dump(task)
     return js.success(data)
示例#27
0
文件: api.py 项目: ysenarath/lynx
 def delete(self, project_id, task_id):
     # authenticate
     task = get_task(task_id)
     # delete object
     op.delete_object(task)
     return js.success()