Пример #1
0
def entity_data_view(request, entity_name):
    """
    API call to fetch and edit entity data
    """
    if request.method == 'GET':
        params = request.GET.dict()
        # Fetch Languages supported by the entity

        try:
            pagination_size = int(params.get('size', 10))
        except ValueError:
            raise APIHandlerException('size should be sent as a number')

        try:
            pagination_from = int(params.get('from', 0))
        except ValueError:
            raise APIHandlerException('from should be sent as a number')

        return dictionary_utils.search_entity_values(
            entity_name=entity_name,
            value_search_term=params.get('value_search_term', None),
            variant_search_term=params.get('variant_search_term', None),
            empty_variants_only=params.get('empty_variants_only', False),
            pagination_size=pagination_size,
            pagination_from=pagination_from)

    elif request.method == 'POST':
        # Update language support in the specified entity
        data = json.loads(request.body.decode(encoding='utf-8'))
        dictionary_utils.update_entity_records(entity_name, data)
        return True

    else:
        raise APIHandlerException("{0} is not allowed.".format(request.method))
Пример #2
0
def entity_update_languages(entity_name, new_language_list):
    """
    Updates the language support list of the entity by creating dummy records. Currently does not
    support removal of a language.
    It creates empty variant records for all the unique values present in this entity.

    Args:
        entity_name (str): Name of the entity for which unique values are to be fetched
        new_language_list (list): List of language codes for the new entity
    Returns:
        bool: Success flag if the update
    Raises:
        APIHandlerException (Exception): for any validation errors
    """
    old_language_list = entity_supported_languages(entity_name)
    languages_added = set(new_language_list) - set(old_language_list)
    languages_removed = set(old_language_list) - set(new_language_list)

    if languages_removed:
        # raise exception as it is not currently supported
        raise APIHandlerException(
            'Removing languages is not currently supported.')

    if not languages_added:
        # no change in language list. raise error
        raise APIHandlerException(
            'No new languages provided. Nothing changed.')

    # fetch all words
    # TODO: If possible add records in single ES query instead of
    #       two (get_entity_unique_values + db.add_entity_data)
    values = get_entity_unique_values(entity_name=entity_name)
    if not values:
        raise APIHandlerException(
            'This entity does not have any records. Please verify the entity name'
        )

    records_to_create = []
    for language_script in languages_added:
        # create records for all words
        for value in values:
            if value and language_script:
                records_to_create.append({
                    'value': value,
                    'language_script': language_script,
                    'variants': []
                })

    datastore_obj = DataStore()
    datastore_obj.add_entity_data(entity_name, records_to_create)

    return True
Пример #3
0
def read_unique_values_for_text_entity(request, entity_name):
    """
    API call to View unique values for text entity.
    """
    if request.method == 'GET':
        try:
            return dictionary_utils.get_entity_unique_values(entity_name=entity_name)
        except (DataStoreSettingsImproperlyConfiguredException,
                EngineNotImplementedException,
                EngineConnectionException, FetchIndexForAliasException) as error_message:
            raise APIHandlerException(str(error_message))
    else:
        raise APIHandlerException("{0} is not allowed.".format(request.method))
Пример #4
0
def entity_data_view(request, entity_name):
    """
    API call to fetch and edit entity data
    """
    if request.method == 'GET':
        params = request.GET.dict()

        shuffle = (params.get('shuffle', 'false') or '').lower() == 'true'
        try:
            seed = int(params.get('seed', random.randint(0, 1000000000)))
        except ValueError:
            raise APIHandlerException('seed should be sent as a number')

        try:
            size = int(params.get('size', 10))
        except ValueError:
            raise APIHandlerException('size should be sent as a number')

        try:
            pagination_from = int(params.get('from', 0))
        except ValueError:
            raise APIHandlerException('from should be sent as a number')

        try:
            return dictionary_utils.search_entity_values(
                entity_name=entity_name,
                value_search_term=params.get('value_search_term', None),
                variant_search_term=params.get('variant_search_term', None),
                empty_variants_only=params.get('empty_variants_only', False),
                shuffle=shuffle,
                offset=pagination_from,
                size=size,
                seed=seed,
            )
        except ValueError as e:
            raise APIHandlerException(str(e)) from e

    elif request.method == 'POST':
        # Update language support in the specified entity
        data = json.loads(request.body.decode(encoding='utf-8'))
        dictionary_utils.update_entity_records(entity_name, data)
        return True

    else:
        raise APIHandlerException("{0} is not allowed.".format(request.method))
Пример #5
0
def entity_language_view(request, entity_name):
    """
    API call to View and Edit the list of languages supported by an entity.
    """
    if request.method == 'GET':
        # Fetch Languages supported by the entity
        return {
            'supported_languages': dictionary_utils.entity_supported_languages(entity_name)
        }

    elif request.method == 'POST':
        # Update language support in the specified entity
        data = json.loads(request.body.decode(encoding='utf-8'))
        dictionary_utils.entity_update_languages(entity_name, data.get('supported_languages', []))
        return True

    else:
        raise APIHandlerException("{0} is not allowed.".format(request.method))