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__ = (
|
|
|
|
'InlineFields',
|
2024-03-13 10:15:34 -04:00
|
|
|
'ObjectAttribute',
|
2024-03-12 17:02:26 -04:00
|
|
|
'TabbedFieldGroups',
|
2024-03-12 15:44:35 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-03-12 17:02:26 -04:00
|
|
|
class FieldGroup:
|
2024-03-12 15:44:35 -04:00
|
|
|
|
2024-03-12 17:02:26 -04:00
|
|
|
def __init__(self, label, *field_names):
|
2024-03-12 15:44:35 -04:00
|
|
|
self.field_names = field_names
|
|
|
|
self.label = label
|
2024-03-12 17:02:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
class InlineFields(FieldGroup):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class TabbedFieldGroups:
|
|
|
|
|
|
|
|
def __init__(self, *groups):
|
|
|
|
self.groups = [
|
|
|
|
FieldGroup(*group) for group in groups
|
|
|
|
]
|
|
|
|
|
|
|
|
# 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}',
|
|
|
|
'title': group.label,
|
|
|
|
'fields': group.field_names,
|
|
|
|
} for i, group in enumerate(self.groups, start=1)
|
|
|
|
]
|
2024-03-13 10:15:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
class ObjectAttribute:
|
|
|
|
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|