diff --git a/aleksis/core/forms.py b/aleksis/core/forms.py index c333ad88e24c8a228774458ea8b66287125da8f6..501e2691b03f8e92d4cce227c32238c2cc98214b 100644 --- a/aleksis/core/forms.py +++ b/aleksis/core/forms.py @@ -5,65 +5,16 @@ from django import forms from django.contrib.auth import get_user_model from django.contrib.contenttypes.models import ContentType from django.core.exceptions import ValidationError -from django.forms.models import ModelFormMetaclass from django.utils import timezone from django.utils.translation import ugettext_lazy as _ from django_select2.forms import ModelSelect2MultipleWidget, Select2Widget from material import Layout, Fieldset, Row -from material.base import LayoutNode +from .mixins import ExtensibleForm from .models import Group, Person, School, SchoolTerm, Announcement, AnnouncementRecipient -class ExtensibleFormMetaclass(ModelFormMetaclass): - def __new__(mcs, name, bases, dct): - x = super().__new__(mcs, name, bases, dct) - - if hasattr(x, "layout"): - base_layout = x.layout.elements - else: - base_layout = [] - - x.base_layout = base_layout - x.layout = Layout(*base_layout) - - return x - - -class ExtensibleForm(forms.ModelForm, metaclass=ExtensibleFormMetaclass): - """ Base model for extensible forms - - This mixin adds functionality which allows - - apps to add layout nodes to the layout used by django-material - - Add layout nodes - ================ - - ``` - from material import Fieldset - - from aleksis.core.forms import ExampleForm - - node = Fieldset("field_name") - ExampleForm.add_node_to_layout(node) - ``` - - """ - - @classmethod - def add_node_to_layout(cls, node: LayoutNode): - """ - Add a node to `layout` attribute - - :param node: django-material layout node (Fieldset, Row etc.) - :type node: LayoutNode - """ - - cls.base_layout.append(node) - cls.layout = Layout(*cls.base_layout) - - class PersonAccountForm(forms.ModelForm): class Meta: model = Person