Skip to content
Snippets Groups Projects
Commit 5634007d authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Merge branch 'feature/extensible-form' into 'master'

Add extensible form which allows to add elements to django-material form layouts

See merge request AlekSIS!180
parents 76241863 d48104e4
No related branches found
Tags 2.0rc7
1 merge request!180Add extensible form which allows to add elements to django-material form layouts
Pipeline #1005 failed
......@@ -4,9 +4,11 @@ from typing import Any, Callable, Optional
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.db.models import QuerySet
from django.forms.models import ModelFormMetaclass, ModelForm
from easyaudit.models import CRUDEvent
from jsonstore.fields import JSONField, JSONFieldMixin
from material.base import LayoutNode, Layout
class CRUDMixin(models.Model):
......@@ -170,3 +172,51 @@ class ExtensibleModel(CRUDMixin):
class PureDjangoModel(object):
""" No-op mixin to mark a model as deliberately not using ExtensibleModel """
pass
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(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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment