示例#1
0
    def login(self, username=None, password=True):
        """Checks whether the user with the username and password can log
            into the application.

        Arguments:
            username {string} -- Username of user
            password {string} -- Password of user

        Returns:
            object -- the user object or None
        """

        if username is None:
            raise ValueError('the username cannot be None')

        if password is None:
            raise ValueError('the password cannot be None')

        # Todo: finish up the login
        try:
            user = User.select().where(User.username == str(username)).get()
            return Utilities.convert_unserializable_fields(model_to_dict(user))
        except Exception as err:
            self.logger.error('Error Occurred: {error}'.format(error=err))
            raise LookupError('User does not exists on this service')

        return None
示例#2
0
    def upload_document(self, data=None, document_id=None):
        """Adds a new Document on the service.

        Arguments:
            data {dict} -- the data for the new document
            document_id {string} -- the id of document to add if available

        Returns:
            object -- the added document object or None
        """

        if data is None:
            raise ValueError('document data cannot be None or empty')

        if document_id is None:
            document_id = Utilities.generate_id(
            )  # generate an uuid for this record

        data['ref_id'] = document_id  # set the document_id for this record

        try:
            saved_document = Document.create(**data)
            return model_to_dict(saved_document)
        except Exception as err:
            self.logger.error('Error Occurred: {error}'.format(error=err))
            raise ValueError('Unable to save the document object')

        return None
示例#3
0
    def add_user(self, data=None, user_id=None):
        """Adds a new User on the service.

        Arguments:
            data {dict} -- the data for the new user
            user_id {string} -- the id of user to add if available

        Returns:
            object -- the added user object or None
        """

        if data is None:
            raise ValueError('user data cannot be None or empty')

        if user_id is None:
            user_id = Utilities.generate_id() # generate an uuid for this record

        data['user_id'] = user_id # set the user_id for this record

        try:
            saved_user = User.create(**data)
            return model_to_dict(saved_user)
        except Exception as err:
            self.logger.error('Error Occurred: {error}'.format(error=err))
            raise ValueError('Unable to save the user object')

        return None
示例#4
0
    def add_named_entity(self, data=None, entity_id=None):
        """Adds a new NamedEntity on the service.

        Arguments:
            data {dict} -- the data for the new named_entity
            entity_id {string} -- the id of named_entity to add if available

        Returns:
            object -- the added named_entity object or None
        """

        if data is None:
            raise ValueError('named_entity data cannot be None or empty')

        if entity_id is None:
            entity_id = Utilities.generate_id(
            )  # generate an uuid for this record

        data['entity_id'] = entity_id  # set the entity_id for this record

        try:
            saved_entity = NamedEntity.create(**data)
            return model_to_dict(saved_entity)
        except Exception as err:
            self.logger.error('Error Occurred: {error}'.format(error=err))
            raise ValueError('Unable to save the named_entity object')

        return None
    def add_annotation(self, data=None, document_id=None):
        """Adds a new Annotation to a document on the service.

        Arguments:
            data {dict} -- the data for the new annotation
            document_id {string} -- the id of document to add the annotation to

        Returns:
            object -- the added annotation object or None
        """

        if data is None:
            raise ValueError('annotation data cannot be None or empty')

        if document_id is None:
            raise ValueError('document_id cannot be None or empty')

        if data['annotation_id'] is None:
            annotation_id = Utilities.generate_id() # generate an uuid for this record

        data['annotation_id'] = annotation_id # set the annotation_id for this record
        data['annotation_id'] = 0; # default version is 0
        data['is_deleted'] = False; # default value is False

        try:
            saved_annotation = Annotation.create(**data)
            return model_to_dict(saved_annotation)
        except Exception as err:
            self.logger.error('Error Occurred: {error}'.format(error=err))
            raise ValueError('Unable to save the annotation object')

        return None
示例#6
0
    def fetch_location(self, annotation=None):
        """Fetches the Logitude and Latitude for an annotation via the Nominatim API.
            Actions:
                - Makes API call to their search endpoint
                - Gets list of all results (first 10)
                - Since they are sorted by "importance", 
                    I just start from the top and iterate through until I find a match

        Arguments:
            annotation {string} -- the annotation to check

        Returns:
            object -- the object containing log. and lat. info or None
        """

        if annotation is None:
            raise ValueError('the annotation cannot be None')

        annotation = annotation.lower() # convert to lower case

        # call the location search service api to get the Lag. and Lat.
        url = SETTINGS['location_lookup']['url']
        search_size = SETTINGS['location_lookup']['search_size']
        search_size = search_size if search_size is not None else '20'

        headers = {
            'content-type': 'application/json'
        }
        # define search criteria object
        search_criteria = {
            'q': str(annotation),
            'format': 'json',
            'limit': search_size
        }

        try:
            response = Utilities.get_request(url, headers, search_criteria)
        except Exception as connection_error: # pylint: disable=W0703
            self.logger.error('Error connecting to the Location Search Query API service: {error}'.format(
                error=connection_error
            ))
            return None

        if len(response) == 0:
            return None

        for record in response:
            if record['display_name'] is not None \
                and annotation.lower() in record['display_name'].lower():
                return {
                    'latitude': record['lat'],
                    'longitude': record['lon']
                }

        return None
示例#7
0
    def get_user(self, user_id=None):
        """Gets users on this services (useful when managing user accounts).

        Arguments:
            user_id {str} -- id of the user to find

        Returns:
            dict -- the user object or an None
        """
        if user_id is None:
            raise ValueError('id of user to fetch cannot be None')

        try:
            user = User.select().where(User.user_id == str(user_id)).get()
            return Utilities.convert_unserializable_fields(model_to_dict(user))
        except Exception as err:
            self.logger.error('Error Occurred: {error}'.format(error=err))
            raise LookupError('User does not exists on this service')

        return None
    def get_annotation(self, annotation_id=None):
        """Gets annotations on this services (useful when managing annotation accounts).

        Arguments:
            annotation_id {str} -- id of the annotation to find

        Returns:
            dict -- the annotation object or an None
        """
        if annotation_id is None:
            raise ValueError('id of annotation to fetch cannot be None')

        try:
            annotation = Annotation.select().where(Annotation.annotation_id == str(annotation_id)).get()
            return Utilities.convert_unserializable_fields(model_to_dict(annotation))
        except Exception as err:
            self.logger.error('Error Occurred: {error}'.format(error=err))
            raise LookupError('Annotation does not exists on this service')

        return None
示例#9
0
    def get_document(self, document_id=None):
        """Gets documents on this services (useful when managing document accounts).

        Arguments:
            document_id {str} -- id of the document to find

        Returns:
            dict -- the document object or an None
        """
        if document_id is None:
            raise ValueError('id of document to fetch cannot be None')

        try:
            document = Document.select().where(
                Document.ref_id == str(document_id)).get()
            return Utilities.convert_unserializable_fields(
                model_to_dict(document))
        except Exception as err:
            self.logger.error('Error Occurred: {error}'.format(error=err))
            raise LookupError('Document does not exists on this service')

        return None
示例#10
0
    def get_users(self, offset=None, limit=True):
        """Gets users on this services (useful when managing user accounts).

        Arguments:
            offset {int} -- offset to start fetching the record
            limit {int} -- limit of the data to return

        Returns:
            List -- the list of user objects or an empty list
        """
        all_users = []
        try:
            users = User.select()
            for user in users:
                all_users \
                    .append(Utilities.convert_unserializable_fields(model_to_dict(user)))
            return all_users
        except Exception as err:
            self.logger.error('Error Occurred: {error}'.format(error=err))
            raise LookupError('Error while fetching all users on this service')

        return []
示例#11
0
    def get_named_entity(self, entity_id=None):
        """Gets named_entities on this services (useful when managing named_entity accounts).

        Arguments:
            entity_id {str} -- id of the named_entity to find

        Returns:
            dict -- the named_entity object or an None
        """
        if entity_id is None:
            raise ValueError('id of named_entity to fetch cannot be None')

        try:
            named_entity = NamedEntity.select().where(
                NamedEntity.entity_id == str(entity_id)).get()
            return Utilities.convert_unserializable_fields(
                model_to_dict(named_entity))
        except Exception as err:
            self.logger.error('Error Occurred: {error}'.format(error=err))
            raise LookupError('NamedEntity does not exists on this service')

        return None
    def get_annotations_in_document(self, document_id=None):
        """Gets annotations on document on this services (useful when managing annotation accounts).

        Arguments:
            document_id {str} -- id of the document

        Returns:
            dict -- the list of annotation objects or an None
        """
        if document_id is None:
            raise ValueError('id of document cannot be None')

        all_annotations = []
        try:
            annotations = Annotation.select().where(Annotation.document_id == str(document_id)).get()
            for annotation in annotations:
                all_annotations.append(Utilities.convert_unserializable_fields(model_to_dict(annotation)))
            return all_annotations
        except Exception as err:
            self.logger.error('Error Occurred: {error}'.format(error=err))
            raise LookupError('Annotations does not exists on this document')

        return None
示例#13
0
    def get_named_entities(self, offset=None, limit=True):
        """Gets named_entities on this services (useful when managing named_entities).

        Arguments:
            offset {int} -- offset to start fetching the record
            limit {int} -- limit of the data to return

        Returns:
            List -- the list of named_entity objects or an empty list
        """

        all_named_entities = []
        try:
            named_entities = NamedEntity.select()
            for named_entity in named_entities:
                all_named_entities \
                    .append(Utilities.convert_unserializable_fields(model_to_dict(named_entity)))
            return all_named_entities
        except Exception as err:
            self.logger.error('Error Occurred: {error}'.format(error=err))
            raise LookupError(
                'Error while fetching all named_entities on this service')

        return {}