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
def update_entity_records(entity_name, data): """ Update dictionary data with the edited and deleted records Args: entity_name (str): Name of the entity for which records are to be fetched data (dict): Dictionary of edited, deleted data. If replace flag is true, then all existing data is deleted before adding the records Returns: None """ # Delete some records first records_to_delete = data.get('deleted', []) records_to_create = data.get('edited', []) replace_data = data.get('replace') if replace_data: # TODO: Delete everything for the `entity_name` without having to fetch values first! # https://www.elastic.co/guide/en/elasticsearch/reference/5.6/docs-delete-by-query.html values_to_delete = get_entity_unique_values(entity_name) else: values_to_delete = [record['word'] for record in records_to_delete] values_to_delete.extend( [record['word'] for record in records_to_create]) value_variants_to_create = [] for record in records_to_create: for language_script, variants in record.get('variants', {}).items(): if record['word'] and language_script: value_variants_to_create.append({ 'value': record['word'], 'language_script': language_script, 'variants': variants.get('value', []) }) # delete words delete_records_by_values(entity_name=entity_name, values=values_to_delete) datastore_obj = DataStore() datastore_obj.add_entity_data(entity_name, value_variants_to_create)
def update_entity_records(entity_name, data): """ Update dictionary data with the edited and deleted records Args: entity_name (str): Name of the entity for which records are to be fetched data (dict): Dictionary of edited, deleted data. If replace flag is true, then all existing data is deleted before adding the records Returns: None """ # Delete some records first records_to_delete = data.get('deleted', []) records_to_create = data.get('edited', []) replace_data = data.get('replace') if replace_data: values_to_delete = get_entity_unique_values(entity_name) else: values_to_delete = [record['word'] for record in records_to_delete] values_to_delete.extend( [record['word'] for record in records_to_create]) value_variants_to_create = [] for record in records_to_create: for language_script, variants in record.get('variants', {}).items(): if record['word'] and language_script: value_variants_to_create.append({ 'value': record['word'], 'language_script': language_script, 'variants': variants.get('value', []) }) # delete words delete_records_by_values(entity_name=entity_name, values=values_to_delete) datastore_obj = DataStore() datastore_obj.add_entity_data(entity_name, value_variants_to_create)