Skip to content

processor_service

django_spire.contrib.ordering.services.processor_service

OrderingProcessorService

Bases: BaseDjangoModelService['OrderingModelMixin']

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__()

obj instance-attribute

move_to_position

Source code in django_spire/contrib/ordering/services/processor_service.py
def move_to_position(
    self,
    destination_objects: QuerySet[Model],
    position: int,
    origin_objects: QuerySet[Model] = None,
):
    # If origin objects not passed, assume origin objects are same as destination
    if origin_objects is None:
        origin_objects = destination_objects

    ordering_mixin_validator = OrderingMixinValidator(
        destination_objects=destination_objects,
        position=position,
        obj=self.obj,
        origin_objects=origin_objects,
    )

    if not ordering_mixin_validator.validate():
        raise OrderingMixinExceptionGroup(
            'Ordering validation failed.',
            ordering_mixin_validator.errors
        )

    destination_objects = destination_objects.exclude(pk=self.obj.pk).order_by('order')
    origin_objects = origin_objects.exclude(pk=self.obj.pk).order_by('order')

    self._reorder_destination_and_origin_objects(
        destination_objects=destination_objects,
        origin_objects=origin_objects,
        insert_position=position,
    )

    self.obj.order = position
    self.obj.save(update_fields=['order'])

remove_from_objects

Source code in django_spire/contrib/ordering/services/processor_service.py
def remove_from_objects(
    self,
    destination_objects: QuerySet[Model],
):
    ordering_mixin_validator = OrderingMixinValidator(
        destination_objects=destination_objects,
        position=0,
        obj=self.obj,
        origin_objects=destination_objects,
    )

    if not ordering_mixin_validator.validate():
        raise ExceptionGroup(
            'Ordering validation failed.',
            ordering_mixin_validator.errors
        )

    destination_objects = destination_objects.exclude(pk=self.obj.pk).order_by('order')

    self._reorder_objects(objects=destination_objects)