Skip to content

automation_service

django_spire.knowledge.entry.services.automation_service

EntryAutomationService

Bases: BaseDjangoModelService['Entry']

Source code in django_spire/contrib/constructor/constructor.py
def __init__(self, obj: Any = None):
    self._obj_type_name: str = str(
        next(iter(self.__class__.__annotations__.values()))
    ).split('.')[-1]

    if obj is None:
        return

    self._obj_mro_type_names = [cls.__name__ for cls in obj.__class__.__mro__]

    if self._obj_type_name not in self._obj_mro_type_names:
        message = f'{self.__class__.__name__} was instantiated with obj type "{obj.__class__.__name__}" and failed as it was expecting "{self._obj_type_name}".'
        raise ConstructorError(message)

    self._obj_type: type[TypeAny] = obj.__class__

    if self._obj_type is None or self._obj_type is ...:
        message = f'{self.__class__.__name__} top class attribute must have an annotated type.'
        raise ConstructorError(message)

    self.obj: TypeAny = obj

    if ABC not in self.__class__.__bases__:
        if not self._obj_is_valid:
            message = f'{self._obj_type_name} failed to validate on {self.__class__.__name__}'
            raise ConstructorError(message)

    self.__post_init__()

obj instance-attribute

convert_files_to_model_objects

Source code in django_spire/knowledge/entry/services/automation_service.py
@close_db_connections
def convert_files_to_model_objects(self) -> str:
    file_objects = self.obj_class.services.tool.get_files_to_convert()

    entries = self.obj_class.objects.id_in(
        list({file_object.object_id for file_object in file_objects})
    ).select_related('current_version')

    entry_pk_map = {entry.pk: entry for entry in entries}

    errored = []
    for file_object in file_objects:
        try:
            EntryVersionBlock.services.factory.create_blocks_from_file(
                file=file_object,
                entry_version=entry_pk_map[file_object.object_id].current_version,
            )
        except Exception as e:
            errored.append({'file': file_object.name, 'error': traceback.format_exc()})
            file_object.set_deleted()
        else:
            file_object.set_deleted()

    message = f'Files Converted: {len(file_objects) - len(errored)}'

    if errored:
        error_string = f'\n{message}\nFiles Errored:'

        for error in errored:
            error_string += f'    File Name: {error["file"]}\n    Error: {error["error"]}'

        raise KnowledgeBaseConversionError(error_string)

    return message