2024-03-12 17:02:26 -04:00
|
|
|
import random
|
|
|
|
import string
|
|
|
|
from functools import cached_property
|
|
|
|
|
2024-03-12 15:44:35 -04:00
|
|
|
__all__ = (
|
2024-03-15 12:59:42 -04:00
|
|
|
'FieldSet',
|
2024-03-12 15:44:35 -04:00
|
|
|
'InlineFields',
|
2024-03-13 10:15:34 -04:00
|
|
|
'ObjectAttribute',
|
2024-03-15 12:59:42 -04:00
|
|
|
'TabbedGroups',
|
2024-03-12 15:44:35 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-03-15 12:59:42 -04:00
|
|
|
class FieldSet:
|
|
|
|
"""
|
|
|
|
A generic grouping of fields, with an optional name. Each field will be rendered
|
|
|
|
on its own row under the heading (name).
|
|
|
|
"""
|
|
|
|
def __init__(self, *fields, name=None):
|
|
|
|
self.fields = fields
|
|
|
|
self.name = name
|
2024-03-12 17:02:26 -04:00
|
|
|
|
|
|
|
|
2024-03-15 12:59:42 -04:00
|
|
|
class InlineFields:
|
|
|
|
"""
|
|
|
|
A set of fields rendered inline (side-by-side) with a shared label; typically nested within a FieldSet.
|
|
|
|
"""
|
|
|
|
def __init__(self, *fields, label=None):
|
|
|
|
self.fields = fields
|
|
|
|
self.label = label
|
2024-03-12 17:02:26 -04:00
|
|
|
|
|
|
|
|
2024-03-15 12:59:42 -04:00
|
|
|
class TabbedGroups:
|
|
|
|
"""
|
|
|
|
Two or more groups of fields (FieldSets) arranged under tabs among which the user can navigate.
|
|
|
|
"""
|
2024-03-18 15:08:28 -04:00
|
|
|
def __init__(self, *fieldsets):
|
|
|
|
for fs in fieldsets:
|
|
|
|
if not fs.name:
|
|
|
|
raise ValueError(f"Grouped fieldset {fs} must have a name.")
|
|
|
|
self.groups = fieldsets
|
2024-03-12 17:02:26 -04:00
|
|
|
|
|
|
|
# Initialize a random ID for the group (for tab selection)
|
|
|
|
self.id = ''.join(
|
|
|
|
random.choice(string.ascii_lowercase + string.digits) for _ in range(8)
|
|
|
|
)
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def tabs(self):
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
'id': f'{self.id}_{i}',
|
2024-03-15 12:59:42 -04:00
|
|
|
'title': group.name,
|
|
|
|
'fields': group.fields,
|
2024-03-12 17:02:26 -04:00
|
|
|
} for i, group in enumerate(self.groups, start=1)
|
|
|
|
]
|
2024-03-13 10:15:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
class ObjectAttribute:
|
2024-03-15 12:59:42 -04:00
|
|
|
"""
|
|
|
|
Renders the value for a specific attribute on the form's instance.
|
|
|
|
"""
|
2024-03-13 10:15:34 -04:00
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|