Skip to content
Snippets Groups Projects
Commit 5c05fb4e authored by Julian's avatar Julian
Browse files

Create functionality to specify a Layout for forms

parent 1298ac30
No related branches found
No related tags found
No related merge requests found
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):
...
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