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
1298ac30
Commit
1298ac30
authored
2 years ago
by
Julian
Browse files
Options
Downloads
Patches
Plain Diff
Move creation and addition of fields and sections to separate methods
parent
1ba1d2c8
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/jsonschema.py
+36
-59
36 additions, 59 deletions
django_forms_as_jsonschema/jsonschema.py
with
36 additions
and
59 deletions
django_forms_as_jsonschema/jsonschema.py
+
36
−
59
View file @
1298ac30
from
django
import
forms
from
django
import
forms
from
django_forms_as_jsonschema.layout
import
_Section
class
JSONSchema
:
class
JSONSchema
:
...
@@ -10,63 +12,8 @@ class JSONSchema:
...
@@ -10,63 +12,8 @@ class JSONSchema:
"
properties
"
:
{},
"
properties
"
:
{},
}
}
# example_json = {
@staticmethod
# "type": "object",
def
generate_field
(
field
):
# "properties": {
# "stringProp": {
# "type": "string",
# "title": "I'm a string",
# "description": "This description is used as a help message."
# },
# "stringTextareaProp": {
# "type": "string",
# "title": "I'm a string in a textarea",
# "x-display": "textarea"
# },
# "numberProp": {
# "type": "number",
# "title": "I'm a number"
# },
# "integerProp": {
# "type": "integer",
# "title": "I'm an integer"
# },
# "integerSliderProp": {
# "type": "integer",
# "title": "I'm an integer in a slider",
# "x-display": "slider",
# "minimum": 0,
# "maximum": 5
# },
# "booleanProp": {
# "type": "boolean",
# "title": "I'm a boolean",
# "description": "This description is used as a help message."
# },
# "booleanSwitchProp": {
# "type": "boolean",
# "title": "I'm a boolean with switch display",
# "x-display": "switch",
# "description": "This description is used as a help message."
# },
# "stringArrayProp": {
# "type": "array",
# "title": "I'm an array of strings",
# "items": {
# "type": "string"
# }
# },
# "integerArrayProp": {
# "type": "array",
# "title": "I'm an array of integers",
# "items": {
# "type": "integer"
# }
# }
# }
# }
def
add_field
(
self
,
name
,
field
):
new_field
=
{
new_field
=
{
"
type
"
:
"
string
"
,
"
type
"
:
"
string
"
,
"
title
"
:
str
(
field
.
label
or
""
),
"
title
"
:
str
(
field
.
label
or
""
),
...
@@ -162,6 +109,36 @@ class JSONSchema:
...
@@ -162,6 +109,36 @@ class JSONSchema:
...
...
else
:
else
:
print
(
field
,
type
(
field
),
type
(
field
.
widget
))
print
(
"
[Django-forms-as-jsonschema] Unsupported field/widget detected:
"
)
print
(
f
"
{
field
=
}
,
{
type
(
field
)
=
}
,
{
type
(
field
.
widget
)
=
}
"
)
return
new_field
def
add_field
(
self
,
name
,
field
,
metadata
:
dict
=
None
):
new_field
=
self
.
generate_field
(
field
)
if
metadata
and
metadata
.
get
(
"
section_name
"
)
and
self
.
schema
[
"
properties
"
].
get
(
metadata
[
"
section_name
"
]
)
is
not
None
:
self
.
schema
[
"
properties
"
][
metadata
[
"
section_name
"
]][
"
properties
"
]
=
\
self
.
schema
[
"
properties
"
][
metadata
[
"
section_name
"
]][
"
properties
"
]
or
{}
self
.
schema
[
"
properties
"
][
metadata
[
"
section_name
"
]][
"
properties
"
][
name
]
=
new_field
else
:
self
.
schema
[
"
properties
"
][
name
]
=
new_field
@staticmethod
def
generate_section
(
section
:
_Section
):
sec
=
{
"
title
"
:
section
.
title
,
"
type
"
:
"
object
"
,
"
properties
"
:
{}
}
if
section
.
description
:
sec
[
"
description
"
]
=
section
.
description
return
sec
def
add_section
(
self
,
section
:
_Section
):
self
.
schema
[
"
properties
"
][
section
.
codename
]
=
self
.
generate_section
(
section
)
self
.
schema
[
"
properties
"
][
name
]
=
new_field
def
update_properties
(
self
,
props
:
dict
):
self
.
schema
[
"
properties
"
].
update
(
props
)
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