Skip to content

service

django_spire.contrib.service

__all__ = ['BaseDjangoModelService', 'ServiceException'] module-attribute

ServiceException

BaseDjangoModelService

Bases: BaseDjangoModelConstructor[TypeDjangoModel], ABC, Generic[TypeDjangoModel]

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

    if obj is None:
        return

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

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

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

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

    self.obj: TypeAny = obj

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

    self.__post_init__()

validate_model_obj

Source code in django_spire/contrib/service/django_model_service.py
def validate_model_obj(self, **field_data: dict) -> list[str]:
    concrete_fields = self._get_concrete_fields()
    touched_fields = self._get_touched_fields(concrete_fields, **field_data)

    try:
        self.obj.full_clean(
            exclude=[field for field in concrete_fields if field not in touched_fields]
        )
    except:
        raise

    return touched_fields

save_model_obj

Source code in django_spire/contrib/service/django_model_service.py
@transaction.atomic
def save_model_obj(self, **field_data: dict) -> tuple[Model, bool]:
    new_model_obj_was_created = False

    if not field_data:
        raise ServiceException(f'Field data is required to save on {self.obj.__class__.__name__}')

    touched_fields = self.validate_model_obj(**field_data)

    if self.model_obj_is_new:
        new_model_obj_was_created = True
        self.obj.save()

    elif touched_fields:
        self.obj.save(update_fields=touched_fields)

    else:
        logging.warning(
            f'{self.obj.__class__.__name__} is not a new object or there was no touched fields to update.')

    return self.obj, new_model_obj_was_created