def delete_annotations(self, request, annotation_id=None):
        """Verifies that the required fields are set in request
        - open db connection and parse get/post request
        - delete annotation where annotation_id in db
        - close db connection
        - Return successful or failed
        - end

        Arguments:
            request {object} -- the query parameters passed into this function

        Returns:
            object -- response from this endpoint
        """

        # query_params = request.raw_args
        # annotation_id = query_params['annotation_id'] if 'annotation_id' in query_params else None

        if annotation_id is None:
            return ApiResponse.failure(
                {
                    'message':
                    'Kindly pass the annotation_id of the annotation to patch',
                    'response': {}
                }, 400)

        self.logger.info(
            'Received delete annotation request for id: {annotation_id}'.
            format(annotation_id=annotation_id))

        try:
            is_deleted = self.service.delete_annotation(annotation_id)
        except Exception as err:
            self.logger.error('Error Occurred: {error}'.format(error=err))
            return ApiResponse.failure(
                {
                    'message':
                    'Error occured while deleting the annotation. ' +
                    'Error: {error}'.format(error=err),
                    'response': {}
                }, 500)

        if is_deleted is False:
            return ApiResponse.failure(
                {
                    'message':
                    'An error occured while deleting the annotation',
                    'response': {}
                }, 500)

        # Annotation has been successfully updated
        return ApiResponse.success(
            {
                'code': 204,
                'message': 'annotation was successfully deleted',
                'response': {}
            }, 204)
    def get_annotations(self, request, annotation_id=None):
        """Fetches annotation(s) on the service
        - Receive and parse get request
        - Go into AWS RDS and fetch annotation(s)
        - Return success or not found
        - end

        Arguments:
            request {object} -- the query parameters passed into this function

        Returns:
            object -- response from this endpoint
        """

        # query_params = request.raw_args
        # annotation_id = query_params['annotation_id'] if 'annotation_id' in query_params else None

        self.logger.info(
            'Received get annotation request for id: {annotation_id}'.format(
                annotation_id=(
                    annotation_id if annotation_id is not None else '')))

        annotation = None
        annotations = {}
        try:
            if annotation_id is None:
                annotations = self.service.get_annotations()
            else:
                annotation = self.service.get_annotation(annotation_id)
        except LookupError as error:
            self.logger.error('Error Occurred: {error}'.format(error=error))
            return ApiResponse.failure(
                {
                    'message': 'Error while fetching annotation(s)',
                    'response': {}
                }, 500)

        if not annotations and annotation is None:
            return ApiResponse.failure(
                {
                    'message': 'No annotation(s) found',
                    'response': {}
                }, 404)

        return_data = annotation if annotation is not None else annotations
        print(return_data)

        # Return annotation object on successful login
        return ApiResponse.success({
            'code': 200,
            'message': 'successfully fetched annotation(s) on service',
            'response': return_data
        })
    def get_entities(self, request, entity_id=None):
        """Fetches named_entity(s) on the service
        - Receive and parse get request
        - Go into AWS RDS and fetch named_entity(s)
        - Return success or not found
        - end

        Arguments:
            request {object} -- the query parameters passed into this function

        Returns:
            object -- response from this endpoint
        """

        # query_params = request.raw_args
        # entity_id = query_params['entity_id'] if 'entity_id' in query_params else None

        self.logger.info(
            'Received get named_entity request for id: {entity_id}'.format(
                entity_id=(entity_id if entity_id is not None else '')))

        named_entity = None
        named_entities = {}
        try:
            if entity_id is None:
                named_entities = self.service.get_named_entities()
            else:
                named_entity = self.service.get_named_entity(entity_id)
        except LookupError as error:
            self.logger.error('Error Occurred: {error}'.format(error=error))
            return ApiResponse.failure(
                {
                    'message': 'Error while fetching named_entity(s)',
                    'response': {}
                }, 500)

        if not named_entities and named_entity is None:
            return ApiResponse.failure(
                {
                    'message': 'No named_entity(s) found',
                    'response': {}
                }, 404)

        return_data = named_entity if named_entity is not None else named_entities
        print(return_data)

        # Return named_entity object on successful login
        return ApiResponse.success({
            'code': 200,
            'message': 'successfully fetched named_entity(s) on service',
            'response': return_data
        })
Exemplo n.º 4
0
    def get_documents(self, request, document_id=None):
        """Fetches document(s) on the service
        - Receive and parse get request
        - Go into AWS RDS and fetch document(s)
        - Return success or not found
        - end

        Arguments:
            request {object} -- the query parameters passed into this function

        Returns:
            object -- response from this endpoint
        """

        # query_params = request.raw_args
        # document_id = query_params['document_id'] if 'document_id' in query_params else None

        self.logger.info(
            'Received get document request for id: {document_id}'.format(
                document_id=(document_id if document_id is not None else '')))

        document = None
        documents = {}
        try:
            if document_id is None:
                documents = self.service.get_documents()
            else:
                document = self.service.get_document(document_id)
        except LookupError as error:
            self.logger.error('Error Occurred: {error}'.format(error=error))
            return ApiResponse.failure(
                {
                    'message': 'Error while fetching document(s)',
                    'response': {}
                }, 500)

        if not documents and document is None:
            return ApiResponse.failure(
                {
                    'message': 'No document(s) found',
                    'response': {}
                }, 404)

        return_data = document if document is not None else documents

        # Return document object on successful login
        return ApiResponse.success({
            'code': 200,
            'message': 'successfully fetched document(s) on service',
            'response': return_data
        })
    def get_users(self, request, user_id=None):
        """Fetches user(s) on the service
        - Receive and parse get request
        - Go into AWS RDS and fetch user(s)
        - Return success or not found
        - end

        Arguments:
            request {object} -- the query parameters passed into this function

        Returns:
            object -- response from this endpoint
        """

        # query_params = request.raw_args
        # user_id = query_params['user_id'] if 'user_id' in query_params else None

        self.logger.info('Received get user request for id: {user_id}'.format(
            user_id=(user_id if user_id is not None else '')))

        user = None
        users = {}
        try:
            if user_id is None:
                users = self.service.get_users()
            else:
                user = self.service.get_user(user_id)
        except LookupError as error:
            self.logger.error('Error Occurred: {error}'.format(error=error))
            return ApiResponse.failure(
                {
                    'message': 'Error while fetching user(s)',
                    'response': {}
                }, 500)

        if not users and user is None:
            return ApiResponse.failure(
                {
                    'message': 'No user(s) found',
                    'response': {}
                }, 404)

        return_data = user if user is not None else users

        # Return users
        return ApiResponse.success({
            'code': 200,
            'message': 'successfully fetched user(s) on service',
            'response': return_data
        })
    def get_document_annotations(self, request, document_id=None):
        """Fetches annotation(s) on the document on the service
        - Receive and parse get request
        - Go into AWS RDS and fetch annotation(s)
        - Return success or not found
        - end

        Arguments:
            request {object} -- the query parameters passed into this function

        Returns:
            object -- response from this endpoint
        """

        self.logger.info(
            'Received get annotations request for document_id: {document_id}'.
            format(
                document_id=(document_id if document_id is not None else '')))

        try:
            annotations = self.service.get_annotations_in_document(document_id)
        except LookupError as error:
            self.logger.error('Error Occurred: {error}'.format(error=error))
            return ApiResponse.failure(
                {
                    'message': 'Error while fetching annotation(s)',
                    'response': {}
                }, 500)

        if not annotations:
            return ApiResponse.failure(
                {
                    'message': 'No annotation(s) found on document',
                    'response': {}
                }, 404)

        # Return annotation object on successful login
        return ApiResponse.success({
            'code': 200,
            'message':
            'successfully fetched annotation(s) on document on service',
            'response': annotations
        })
    def update_annotations(self, request, annotation_id=None):
        """Verifies that the required fields are set in request
        - open db connection and parse get/post request
        - update model within db
        - close db connection
        - Return updated annotation object
        - end

        Arguments:
            request {object} -- the query parameters passed into this function

        Returns:
            object -- response from this endpoint
        """

        request_body = request.body
        body_params = {}
        if request_body != b'':
            body_params = json.loads(request_body)

        if annotation_id is None:
            return ApiResponse.failure(
                {
                    'message':
                    'Kindly pass the annotation_id of the annotation to patch',
                    'response': {}
                }, 400)

        self.logger.info(
            'Received update annotation request for id: {annotation_id} // : {params}' \
                .format(
                    annotation_id=annotation_id,
                    params=body_params
                ))

        if not body_params.keys():
            return ApiResponse.failure(
                {
                    'message': 'annotation data cannot be null',
                    'response': {}
                }, 400)

        document_id = body_params[
            'document_id'] if 'document_id' in body_params else None
        user_id = body_params['user_id'] if 'user_id' in body_params else None
        data = body_params['data'] if 'data' in body_params else None

        annotation_data = {}

        if document_id is not None:
            annotation_data['document_id'] = document_id
        if user_id is not None:
            annotation_data['user_id'] = user_id
        if data is not None:
            annotation_data['data'] = json.dumps(
                data)  # serialize the annotation data

        try:
            annotation = self.service.update_annotation(
                annotation_id, annotation_data)
        except Exception as err:
            self.logger.error('Error Occurred: {error}'.format(error=err))
            return ApiResponse.failure(
                {
                    'message':
                    'Error occured while updating the annotation. ' +
                    'Error: {error}'.format(error=err),
                    'response': {}
                }, 500)

        if annotation is None:
            return ApiResponse.failure(
                {
                    'message':
                    'An error occured while updating the annotation',
                    'response': {}
                }, 500)

        # Annotation has been successfully updated
        return ApiResponse.success(
            {
                'code': 200,
                'message': 'annotation was successfully updated',
                'response': annotation
            }, 200)
    def add_annotations(self, request):
        """Verifies that the required fields are set in request
        - open db connection and parse get request
        - commit model to db
        - colse db connection
        - Return created annotation object
        - end

        Arguments:
            request {object} -- the query parameters passed into this function

        Returns:
            object -- response from this endpoint
        """

        request_body = request.body
        body_params = {}
        if request_body != b'':
            body_params = json.loads(request_body)

        self.logger.info('Received create annotation request: {params}'.format(
            params=body_params))

        if not body_params.keys():
            return ApiResponse.failure(
                {
                    'message': 'annotation data cannot be null',
                    'response': {}
                }, 400)

        document_id = body_params[
            'document_id'] if 'document_id' in body_params else None
        user_id = body_params['user_id'] if 'user_id' in body_params else None
        data = body_params['data'] if 'data' in body_params else None

        annotation_data = {
            'document_id': document_id,
            'user_id': user_id,
            'data': json.dumps(data),  # serialize the annotation data
        }

        try:
            annotation = self.service.add_annotation(annotation_data)
        except Exception as err:
            self.logger.error('Error Occurred: {error}'.format(error=err))
            return ApiResponse.failure(
                {
                    'message':
                    'Error occured while adding the new annotation. ' +
                    'Error: {error}'.format(error=err),
                    'response': {}
                }, 500)

        if annotation is None:
            return ApiResponse.failure(
                {
                    'message':
                    'An error occured while adding the new annotation',
                    'response': {}
                }, 500)

        # Return annotation object on successful login
        return ApiResponse.success(
            {
                'code': 201,
                'message': 'annotation was successfully created',
                'response': annotation
            }, 201)
    def update_users(self, request, user_id=None):
        """Verifies that the required fields are set in request
        - open db connection and parse get/post request
        - update model within db
        - close db connection
        - Return updated user object
        - end

        Arguments:
            request {object} -- the query parameters passed into this function

        Returns:
            object -- response from this endpoint
        """

        request_body = request.body
        body_params = {}
        if request_body != b'':
            body_params = json.loads(request_body)

        if user_id is None:
            return ApiResponse.failure(
                {
                    'message': 'Kindly pass the user_id of the user to patch',
                    'response': {}
                }, 400)

        self.logger.info(
            'Received update user request for id: {user_id} // : {params}'.
            format(user_id=user_id, params=body_params))

        if not body_params.keys():
            return ApiResponse.failure(
                {
                    'message': 'user data cannot be null',
                    'response': {}
                }, 400)

        username = body_params[
            'username'] if 'username' in body_params else None
        first_name = body_params[
            'first_name'] if 'first_name' in body_params else None
        last_name = body_params[
            'last_name'] if 'last_name' in body_params else None
        role = body_params['role'] if 'role' in body_params else None

        user_data = {}

        if first_name is not None:
            user_data['first_name'] = first_name
        if last_name is not None:
            user_data['last_name'] = last_name
        if username is not None:
            user_data['username'] = username
        if role is not None:
            role = int(role, base=10)
            user_data['role'] = 0 if (role < 0 or role > 1) else role

        try:
            user = self.service.update_user(user_id, user_data)
        except Exception as err:
            self.logger.error('Error Occurred: {error}'.format(error=err))
            return ApiResponse.failure(
                {
                    'message':
                    'Error occured while updating the user. ' +
                    'Error: {error}'.format(error=err),
                    'response': {}
                }, 500)

        if user is None:
            return ApiResponse.failure(
                {
                    'message': 'An error occured while updating the user',
                    'response': {}
                }, 500)

        # User has been successfully updated
        return ApiResponse.success(
            {
                'code': 200,
                'message': 'user was successfully updated',
                'response': user
            }, 200)
    def add_users(self, request):
        """Verifies that the required fields are set in request
        - open db connection and parse get request
        - commit model to db
        - colse db connection
        - Return created user object
        - end

        Arguments:
            request {object} -- the query parameters passed into this function

        Returns:
            object -- response from this endpoint
        """

        request_body = request.body
        body_params = {}
        if request_body != b'':
            body_params = json.loads(request_body)

        self.logger.info('Received create user request: {params}'.format(
            params=body_params))

        if not body_params.keys():
            return ApiResponse.failure(
                {
                    'message': 'user data cannot be null',
                    'response': {}
                }, 400)

        username = body_params[
            'username'] if 'username' in body_params else None
        password = body_params[
            'password'] if 'password' in body_params else None
        first_name = body_params[
            'first_name'] if 'first_name' in body_params else None
        last_name = body_params[
            'last_name'] if 'last_name' in body_params else None
        role = body_params['role'] if 'role' in body_params else None

        user_data = {
            'username': username,
            'password': password,
            'first_name': first_name,
            'last_name': last_name,
            'role': int(role) if role is not None else 0,
            'ip_address': None,
            'last_login_time': None,
        }

        if user_data['role'] < 0 or user_data['role'] > 1:
            user_data['role'] = 0

        try:
            user = self.service.add_user(user_data)
        except Exception as err:
            self.logger.error('Error Occurred: {error}'.format(error=err))
            return ApiResponse.failure(
                {
                    'message':
                    'Error occured while adding the new user. ' +
                    'Error: {error}'.format(error=err),
                    'response': {}
                }, 500)

        if user is None:
            return ApiResponse.failure(
                {
                    'message': 'An error occured while adding the new user',
                    'response': {}
                }, 500)

        # Return user object on successful login
        return ApiResponse.success(
            {
                'code': 201,
                'message': 'user was successfully created',
                'response': user
            }, 201)
    def add_entities(self, request):
        """Verifies that the required fields are set in request
        - open db connection and parse get request
        - commit model to db
        - colse db connection
        - Return created named_entity object
        - end

        Arguments:
            request {object} -- the query parameters passed into this function

        Returns:
            object -- response from this endpoint
        """

        request_body = request.body
        body_params = {}
        if request_body != b'':
            body_params = json.loads(request_body)

        self.logger.info(
            'Received create named_entity request: {params}'.format(
                params=body_params))

        if not body_params.keys():
            return ApiResponse.failure(
                {
                    'message': 'named_entity data cannot be null',
                    'response': {}
                }, 400)

        value = body_params['value'] if 'value' in body_params else None
        description = body_params[
            'description'] if 'description' in body_params else None
        user_id = body_params['user_id'] if 'user_id' in body_params else None
        should_use = body_params[
            'should_use'] if 'should_use' in body_params else None

        entity_data = {
            'value': value,
            'description': description,
            'user_id': user_id,
            'should_use': bool(should_use) if should_use is bool else False,
        }

        try:
            named_entity = self.service.add_named_entity(entity_data)
        except Exception as err:
            self.logger.error('Error Occurred: {error}'.format(error=err))
            return ApiResponse.failure(
                {
                    'message':
                    'Error occured while adding the new named_entity. ' +
                    'Error: {error}'.format(error=err),
                    'response': {}
                }, 500)

        if named_entity is None:
            return ApiResponse.failure(
                {
                    'message':
                    'An error occured while adding the new named_entity',
                    'response': {}
                }, 500)

        # Return named_entity object on successful login
        return ApiResponse.success(
            {
                'code': 201,
                'message': 'named_entity was successfully created',
                'response': named_entity
            }, 201)
    def update_entities(self, request, entity_id=None):
        """Verifies that the required fields are set in request
        - open db connection and parse get/post request
        - update model within db
        - close db connection
        - Return updated named_entity object
        - end

        Arguments:
            request {object} -- the query parameters passed into this function

        Returns:
            object -- response from this endpoint
        """

        request_body = request.body
        body_params = {}
        if request_body != b'':
            body_params = json.loads(request_body)

        if entity_id is None:
            return ApiResponse.failure(
                {
                    'message':
                    'Kindly pass the entity_id of the named_entity to patch',
                    'response': {}
                }, 400)

        self.logger.info(
            'Received update named_entity request for id: {entity_id} // : {params}'
            .format(entity_id=entity_id, params=body_params))

        if not body_params.keys():
            return ApiResponse.failure(
                {
                    'message': 'named_entity data cannot be null',
                    'response': {}
                }, 400)

        value = body_params['value'] if 'value' in body_params else None
        description = body_params[
            'description'] if 'description' in body_params else None
        user_id = body_params['user_id'] if 'user_id' in body_params else None
        should_use = body_params[
            'should_use'] if 'should_use' in body_params else None

        entity_data = {}

        if value is not None:
            entity_data['value'] = value
        if description is not None:
            entity_data['description'] = description
        if user_id is not None:
            entity_data['user_id'] = user_id
        if should_use is not None:
            entity_data['should_use'] = bool(
                should_use) if should_use is bool else False

        try:
            named_entity = self.service.update_named_entity(
                entity_id, entity_data)
        except Exception as err:
            self.logger.error('Error Occurred: {error}'.format(error=err))
            return ApiResponse.failure(
                {
                    'message':
                    'Error occured while updating the named_entity. ' +
                    'Error: {error}'.format(error=err),
                    'response': {}
                }, 500)

        if named_entity is None:
            return ApiResponse.failure(
                {
                    'message':
                    'An error occured while updating the named_entity',
                    'response': {}
                }, 500)

        # NamedEntity has been successfully updated
        return ApiResponse.success(
            {
                'code': 200,
                'message': 'named_entity was successfully updated',
                'response': named_entity
            }, 200)
Exemplo n.º 13
0
    def upload_documents(self, request):
        """Verifies that the required fields are set in request
        - open db connection and parse get request
        - commit model to db
        - colse db connection
        - Return created document object
        - end

        Arguments:
            request {object} -- the query parameters passed into this function

        Returns:
            object -- response from this endpoint
        """

        request_body = request.body
        body_params = {}
        if request_body != b'':
            body_params = json.loads(request_body)

        self.logger.info('Received create document request: {params}'.format(
            params=body_params))

        if not body_params.keys():
            return ApiResponse.failure(
                {
                    'message': 'document data cannot be null',
                    'response': {}
                }, 400)

        name = request.json.get('name', None)
        pdf_content = request.json.get('pdf_content', None)
        auth_user = request.json.get('auth_user', None)
        user_id = auth_user['user_id'] if 'user_id' in auth_user else None

        document_data = {
            'name': name,
            'pdf_content': pdf_content,
            'user_id': user_id,
        }

        try:
            document = self.service.upload_document(document_data)
        except Exception as err:
            self.logger.error('Error Occurred: {error}'.format(error=err))
            return ApiResponse.failure(
                {
                    'message':
                    'Error occured while adding the new document. ' +
                    'Error: {error}'.format(error=err),
                    'response': {}
                }, 500)

        if document is None:
            return ApiResponse.failure(
                {
                    'message':
                    'An error occured while adding the new document',
                    'response': {}
                }, 500)

        # Return document object on successful login
        return ApiResponse.success(
            {
                'code': 201,
                'message': 'document was successfully uploaded',
                'response': document
            }, 201)
Exemplo n.º 14
0
    def update_documents(self, request, document_id=None):
        """Verifies that the required fields are set in request
        - open db connection and parse get/post request
        - update model within db
        - close db connection
        - Return updated document object
        - end

        Arguments:
            request {object} -- the query parameters passed into this function

        Returns:
            object -- response from this endpoint
        """

        request_body = request.body
        body_params = {}
        if request_body != b'':
            body_params = json.loads(request_body)

        if document_id is None:
            return ApiResponse.failure(
                {
                    'message':
                    'Kindly pass the document_id of the document to patch',
                    'response': {}
                }, 400)

        self.logger.info(
            'Received update document request for id: {document_id} // : {params}'
            .format(document_id=document_id, params=body_params))

        if not body_params.keys():
            return ApiResponse.failure(
                {
                    'message': 'document data cannot be null',
                    'response': {}
                }, 400)

        pdf_content = body_params[
            'pdf_content'] if 'pdf_content' in body_params else None
        user_id = body_params['user_id'] if 'user_id' in body_params else None

        document_data = {}

        if pdf_content is not None:
            document_data['pdf_content'] = pdf_content
        if user_id is not None:
            document_data['user_id'] = user_id

        try:
            document = self.service.update_document(document_id, document_data)
        except Exception as err:
            self.logger.error('Error Occurred: {error}'.format(error=err))
            return ApiResponse.failure(
                {
                    'message':
                    'Error occured while updating the document. ' +
                    'Error: {error}'.format(error=err),
                    'response': {}
                }, 500)

        if document is None:
            return ApiResponse.failure(
                {
                    'message': 'An error occured while updating the document',
                    'response': {}
                }, 500)

        # Document has been successfully updated
        return ApiResponse.success(
            {
                'code': 200,
                'message': 'document was successfully updated',
                'response': document
            }, 200)