Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
django-forms-as-jsonschema
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
Model registry
Monitor
Service Desk
Analyze
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®
Libraries
django-forms-as-jsonschema
Commits
5c05fb4e
Commit
5c05fb4e
authored
2 years ago
by
Julian
Browse files
Options
Downloads
Patches
Plain Diff
Create functionality to specify a Layout for forms
parent
1298ac30
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
django_forms_as_jsonschema/layout.py
+72
-0
72 additions, 0 deletions
django_forms_as_jsonschema/layout.py
with
72 additions
and
0 deletions
django_forms_as_jsonschema/layout.py
0 → 100644
+
72
−
0
View file @
5c05fb4e
import
uuid
from
abc
import
ABC
from
dataclasses
import
dataclass
from
typing
import
Union
from
django.utils.text
import
slugify
class
LayoutNode
(
ABC
):
def
__init__
(
self
,
*
elements
):
self
.
elements
=
elements
def
build_schema
(
self
,
schema
,
form_fields
)
->
dict
:
props
=
{}
for
field
in
self
.
elements
:
if
isinstance
(
field
,
LayoutNode
):
built_schema
=
field
.
build_schema
(
schema
,
form_fields
)
props
.
update
(
built_schema
[
"
properties
"
])
else
:
props
[
field
]
=
schema
.
generate_field
(
form_fields
[
field
])
return
{
"
properties
"
:
props
}
@dataclass
class
_Section
:
codename
:
str
title
:
str
description
:
str
=
None
class
Row
(
LayoutNode
):
def
build_schema
(
self
,
schema
,
form_fields
):
row_name
=
"
row-
"
+
str
(
uuid
.
uuid4
())
fields
=
super
().
build_schema
(
schema
,
form_fields
)[
"
properties
"
]
for
k
in
fields
.
keys
():
fields
[
k
][
"
x-options
"
]
=
{
"
fieldColProps
"
:
{
"
cols
"
:
12
,
"
md
"
:
""
}
}
return
{
"
properties
"
:
{
row_name
:
{
"
x-cols
"
:
12
,
"
properties
"
:
fields
}
}
}
class
Fieldset
(
LayoutNode
):
def
__init__
(
self
,
name
:
Union
[
tuple
[
str
,
str
],
str
],
*
elements
):
super
().
__init__
(
*
elements
)
codename
=
str
(
slugify
(
name
))
if
type
(
name
)
==
tuple
:
self
.
section
=
_Section
(
codename
,
*
map
(
str
,
name
))
else
:
self
.
section
=
_Section
(
codename
,
str
(
name
))
def
build_schema
(
self
,
schema
,
form_fields
)
->
dict
:
section
=
schema
.
generate_section
(
self
.
section
)
section
[
"
properties
"
].
update
(
super
().
build_schema
(
schema
,
form_fields
)[
"
properties
"
])
return
{
"
properties
"
:
{
self
.
section
.
codename
:
section
}
}
class
Layout
(
LayoutNode
):
...
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