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
Compare revisions
c5a5f75684bdfccfabf223d7c68f81250c5ab244 to b7e136716ecb183eeb4a86a252a371a615b7c299
Compare revisions
Changes are shown as if the
source
revision was being merged into the
target
revision.
Learn more about comparing revisions.
Source
AlekSIS/libs/django-forms-as-jsonschema
Select target project
No results found
b7e136716ecb183eeb4a86a252a371a615b7c299
Select Git revision
Branches
11-create-model-for-instances-of-a-form
main
renovate/django-5.x
renovate/pytest-8.x
4 results
Swap
Target
AlekSIS/libs/django-forms-as-jsonschema
Select target project
AlekSIS/libs/django-forms-as-jsonschema
1 result
c5a5f75684bdfccfabf223d7c68f81250c5ab244
Select Git revision
Branches
11-create-model-for-instances-of-a-form
main
renovate/django-5.x
renovate/pytest-8.x
4 results
Show changes
Only incoming changes from source
Include changes to target since source was created
Compare
Commits on Source (2)
Create JSONSchemaFormMixin
· 97c6da5b
Julian
authored
2 years ago
97c6da5b
Create schemas automatically
· b7e13671
Julian
authored
2 years ago
b7e13671
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
django_forms_as_jsonschema/forms.py
+11
-0
11 additions, 0 deletions
django_forms_as_jsonschema/forms.py
django_forms_as_jsonschema/schema.py
+170
-0
170 additions, 0 deletions
django_forms_as_jsonschema/schema.py
pyproject.toml
+1
-0
1 addition, 0 deletions
pyproject.toml
with
182 additions
and
0 deletions
django_forms_as_jsonschema/forms.py
0 → 100644
View file @
b7e13671
from
django.forms
import
Form
from
django_forms_as_jsonschema.schema
import
Schema
class
JSONSchemaFormMixin
:
def
as_jsonschema
(
self
:
Form
)
->
str
:
schema
=
Schema
()
for
name
,
field
in
self
.
fields
.
items
():
schema
.
add_field
(
name
,
field
)
return
schema
.
schema
This diff is collapsed.
Click to expand it.
django_forms_as_jsonschema/schema.py
0 → 100644
View file @
b7e13671
import
copy
import
json
from
django
import
forms
class
Schema
:
SCHEMA
=
{
"
$schema
"
:
"
https://json-schema.org/draft/2020-12/schema
"
,
"
type
"
:
"
object
"
,
"
properties
"
:
{},
}
def
__init__
(
self
):
self
.
schema
=
copy
.
deepcopy
(
self
.
SCHEMA
)
# example_json = {
# "type": "object",
# "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
build
(
self
):
return
json
.
dumps
(
self
.
schema
)
def
add_field
(
self
,
name
,
field
):
type_
=
"
string
"
format_
=
None
x_display
=
None
enum
=
None
# string, number, integer, boolean.
match
type
(
field
.
widget
):
case
forms
.
TextInput
:
type_
=
"
string
"
case
forms
.
NumberInput
:
type_
=
"
number
"
case
forms
.
EmailInput
:
type_
=
"
string
"
format_
=
"
email
"
case
forms
.
URLInput
:
type_
=
"
string
"
format_
=
"
url
"
case
forms
.
PasswordInput
:
type_
=
"
string
"
format_
=
"
password
"
x_display
=
"
password
"
case
forms
.
HiddenInput
:
type_
=
"
string
"
format_
=
"
hidden
"
case
forms
.
MultipleHiddenInput
:
...
case
forms
.
FileInput
:
...
case
forms
.
ClearableFileInput
:
...
case
forms
.
Textarea
:
x_display
=
"
textarea
"
case
forms
.
DateInput
:
type_
=
"
string
"
format_
=
"
date
"
case
forms
.
DateTimeInput
:
type_
=
"
string
"
format_
=
"
date-time
"
case
forms
.
TimeInput
:
type_
=
"
string
"
format_
=
"
time
"
case
forms
.
CheckboxInput
:
type_
=
"
boolean
"
case
forms
.
Select
:
type_
=
"
string
"
enum
=
[]
# Fixme: load data from widget.options(…)
case
forms
.
NullBooleanSelect
:
...
case
forms
.
SelectMultiple
:
...
case
forms
.
RadioSelect
:
...
case
forms
.
CheckboxSelectMultiple
:
...
case
forms
.
SplitDateTimeWidget
:
...
case
forms
.
SplitHiddenDateTimeWidget
:
...
case
forms
.
SelectDateWidget
:
...
self
.
schema
[
"
properties
"
][
name
]
=
{
"
type
"
:
type_
,
"
title
"
:
str
(
field
.
label
),
"
description
"
:
str
(
field
.
help_text
),
}
if
x_display
:
self
.
schema
[
"
properties
"
][
name
][
"
x-display
"
]
=
x_display
if
format_
:
self
.
schema
[
"
properties
"
][
name
][
"
format
"
]
=
format_
if
enum
:
self
.
schema
[
"
properties
"
][
name
][
"
enum
"
]
=
enum
This diff is collapsed.
Click to expand it.
pyproject.toml
View file @
b7e13671
...
@@ -6,6 +6,7 @@ authors = ["Julian Leucker <leuckerj@gmail.com>"]
...
@@ -6,6 +6,7 @@ authors = ["Julian Leucker <leuckerj@gmail.com>"]
[tool.poetry.dependencies]
[tool.poetry.dependencies]
python
=
"^3.10"
python
=
"^3.10"
Django
=
"^4.1"
[tool.poetry.dev-dependencies]
[tool.poetry.dev-dependencies]
pytest
=
"^5.2"
pytest
=
"^5.2"
...
...
This diff is collapsed.
Click to expand it.