Skip to content

Home Information - Your home, your data, organized.

Python
3.2 KB
Raw Blame
"""
Entity Attribute Edit Context - Entity-specific context for attribute editing templates.

This module contains Entity-specific implementations of the AttributeItemEditContext
pattern, encapsulating entity-specific domain knowledge while maintaining
the generic template interface.
"""
from typing import Any, Dict, Optional, Type

from django.forms import ModelForm, BaseInlineFormSet
from django.urls import reverse

from hi.apps.attribute.edit_context import AttributeItemEditContext
from hi.apps.attribute.forms import AttributeUploadForm
from hi.apps.attribute.models import AttributeModel

from .forms import EntityForm, EntityAttributeRegularFormSet, EntityAttributeUploadForm
from .models import Entity, EntityAttribute


class EntityAttributeItemEditContext(AttributeItemEditContext):
    """
    Entity-specific context provider for attribute editing templates.
    
    This class encapsulates Entity-specific knowledge while providing
    the generic interface expected by attribute editing templates.
    """
    
    def __init__(self,
                 entity                 : Entity,
                 extra_template_context : Optional[Dict[str, Any]] = None ) -> None:
        """
        Initialize context for Entity attribute editing.

        Args:
            entity: The Entity instance that owns the attributes
            extra_template_context: View-supplied template variables
                (external_view_data, external_references, data_priority,
                etc.). The view computes them once and threads them
                through the context so the framework's GET and async-
                POST renderers both see them.
        """
        super().__init__(
            owner_type = 'entity',
            owner = entity,
            extra_template_context = extra_template_context,
        )
        return
    
    @property
    def entity(self) -> Entity:
        """Get the Entity instance (typed accessor)."""
        return self.owner
    
    @property
    def attribute_model_subclass(self) -> Type[AttributeModel]:
        return EntityAttribute

    @property
    def attribute_upload_form_class(self) -> Type[AttributeUploadForm]:
        return EntityAttributeUploadForm
    
    def create_owner_form( self, form_data : Optional[ Dict[str, Any] ] = None ) -> ModelForm:
        return EntityForm( form_data, instance = self.entity )

    def create_attribute_model( self ) -> AttributeModel:
        return EntityAttribute( entity = self.entity )
    
    def create_regular_attributes_formset(
            self, form_data : Optional[ Dict[str, Any] ] = None ) -> BaseInlineFormSet:
        return EntityAttributeRegularFormSet(
            form_data,
            instance = self.entity,
            prefix = self.formset_prefix,
        )

    @property
    def can_restore_default(self):
        return False
    
    @property
    def content_body_template_name(self):
        return 'entity/panes/entity_edit_content_body.html'
    
    @property
    def file_upload_url(self) -> str:
        return reverse( 'entity_attribute_upload',
                        kwargs = { 'entity_id': self.entity.id })

    @property
    def allow_internal_attributes(self) -> bool:
        return self.entity.allow_internal_attributes