vtt/src/ttfrog/forms.py

81 lines
1.5 KiB
Python
Raw Normal View History

2025-10-04 01:26:09 -07:00
from dataclasses import dataclass, field
from functools import cached_property
2025-10-05 00:15:37 -07:00
from flask import g
2025-10-22 19:20:47 -07:00
from grung.types import BackReference, Collection, Pointer, Record, Timestamp
2025-10-04 01:26:09 -07:00
from ttfrog import schema
2025-10-22 19:20:47 -07:00
READ_ONLY_FIELD_TYPES = [Collection, Pointer, BackReference, Timestamp]
2025-10-04 01:26:09 -07:00
@dataclass
class Form:
"""
The base Form controller for the web UI.
"""
2025-10-05 00:15:37 -07:00
2025-10-04 01:26:09 -07:00
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
2025-10-22 19:20:47 -07:00
] + ["uid", "acl"]
2025-10-04 01:26:09 -07:00
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
2025-10-04 10:48:18 -07:00
self.record.author = g.user
2025-10-04 01:26:09 -07:00
return self.record
@dataclass
class Page(Form):
"""
A form for creating and updating Page records.
"""
2025-10-05 00:15:37 -07:00
2025-10-04 01:26:09 -07:00
record: schema.Page
2025-10-22 19:20:47 -07:00
@dataclass
class Wiki(Form):
"""
A form for creating and updating Wiki records.
"""
record: schema.Page
2025-10-04 01:26:09 -07:00
@dataclass
class NPC(Page):
"""
A form for creating and updating Page records.
"""
2025-10-05 00:15:37 -07:00
2025-10-04 01:26:09 -07:00
record: schema.NPC
@dataclass
class User(Page):
"""
A form for creating and updating Page records.
"""
2025-10-05 00:15:37 -07:00
2025-10-04 01:26:09 -07:00
record: schema.NPC
@dataclass
class Group(Page):
"""
A form for creating and updating Page records.
"""
2025-10-05 00:15:37 -07:00
2025-10-04 01:26:09 -07:00
record: schema.NPC