Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AlekSIS-Core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
AlekSIS®
Official
AlekSIS-Core
Commits
47530bfd
Verified
Commit
47530bfd
authored
5 years ago
by
Jonathan Weth
Browse files
Options
Downloads
Patches
Plain Diff
Add extensible form which allows to add elements to django-material form layouts
parent
671c79f8
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!184
Improve announcement queries
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
aleksis/core/forms.py
+50
-0
50 additions, 0 deletions
aleksis/core/forms.py
with
50 additions
and
0 deletions
aleksis/core/forms.py
+
50
−
0
View file @
47530bfd
...
...
@@ -5,15 +5,65 @@ 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
.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
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment