Example #1
0
def document_package_random_collaboration(document_package_id):

    except_document_id = request.args.get('exceptDocumentId')

    if request.method == 'GET':

        document_package = DocumentPackageService.get_document_package(
            document_package_id)

        if 'randomAnnotation' in document_package and document_package[
                'randomAnnotation']:
            return RouteUtil.data_to_json_response(
                DocumentService.get_random_undone_collaboration(
                    document_package_id, g.user['email'], except_document_id),
                True)
        else:
            return RouteUtil.data_to_json_response(
                DocumentService.get_collaborations(document_package_id,
                                                   g.user['email'],
                                                   status='UNDONE'), True)

    elif request.method == 'POST':

        state_key = request.args.get('stateKey')

        state_key = DocumentService.update_collaboration(
            document_package_id, request.args.get('documentId'),
            g.user['email'], request.json, state_key)

        return state_key, 200
Example #2
0
def document_package_collaborator(document_package_id):

    email = request.args.get('email')

    if request.method == 'GET':

        if email:
            return RouteUtil.data_to_json_response(
                DocumentPackageService.get_document_packages_by_collaborator(
                    document_package_id, email))
        else:
            return RouteUtil.data_to_json_response(
                DocumentPackageService.get_document_package_collaborators(
                    document_package_id))

    elif request.method == 'POST':

        if email:  # update
            DocumentPackageService.update_document_package_collaborator(
                document_package_id, RouteUtil.safe_json_get('groupId'), email)
        else:
            DocumentPackageService.insert_document_package_collaborator(
                document_package_id, RouteUtil.safe_json_get('groupId'),
                RouteUtil.safe_json_get('email'))

        return 'OK', 200

    elif request.method == 'DELETE':
        DocumentPackageService.remove_document_package_collaborator(
            document_package_id, email)
        return 'OK', 200
Example #3
0
def refresh_token():
    try:
        return RouteUtil.data_to_json_response(
            AuthenticationService.refresh_token(request.headers.get('email'),
                                                request.headers.get('token')))
    except ServiceException:
        return 'INVALID TOKEN', 401
Example #4
0
def document_package(document_package_id):

    append_statistics = request.args.get('appendStatistics')

    if request.method == 'GET':
        return RouteUtil.data_to_json_response(
            DocumentPackageService.get_document_package(
                document_package_id, append_statistics=append_statistics),
            True)

    elif request.method == 'POST':
        DocumentPackageService.update_document_package(
            document_package_id, RouteUtil.safe_json_get('name'),
            RouteUtil.safe_json_get('randomAnnotation'),
            RouteUtil.safe_json_get('showSelfAgreementFeedback'),
            RouteUtil.safe_json_get('selfAgreementFeedbackGoal'),
            RouteUtil.safe_json_get('usePrecheckAgreementThreshold'),
            RouteUtil.safe_json_get('precheckAgreementThreshold'),
            RouteUtil.safe_json_get('useTagAgreement'),
            RouteUtil.safe_json_get('useRelationAgreement'),
            RouteUtil.safe_json_get('useConnectorAgreement'))
        return 'OK', 200

    elif request.method == 'DELETE':
        DocumentPackageService.remove_document_package(document_package_id)
        return 'OK', 200
Example #5
0
def document_root():

    project_id = request.args.get('projectId')
    document_package_id = request.args.get('documentPackageId')
    status = request.args.get('status')
    skip = request.args.get('skip')
    limit = request.args.get('limit')
    append_document_package_detail = RouteUtil.value_to_boolean(
        request.args.get('appendDocumentPackageDetail'))
    append_collaborator_detail = RouteUtil.value_to_boolean(
        request.args.get('appendCollaboratorDetail'))

    if request.method == 'GET':

        return RouteUtil.data_to_json_response(
            DocumentService.get_documents(project_id, document_package_id,
                                          status, skip, limit,
                                          append_document_package_detail,
                                          append_collaborator_detail))

    elif request.method == 'POST':

        return DocumentService.insert_document(
            document_package_id, RouteUtil.safe_json_get('text'),
            RouteUtil.safe_json_get('name'),
            RouteUtil.safe_json_get('isPlainText'))
Example #6
0
def authenticate():
    try:
        return RouteUtil.data_to_json_response(
            AuthenticationService.authenticate(
                RouteUtil.safe_json_get('email'),
                RouteUtil.safe_json_get('password')))
    except ServiceException:
        return 'INVALID USER/PASSWORD', 401
def tokenizer_postagger():

    return RouteUtil.data_to_json_response(
        NlpService.tokenizer_postagger(
            RouteUtil.safe_json_get('text'), RouteUtil.safe_json_get('lang'),
            RouteUtil.safe_json_get('filter'),
            RouteUtil.safe_json_get('smartSentenceSegmentation'),
            RouteUtil.safe_json_get('smartWordSegmentation')))
Example #8
0
def document_package_root():

    ids = request.args.get('ids')
    project_id = request.args.get('projectId')
    collaborator_email = request.args.get('collaboratorEmail')
    append_statistics = request.args.get('appendStatistics')
    skip = request.args.get('skip')
    limit = request.args.get('limit')

    if request.method == 'GET':

        if ids:
            return RouteUtil.data_to_json_response(
                DocumentPackageService.get_document_packages(
                    ids.split(","), append_statistics=append_statistics))

        elif project_id:
            return RouteUtil.data_to_json_response(
                DocumentPackageService.get_document_packages_by_project(
                    project_id, append_statistics=append_statistics))

        elif collaborator_email:
            return RouteUtil.data_to_json_response(
                DocumentPackageService.get_document_packages_by_collaborator(
                    collaborator_email, append_statistics=append_statistics))

        else:
            return RouteUtil.data_to_json_response(
                DocumentPackageService.get_document_packages(
                    skip=skip,
                    limit=limit,
                    append_statistics=append_statistics))

    elif request.method == 'POST':

        return DocumentPackageService.create_document_package(
            RouteUtil.safe_json_get('projectId'),
            RouteUtil.safe_json_get('name'),
            RouteUtil.safe_json_get('randomAnnotation'),
            RouteUtil.safe_json_get('showSelfAgreementFeedback'),
            RouteUtil.safe_json_get('selfAgreementFeedbackGoal'),
            RouteUtil.safe_json_get('usePrecheckAgreementThreshold'),
            RouteUtil.safe_json_get('precheckAgreementThreshold'),
            RouteUtil.safe_json_get('useTagAgreement'),
            RouteUtil.safe_json_get('useRelationAgreement'),
            RouteUtil.safe_json_get('useConnectorAgreement'))
Example #9
0
def document_package_group(document_package_id):

    group_id = request.args.get('groupId')
    collaborator_email = request.args.get('collaboratorEmail')

    if request.method == 'GET':

        if group_id or collaborator_email:
            if group_id:
                return RouteUtil.data_to_json_response(
                    DocumentPackageService.get_document_package_group(
                        document_package_id, int(group_id)))
            else:
                return RouteUtil.data_to_json_response(
                    DocumentPackageService.
                    get_document_package_group_by_collaborator_email(
                        document_package_id, collaborator_email))
        else:
            return RouteUtil.data_to_json_response(
                DocumentPackageService.get_document_package_groups(
                    document_package_id))

    elif request.method == 'POST':

        if group_id:  # update
            DocumentPackageService.update_document_package_group(
                document_package_id, int(group_id),
                RouteUtil.safe_json_get('name'),
                RouteUtil.safe_json_get('warmUpSize'),
                RouteUtil.safe_json_get('reannotationStep'))
        else:
            return str(
                DocumentPackageService.insert_document_package_group(
                    document_package_id, RouteUtil.safe_json_get('name'),
                    RouteUtil.safe_json_get('warmUpSize'),
                    RouteUtil.safe_json_get('reannotationStep')))

        return 'OK', 200

    elif request.method == 'DELETE':
        DocumentPackageService.remove_document_package_group(
            document_package_id, int(group_id))
        return 'OK', 200
Example #10
0
def ontology(project_id):

    ontology_type = request.args.get('type')

    if request.method == 'GET':

        if ontology_type == 'map':
            return RouteUtil.data_to_json_response(
                ProjectService.get_ontology_map(project_id))
        elif ontology_type == 'tree':
            return RouteUtil.data_to_json_response(
                ProjectService.get_ontology_tree(project_id))
        elif ontology_type == 'summary':
            return RouteUtil.data_to_json_response(
                ProjectService.get_ontology_summary(project_id))
        else:
            project_ontology = ProjectService.get_ontology(project_id)
            if project_ontology:
                return make_response(
                    send_file(project_ontology,
                              as_attachment=False,
                              add_etags=False,
                              attachment_filename=project_ontology.name,
                              mimetype=project_ontology.contentType))
            return 'NOT FOUND', 404

    elif request.method == 'POST':
        if request.json:

            _, b64data = request.json['data'].split(',')
            request.json['bytes'] = base64.b64decode(b64data)

            return ProjectService.update_project_ontology(
                project_id, request.json), 201

        return 'CHECK THE INPUT', 400
Example #11
0
def ontology_structure():

    if request.json:
        _, b64data = request.json['data'].split(',')
        request.json['bytes'] = base64.b64decode(b64data)

        ontology_string = io.BytesIO(
            request.json['bytes']).read().decode('UTF-8')

        could_read, result = OntologyReader.read_ontology_as_dict(
            ontology_string)

        if could_read:
            return RouteUtil.data_to_json_response(result)

    return 'CHECK THE INPUT', 400
Example #12
0
def document_packages_statistics():

    document_package_ids = request.args.get('documentPackageIds')

    statistics_type = request.args.get('type')
    statistics_filter = request.args.get('filter')
    append_collaborations = request.args.get('appendCollaborations')
    collaborators_to_filter = request.args.get('collaboratorsToFilter')

    if statistics_type == 'words':

        return RouteUtil.data_to_json_response(
            DocumentPackageService.get_document_package_words_statistics(
                document_package_ids=document_package_ids.split(','),
                include_collaborations=append_collaborations,
                collaborators_to_filter=collaborators_to_filter))
Example #13
0
def project(project_id):

    append_statistics = RouteUtil.value_to_boolean(
        request.args.get('appendStatistics'))

    if request.method == 'GET':
        return RouteUtil.data_to_json_response(
            ProjectService.get_project(project_id, True, append_statistics),
            True)

    elif request.method == 'POST':
        ProjectService.update_project(
            project_id, RouteUtil.safe_json_get('name'),
            RouteUtil.safe_json_get('smartSentenceSegmentation'),
            RouteUtil.safe_json_get('smartWordSegmentation'),
            RouteUtil.safe_json_get('textReplacements'))
        return 'OK', 200

    elif request.method == 'DELETE':
        ProjectService.remove_project(project_id)
        return 'OK', 200
Example #14
0
def project_root():

    skip = request.args.get('skip')
    limit = request.args.get('limit')
    append_statistics = RouteUtil.value_to_boolean(
        request.args.get('appendStatistics'))

    if request.method == 'GET':
        return RouteUtil.data_to_json_response(
            ProjectService.get_projects(
                g.user['email'],
                skip=skip,
                limit=limit,
                append_documents_package_statistics=append_statistics))

    elif request.method == 'POST':

        if 'ontology' not in request.json:
            return 'ONTOLOGY IS REQUIRED', 400

        _, b64data = request.json['ontology']['data'].split(',')
        request.json['ontology']['bytes'] = base64.b64decode(b64data)

        if 'annotationGuidelines' in request.json:
            _, b64data = request.json['annotationGuidelines']['data'].split(
                ',')
            request.json['annotationGuidelines']['bytes'] = base64.b64decode(
                b64data)

        return ProjectService.create_project(
            g.user['email'], RouteUtil.safe_json_get('name'),
            RouteUtil.safe_json_get('language'),
            RouteUtil.safe_json_get('smartSentenceSegmentation'),
            RouteUtil.safe_json_get('smartWordSegmentation'),
            RouteUtil.safe_json_get('textReplacements'),
            RouteUtil.safe_json_get('ontology'),
            RouteUtil.safe_json_get('annotationGuidelines'))
Example #15
0
def user():
    user_email = request.args.get('email')

    if request.method == 'GET':

        # common user only can see your own details
        if g.user['role'] == 'USER':
            return RouteUtil.data_to_json_response(
                AuthenticationService.get_user(g.user['email']))

        if user_email:
            return RouteUtil.data_to_json_response(
                AuthenticationService.get_user(user_email))
        else:
            return RouteUtil.data_to_json_response(
                AuthenticationService.get_users())

    elif request.method == 'POST':

        if user_email:  # update

            # common user only can update your own details and cannot change your role
            if g.user['role'] == 'USER':
                AuthenticationService.update_user(
                    g.user['email'], RouteUtil.safe_json_get('firstName'),
                    RouteUtil.safe_json_get('lastName'), g.user['role'])
            else:
                AuthenticationService.update_user(
                    RouteUtil.safe_json_get('email'),
                    RouteUtil.safe_json_get('firstName'),
                    RouteUtil.safe_json_get('lastName'),
                    RouteUtil.safe_json_get('role'))

            return 'USER UPDATED', 200

        else:  # insert

            if g.user['role'] == 'USER':
                return 'INVALID ROLE', 403

            temporary_token = AuthenticationService.insert_user(
                RouteUtil.safe_json_get('email'),
                RouteUtil.safe_json_get('firstName'),
                RouteUtil.safe_json_get('lastName'),
                RouteUtil.safe_json_get('role'))

            recovery_url = config['RECOVERY_PASSWORD_PAGE_URL'] + '?token=' + temporary_token + \
                           '&email=' + RouteUtil.safe_json_get('email')

            message = config['EMAIL_HEADER']
            message += '<div style="height:200px; padding:50px;">'
            message += '<p>Hi ' + RouteUtil.safe_json_get(
                'firstName') + ',</p>'
            message += config['WELCOME_EMAIL_MESSAGE']
            message += '<p><a href="' + recovery_url + '">' + recovery_url + '</a></p>'
            message += '</div>'
            message += config['EMAIL_FOOTER']

            EmailService.send(config['WELCOME_EMAIL_SUBJECT'],
                              [RouteUtil.safe_json_get('email')], message)

            return 'USER INSERTED', 200

    elif request.method == 'DELETE':

        if g.user['role'] == 'USER':
            return 'INVALID ROLE', 403

        AuthenticationService.remove_user(user_email)
        return 'USER REMOVED', 200
Example #16
0
def document(document_id):

    as_file = RouteUtil.value_to_boolean(request.args.get('asFile'))

    if request.method == 'GET':

        if as_file:

            found_document = DocumentService.get_document(
                document_id,
                append_document_package_detail=False,
                append_collaborator_detail=False,
                log_document_open=False)

            if not found_document:
                return 'NOT FOUND', 404
            '''
            document_text = DocumentService.get_document_as_text(found_document)

            temp_file = tempfile.NamedTemporaryFile(delete=False)
            temp_file.write(document_text.encode('utf-8'))
            temp_file.flush()
            temp_file.seek(0)
            '''

            document_file = DocumentService.get_document_as_file(
                found_document, None,
                RouteUtil.value_to_boolean(
                    request.args.get('includeMetadata')),
                RouteUtil.value_to_boolean(request.args.get('includeText')),
                RouteUtil.value_to_boolean(
                    request.args.get('includeCollaboration')),
                RouteUtil.value_to_boolean(request.args.get('includeStatus')),
                RouteUtil.value_to_boolean(
                    request.args.get('includeDescription')),
                RouteUtil.value_to_boolean(request.args.get('includeLog')),
                RouteUtil.value_to_boolean(
                    request.args.get('includeComments')))

            return make_response(
                send_file(document_file.name,
                          as_attachment=True,
                          add_etags=False,
                          attachment_filename=found_document['name'],
                          mimetype='text/plain',
                          cache_timeout=10))
        else:
            return RouteUtil.data_to_json_response(
                DocumentService.get_document(document_id), True)

    elif request.method == 'POST':

        using_change_log = RouteUtil.value_to_boolean(
            request.args.get('usingChangeLog'))
        state_key = request.args.get('stateKey')

        if using_change_log:
            lock_token = DocumentService.get_document_lock(document_id)
            state_key = DocumentService.update_document_by_log(
                document_id, request.json, lock_token, state_key)
            DocumentService.release_document_lock(lock_token, document_id)
            return state_key, 201
        else:
            DocumentService.update_document(document_id, request.json['name'],
                                            request.json['metadata'])
            return 'OK', 201

    elif request.method == 'DELETE':
        DocumentService.remove_document(document_id)
        return 'OK', 201
Example #17
0
def document_package_comments(document_package_id):

    return RouteUtil.data_to_json_response(
        DocumentPackageService.get_document_package_comments(
            document_package_id))
Example #18
0
def document_package_statistics(document_package_id):

    collaborator_email = request.args.get('collaboratorEmail')
    append_agreement = RouteUtil.value_to_boolean(
        request.args.get('appendAgreement'))

    if collaborator_email:
        return RouteUtil.data_to_json_response(
            DocumentPackageService.
            get_document_package_collaborator_statistics(
                document_package_id, collaborator_email, append_agreement))
    else:

        statistics_type = request.args.get('type')
        statistics_filter = request.args.get('filter')
        append_collaborations = request.args.get('appendCollaborations')
        collaborators_to_filter = request.args.get('collaboratorsToFilter')
        if collaborators_to_filter:
            collaborators_to_filter = collaborators_to_filter.split(',')

        if statistics_type == 'token':
            return RouteUtil.data_to_json_response(
                DocumentPackageService.get_document_package_token_statistics(
                    document_package_id=document_package_id,
                    include_collaborations=append_collaborations,
                    collaborators_to_filter=collaborators_to_filter))

        elif statistics_type == 'tag':
            return RouteUtil.data_to_json_response(
                DocumentPackageService.get_document_package_tag_statistics(
                    document_package_id=document_package_id,
                    include_collaborations=append_collaborations,
                    collaborators_to_filter=collaborators_to_filter))
        elif statistics_type == 'relation':
            return RouteUtil.data_to_json_response(
                DocumentPackageService.
                get_document_package_relation_statistics(
                    document_package_id=document_package_id,
                    include_collaborations=append_collaborations,
                    collaborators_to_filter=collaborators_to_filter))
        elif statistics_type == 'connector':
            return RouteUtil.data_to_json_response(
                DocumentPackageService.
                get_document_package_connector_statistics(
                    document_package_id=document_package_id,
                    include_collaborations=append_collaborations,
                    collaborators_to_filter=collaborators_to_filter))
        elif statistics_type == 'summary':
            return RouteUtil.data_to_json_response(
                DocumentPackageService.get_document_package_summary_statistics(
                    document_package_id=document_package_id,
                    include_collaborations=append_collaborations,
                    collaborators_to_filter=collaborators_to_filter))
        elif statistics_type == 'agreement':

            if statistics_filter == 'all':
                agreement_types = ['tag', 'relation', 'connector']
            else:
                agreement_types = [statistics_filter]

            return RouteUtil.data_to_json_response(
                DocumentPackageService.
                get_document_package_agreement_statistics(
                    document_package_id=document_package_id,
                    collaborators_to_filter=collaborators_to_filter,
                    agreement_types=agreement_types))
        elif statistics_type == 'words':

            return RouteUtil.data_to_json_response(
                DocumentPackageService.get_document_package_words_statistics(
                    document_package_id=document_package_id,
                    include_collaborations=append_collaborations,
                    collaborators_to_filter=collaborators_to_filter))
        else:
            return RouteUtil.data_to_json_response(
                DocumentPackageService.get_document_package_status_statistics(
                    document_package_id=document_package_id,
                    include_collaborations=append_collaborations,
                    collaborators_to_filter=collaborators_to_filter))