70 lines
1.4 KiB
Python
70 lines
1.4 KiB
Python
from dataclasses import dataclass, field
|
|
from functools import cached_property
|
|
|
|
from grung.types import BackReference, Collection, Pointer, Record
|
|
|
|
from ttfrog import schema
|
|
|
|
READ_ONLY_FIELD_TYPES = [Collection, Pointer, BackReference]
|
|
|
|
|
|
@dataclass
|
|
class Form:
|
|
"""
|
|
The base Form controller for the web UI.
|
|
"""
|
|
record: Record
|
|
data: field(default_factory=dict)
|
|
|
|
@cached_property
|
|
def read_only(self) -> set:
|
|
return [
|
|
name for (name, attr) in self.record._metadata.fields.items() if type(attr) in READ_ONLY_FIELD_TYPES
|
|
] + ["uid"]
|
|
|
|
def prepare(self):
|
|
for key, value in self.data.items():
|
|
|
|
# filter out fields that cannot be set by the user
|
|
if key in self.read_only:
|
|
continue
|
|
|
|
self.record[key] = value
|
|
return self.record
|
|
|
|
|
|
@dataclass
|
|
class Page(Form):
|
|
"""
|
|
A form for creating and updating Page records.
|
|
"""
|
|
record: schema.Page
|
|
|
|
@cached_property
|
|
def read_only(self) -> set:
|
|
return set(list(super().read_only) + ["stub"])
|
|
|
|
|
|
@dataclass
|
|
class NPC(Page):
|
|
"""
|
|
A form for creating and updating Page records.
|
|
"""
|
|
record: schema.NPC
|
|
|
|
|
|
@dataclass
|
|
class User(Page):
|
|
"""
|
|
A form for creating and updating Page records.
|
|
"""
|
|
record: schema.NPC
|
|
|
|
|
|
@dataclass
|
|
class Group(Page):
|
|
"""
|
|
A form for creating and updating Page records.
|
|
"""
|
|
record: schema.NPC
|