| 1 |
""" |
| 2 |
The ``extra_views.formsets`` provides a simple way to handle formsets. |
| 3 |
The ``extra_views.advanced`` provides a method to combine that with a create/update form. |
| 4 |
|
| 5 |
This package provides classes that support both options for polymorphic formsets. |
| 6 |
""" |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
from typing import Any, cast |
| 11 |
|
| 12 |
import extra_views # type: ignore[import-untyped] |
| 13 |
from django.core.exceptions import ImproperlyConfigured |
| 14 |
from django.forms import BaseFormSet |
| 15 |
|
| 16 |
from polymorphic.formsets import ( |
| 17 |
BasePolymorphicInlineFormSet, |
| 18 |
BasePolymorphicModelFormSet, |
| 19 |
PolymorphicFormSetChild, |
| 20 |
polymorphic_child_forms_factory, |
| 21 |
) |
| 22 |
|
| 23 |
__all__ = ( |
| 24 |
"PolymorphicFormSetView", |
| 25 |
"PolymorphicInlineFormSetView", |
| 26 |
"PolymorphicInlineFormSet", |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
class PolymorphicFormSetMixin: |
| 31 |
""" |
| 32 |
Internal Mixin, that provides polymorphic integration with the ``extra_views`` package. |
| 33 |
""" |
| 34 |
|
| 35 |
formset_class: type[BaseFormSet] = BasePolymorphicModelFormSet |
| 36 |
|
| 37 |
#: Default 0 extra forms |
| 38 |
factory_kwargs: dict[str, Any] = {"extra": 0} |
| 39 |
|
| 40 |
#: Define the children |
| 41 |
# :type: list[PolymorphicFormSetChild] |
| 42 |
formset_children: list[PolymorphicFormSetChild] | None = None |
| 43 |
|
| 44 |
def get_formset_children(self) -> list[PolymorphicFormSetChild]: |
| 45 |
""" |
| 46 |
:rtype: list[PolymorphicFormSetChild] |
| 47 |
""" |
| 48 |
if not self.formset_children: |
| 49 |
raise ImproperlyConfigured( |
| 50 |
"Define 'formset_children' as list of `PolymorphicFormSetChild`" |
| 51 |
) |
| 52 |
return self.formset_children |
| 53 |
|
| 54 |
def get_formset_child_kwargs(self) -> dict[str, Any]: |
| 55 |
return {} |
| 56 |
|
| 57 |
def get_formset(self) -> type[BaseFormSet]: |
| 58 |
""" |
| 59 |
Returns the formset class from the inline formset factory |
| 60 |
""" |
| 61 |
# Implementation detail: |
| 62 |
# Since `polymorphic_modelformset_factory` and `polymorphic_inlineformset_factory` mainly |
| 63 |
# reuse the standard factories, and then add `child_forms`, the same can be done here. |
| 64 |
# This makes sure the base class construction is completely honored. |
| 65 |
FormSet = super().get_formset() # type: ignore[misc] |
| 66 |
FormSet.child_forms = polymorphic_child_forms_factory( |
| 67 |
self.get_formset_children(), **self.get_formset_child_kwargs() |
| 68 |
) |
| 69 |
return cast(type[BaseFormSet], FormSet) |
| 70 |
|
| 71 |
|
| 72 |
class PolymorphicFormSetView(PolymorphicFormSetMixin, extra_views.ModelFormSetView): |
| 73 |
""" |
| 74 |
A view that displays a single polymorphic formset. |
| 75 |
|
| 76 |
.. code-block:: python |
| 77 |
|
| 78 |
from polymorphic.formsets import PolymorphicFormSetChild |
| 79 |
|
| 80 |
|
| 81 |
class ItemsView(PolymorphicFormSetView): |
| 82 |
model = Item |
| 83 |
formset_children = [ |
| 84 |
PolymorphicFormSetChild(ItemSubclass1), |
| 85 |
PolymorphicFormSetChild(ItemSubclass2), |
| 86 |
] |
| 87 |
|
| 88 |
""" |
| 89 |
|
| 90 |
formset_class: type[BasePolymorphicModelFormSet] = BasePolymorphicModelFormSet |
| 91 |
|
| 92 |
|
| 93 |
class PolymorphicInlineFormSetView(PolymorphicFormSetMixin, extra_views.InlineFormSetView): |
| 94 |
""" |
| 95 |
A view that displays a single polymorphic formset - with one parent object. |
| 96 |
This is a variation of the :mod:`extra_views` package classes for django-polymorphic. |
| 97 |
|
| 98 |
.. code-block:: python |
| 99 |
|
| 100 |
from polymorphic.formsets import PolymorphicFormSetChild |
| 101 |
|
| 102 |
|
| 103 |
class OrderItemsView(PolymorphicInlineFormSetView): |
| 104 |
model = Order |
| 105 |
inline_model = Item |
| 106 |
formset_children = [ |
| 107 |
PolymorphicFormSetChild(ItemSubclass1), |
| 108 |
PolymorphicFormSetChild(ItemSubclass2), |
| 109 |
] |
| 110 |
""" |
| 111 |
|
| 112 |
formset_class: type[BasePolymorphicInlineFormSet] = BasePolymorphicInlineFormSet |
| 113 |
|
| 114 |
|
| 115 |
class PolymorphicInlineFormSet(PolymorphicFormSetMixin, extra_views.InlineFormSetFactory): |
| 116 |
""" |
| 117 |
An inline to add to the ``inlines`` of |
| 118 |
the :class:`~extra_views.advanced.CreateWithInlinesView` |
| 119 |
and :class:`~extra_views.advanced.UpdateWithInlinesView` class. |
| 120 |
|
| 121 |
.. code-block:: python |
| 122 |
|
| 123 |
from polymorphic.formsets import PolymorphicFormSetChild |
| 124 |
|
| 125 |
|
| 126 |
class ItemsInline(PolymorphicInlineFormSet): |
| 127 |
model = Item |
| 128 |
formset_children = [ |
| 129 |
PolymorphicFormSetChild(ItemSubclass1), |
| 130 |
PolymorphicFormSetChild(ItemSubclass2), |
| 131 |
] |
| 132 |
|
| 133 |
|
| 134 |
class OrderCreateView(CreateWithInlinesView): |
| 135 |
model = Order |
| 136 |
inlines = [ItemsInline] |
| 137 |
|
| 138 |
def get_success_url(self): |
| 139 |
return self.object.get_absolute_url() |
| 140 |
|
| 141 |
""" |
| 142 |
|
| 143 |
formset_class: type[BasePolymorphicInlineFormSet] = BasePolymorphicInlineFormSet |