haven't committed in a while oops. A fuck-ton of HTML stuff
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,142 @@
|
||||
"""
|
||||
The ``extra_views.formsets`` provides a simple way to handle formsets.
|
||||
The ``extra_views.advanced`` provides a method to combine that with a create/update form.
|
||||
|
||||
This package provides classes that support both options for polymorphic formsets.
|
||||
"""
|
||||
from __future__ import absolute_import
|
||||
|
||||
import extra_views
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
|
||||
from polymorphic.formsets import (
|
||||
BasePolymorphicInlineFormSet,
|
||||
BasePolymorphicModelFormSet,
|
||||
polymorphic_child_forms_factory,
|
||||
)
|
||||
|
||||
__all__ = (
|
||||
"PolymorphicFormSetView",
|
||||
"PolymorphicInlineFormSetView",
|
||||
"PolymorphicInlineFormSet",
|
||||
)
|
||||
|
||||
|
||||
class PolymorphicFormSetMixin(object):
|
||||
"""
|
||||
Internal Mixin, that provides polymorphic integration with the ``extra_views`` package.
|
||||
"""
|
||||
|
||||
formset_class = BasePolymorphicModelFormSet
|
||||
|
||||
#: Default 0 extra forms
|
||||
factory_kwargs = {"extra": 0}
|
||||
|
||||
#: Define the children
|
||||
# :type: list[PolymorphicFormSetChild]
|
||||
formset_children = None
|
||||
|
||||
def get_formset_children(self):
|
||||
"""
|
||||
:rtype: list[PolymorphicFormSetChild]
|
||||
"""
|
||||
if not self.formset_children:
|
||||
raise ImproperlyConfigured(
|
||||
"Define 'formset_children' as list of `PolymorphicFormSetChild`"
|
||||
)
|
||||
return self.formset_children
|
||||
|
||||
def get_formset_child_kwargs(self):
|
||||
return {}
|
||||
|
||||
def get_formset(self):
|
||||
"""
|
||||
Returns the formset class from the inline formset factory
|
||||
"""
|
||||
# Implementation detail:
|
||||
# Since `polymorphic_modelformset_factory` and `polymorphic_inlineformset_factory` mainly
|
||||
# reuse the standard factories, and then add `child_forms`, the same can be done here.
|
||||
# This makes sure the base class construction is completely honored.
|
||||
FormSet = super(PolymorphicFormSetMixin, self).get_formset()
|
||||
FormSet.child_forms = polymorphic_child_forms_factory(
|
||||
self.get_formset_children(), **self.get_formset_child_kwargs()
|
||||
)
|
||||
return FormSet
|
||||
|
||||
|
||||
class PolymorphicFormSetView(PolymorphicFormSetMixin, extra_views.ModelFormSetView):
|
||||
"""
|
||||
A view that displays a single polymorphic formset.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from polymorphic.formsets import PolymorphicFormSetChild
|
||||
|
||||
|
||||
class ItemsView(PolymorphicFormSetView):
|
||||
model = Item
|
||||
formset_children = [
|
||||
PolymorphicFormSetChild(ItemSubclass1),
|
||||
PolymorphicFormSetChild(ItemSubclass2),
|
||||
]
|
||||
|
||||
"""
|
||||
|
||||
formset_class = BasePolymorphicModelFormSet
|
||||
|
||||
|
||||
class PolymorphicInlineFormSetView(
|
||||
PolymorphicFormSetMixin, extra_views.InlineFormSetView
|
||||
):
|
||||
"""
|
||||
A view that displays a single polymorphic formset - with one parent object.
|
||||
This is a variation of the :mod:`extra_views` package classes for django-polymorphic.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from polymorphic.formsets import PolymorphicFormSetChild
|
||||
|
||||
|
||||
class OrderItemsView(PolymorphicInlineFormSetView):
|
||||
model = Order
|
||||
inline_model = Item
|
||||
formset_children = [
|
||||
PolymorphicFormSetChild(ItemSubclass1),
|
||||
PolymorphicFormSetChild(ItemSubclass2),
|
||||
]
|
||||
"""
|
||||
|
||||
formset_class = BasePolymorphicInlineFormSet
|
||||
|
||||
|
||||
class PolymorphicInlineFormSet(
|
||||
PolymorphicFormSetMixin, extra_views.InlineFormSetFactory
|
||||
):
|
||||
"""
|
||||
An inline to add to the ``inlines`` of
|
||||
the :class:`~extra_views.advanced.CreateWithInlinesView`
|
||||
and :class:`~extra_views.advanced.UpdateWithInlinesView` class.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from polymorphic.formsets import PolymorphicFormSetChild
|
||||
|
||||
|
||||
class ItemsInline(PolymorphicInlineFormSet):
|
||||
model = Item
|
||||
formset_children = [
|
||||
PolymorphicFormSetChild(ItemSubclass1),
|
||||
PolymorphicFormSetChild(ItemSubclass2),
|
||||
]
|
||||
|
||||
|
||||
class OrderCreateView(CreateWithInlinesView):
|
||||
model = Order
|
||||
inlines = [ItemsInline]
|
||||
|
||||
def get_success_url(self):
|
||||
return self.object.get_absolute_url()
|
||||
|
||||
"""
|
||||
|
||||
formset_class = BasePolymorphicInlineFormSet
|
||||
@@ -0,0 +1,35 @@
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
|
||||
|
||||
def get_polymorphic_base_content_type(obj):
|
||||
"""
|
||||
Helper function to return the base polymorphic content type id. This should used with django-guardian and the
|
||||
GUARDIAN_GET_CONTENT_TYPE option.
|
||||
|
||||
See the django-guardian documentation for more information:
|
||||
|
||||
https://django-guardian.readthedocs.io/en/latest/configuration.html#guardian-get-content-type
|
||||
"""
|
||||
if hasattr(obj, "polymorphic_model_marker"):
|
||||
try:
|
||||
superclasses = list(obj.__class__.mro())
|
||||
except TypeError:
|
||||
# obj is an object so mro() need to be called with the obj.
|
||||
superclasses = list(obj.__class__.mro(obj))
|
||||
|
||||
polymorphic_superclasses = list()
|
||||
for sclass in superclasses:
|
||||
if hasattr(sclass, "polymorphic_model_marker"):
|
||||
polymorphic_superclasses.append(sclass)
|
||||
|
||||
# PolymorphicMPTT adds an additional class between polymorphic and base class.
|
||||
if hasattr(obj, "can_have_children"):
|
||||
root_polymorphic_class = polymorphic_superclasses[-3]
|
||||
else:
|
||||
root_polymorphic_class = polymorphic_superclasses[-2]
|
||||
ctype = ContentType.objects.get_for_model(root_polymorphic_class)
|
||||
|
||||
else:
|
||||
ctype = ContentType.objects.get_for_model(obj)
|
||||
|
||||
return ctype
|
||||
Reference in New Issue
Block a user