diff --git a/src/ttfrog/app.py b/src/ttfrog/app.py index de533c4..03437fc 100644 --- a/src/ttfrog/app.py +++ b/src/ttfrog/app.py @@ -44,6 +44,8 @@ ADMIN_EMAIL= THEME=default +VIEW_URI=/ + """ def __init__(self): @@ -87,18 +89,14 @@ THEME=default self.db = GrungDB.with_schema(schema, storage=MemoryStorage) else: self.db = GrungDB.with_schema( - schema, - self.path.database, - sort_keys=True, - indent=4, - separators=(',', ': ') + schema, self.path.database, sort_keys=True, indent=4, separators=(",", ": ") ) self.theme = Path(__file__).parent / "themes" / "default" self.web = Flask(self.config.NAME, template_folder=self.theme) self.web.config["SECRET_KEY"] = self.config.SECRET_KEY - self.web.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0 + self.web.config["SEND_FILE_MAX_AGE_DEFAULT"] = 0 self.web.config["DEBUG"] = True self._initialized = True @@ -107,20 +105,32 @@ THEME=default if not self._initialized: raise ApplicationNotInitializedError("This action requires the application to be initialized.") + def add_page(self, parent: schema.Page, child: schema.Page): + parent.pages.append(self.db.save(child)) + parent = self.db.save(parent) + return parent.get_child(child) + def bootstrap(self): """ Bootstrap the database entries by populating the first Page, the Admin user and the Admins group. """ self.check_state() + home = schema.Page(stub=self.config.VIEW_URI, title="Home", body="This is the home page") + npcs = schema.Page(stub="NPC", title="NPC", body="NPCs!") + sabetha = schema.Page(title="Sabetha", body="Sabetha!") + try: - about = self.db.save(schema.Page(title="About", body="About!")) + home = self.db.save(home) except UniqueConstraintError: pass try: - home = self.db.save(schema.Page(title="Home", body="This is the home page", pages=[about])) - about.parent = home - self.db.ssave(home) + npcs = self.add_page(home, npcs) + except UniqueConstraintError: + pass + + try: + sabetha = self.add_page(npcs, sabetha) except UniqueConstraintError: pass diff --git a/src/ttfrog/schema.py b/src/ttfrog/schema.py index 8127142..d6e3548 100644 --- a/src/ttfrog/schema.py +++ b/src/ttfrog/schema.py @@ -1,4 +1,4 @@ -from grung.types import Collection, Field, Record, Pointer +from grung.types import BackReference, Collection, Field, Record class User(Record): @@ -18,17 +18,35 @@ class Page(Record): def fields(cls): return [ *super().fields(), - Field("stub", unique=True), + Field("uri", unique=True), + Field("stub"), Field("title"), Field("body"), - Pointer("parent", value_type=Page), Collection("pages", Page), + BackReference("parent", value_type=Page), ] - def before_insert(self): + def before_insert(self, db): + super().before_insert(db) + if not self.stub and not self.title: raise Exception("Must provide either a stub or a title!") if not self.stub: self.stub = self.title.title().replace(" ", "") if not self.title: - self.title = self.stub.title() + self.title = self.stub + + self.uri = (self.parent.uri + "/" if self.parent and self.parent.uri != "/" else "") + self.stub + + def after_insert(self, db): + super().after_insert(db) + for child in self.pages: + obj = BackReference.dereference(child, db) + obj.uri = f"{self.uri}/{obj.stub}" + child = db.save(obj) + + def get_child(self, obj: Record): + for page in self.pages: + if page.uid == obj.uid: + return page + return None diff --git a/src/ttfrog/themes/default/base.html b/src/ttfrog/themes/default/base.html index 32519a8..6320e05 100644 --- a/src/ttfrog/themes/default/base.html +++ b/src/ttfrog/themes/default/base.html @@ -12,11 +12,12 @@