Skip to content

transformation_service

django_spire.metric.visual.presentation.services.transformation_service

PresentationTransformationService

Bases: BaseDjangoModelService['Presentation']

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

slides

Source code in django_spire/metric/visual/presentation/services/transformation_service.py
def slides(self) -> QuerySet[Slide]:
    return self.obj.slides.with_sections().filter(is_deleted=False)

slide_count

Source code in django_spire/metric/visual/presentation/services/transformation_service.py
def slide_count(self) -> int:
    return self.obj.slides.filter(is_deleted=False).count()

SlideTransformationService

Bases: BaseDjangoModelService['Slide']

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

sections

Source code in django_spire/metric/visual/presentation/services/transformation_service.py
def sections(self) -> QuerySet[SlideSection]:
    return self.obj.sections.filter(is_deleted=False, visual__is_deleted=False)

SlideSectionTransformationService

Bases: BaseDjangoModelService['SlideSection']

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

section_grid_styles staticmethod

Source code in django_spire/metric/visual/presentation/services/transformation_service.py
@staticmethod
def section_grid_styles(sections: Iterable[SlideSection]) -> dict[int, str]:
    ordered = sorted(sections, key=lambda section: (section.row, section.col, section.pk))

    row_counts: dict[int, int] = {}
    for section in ordered:
        row_counts[section.row] = row_counts.get(section.row, 0) + 1

    styles: dict[int, str] = {}
    positions: dict[int, int] = {}
    for section in ordered:
        count = row_counts[section.row]
        span = SLIDE_GRID_COLUMNS // count
        position = positions.get(section.row, 0)
        positions[section.row] = position + 1
        start = (position * span) + 1
        styles[section.pk] = f'grid-column: {start} / span {span}; grid-row: {section.row + 1};'

    return styles

render_context

Source code in django_spire/metric/visual/presentation/services/transformation_service.py
def render_context(self) -> dict:
    if not self.obj.visual_id or self.obj.visual.is_deleted:
        return {'visual': None, 'current_value': None, 'current_condition': None, 'chart': None}

    return self.obj.visual.services.transformation.render_context()