From b3432280720d596bbc2d1b2cc3cd304374527e52 Mon Sep 17 00:00:00 2001 From: Hay1tsme Date: Mon, 20 Feb 2023 21:55:12 -0500 Subject: [PATCH] refactor template directory to be artemis root dir --- core/frontend.py | 27 +++++++++++++++---------- core/frontend/gate/create.jinja | 2 +- core/frontend/gate/gate.jinja | 4 ++-- core/frontend/index.jinja | 2 +- core/frontend/user/index.jinja | 2 +- core/frontend/widgets/topbar.jinja | 9 ++++++++- titles/wacca/frontend.py | 8 +++++++- titles/wacca/frontend/wacca_index.jinja | 4 ++++ 8 files changed, 40 insertions(+), 18 deletions(-) create mode 100644 titles/wacca/frontend/wacca_index.jinja diff --git a/core/frontend.py b/core/frontend.py index 554df13..780698e 100644 --- a/core/frontend.py +++ b/core/frontend.py @@ -1,5 +1,5 @@ import logging, coloredlogs -from typing import Any, Dict +from typing import Any, Dict, List from twisted.web import resource from twisted.web.util import redirectTo from twisted.web.http import Request @@ -12,7 +12,6 @@ from core.data import Data from core.utils import Utils class FrontendServlet(resource.Resource): - children: Dict[str, Any] = {} def getChild(self, name: bytes, request: Request): self.logger.debug(f"{request.getClientIP()} -> {name.decode()}") if name == b'': @@ -24,7 +23,9 @@ class FrontendServlet(resource.Resource): log_fmt_str = "[%(asctime)s] Frontend | %(levelname)s | %(message)s" log_fmt = logging.Formatter(log_fmt_str) self.logger = logging.getLogger("frontend") - self.environment = jinja2.Environment(loader=jinja2.FileSystemLoader("core/frontend")) + self.environment = jinja2.Environment(loader=jinja2.FileSystemLoader(".")) + self.game_list: List[Dict[str, str]] = [] + self.children: Dict[str, Any] = {} fileHandler = TimedRotatingFileHandler("{0}/{1}.log".format(self.config.server.log_dir, "frontend"), when="d", backupCount=10) fileHandler.setFormatter(log_fmt) @@ -43,10 +44,13 @@ class FrontendServlet(resource.Resource): for game_dir, game_mod in games.items(): if hasattr(game_mod, "frontend"): try: - fe_game.putChild(game_dir.encode(), game_mod.frontend(cfg, self.environment, config_dir)) + game_fe = game_mod.frontend(cfg, self.environment, config_dir) + self.game_list.append({"url": game_dir, "name": game_fe.nav_name}) + fe_game.putChild(game_dir.encode(), game_fe) except: raise - + + self.environment.globals["game_list"] = self.game_list self.putChild(b"gate", FE_Gate(cfg, self.environment)) self.putChild(b"user", FE_User(cfg, self.environment)) self.putChild(b"game", fe_game) @@ -55,8 +59,8 @@ class FrontendServlet(resource.Resource): def render_GET(self, request): self.logger.debug(f"{request.getClientIP()} -> {request.uri.decode()}") - template = self.environment.get_template("index.jinja") - return template.render(server_name=self.config.server.name, title=self.config.server.name).encode("utf-16") + template = self.environment.get_template("core/frontend/index.jinja") + return template.render(server_name=self.config.server.name, title=self.config.server.name, game_list=self.game_list).encode("utf-16") class FE_Base(resource.Resource): """ @@ -65,11 +69,12 @@ class FE_Base(resource.Resource): It is expected that game implementations of this class overwrite many of these """ isLeaf = True - def __init__(self, cfg: CoreConfig, environment: jinja2.Environment, cfg_dir: str = None) -> None: + def __init__(self, cfg: CoreConfig, environment: jinja2.Environment) -> None: self.core_config = cfg self.data = Data(cfg) self.logger = logging.getLogger('frontend') self.environment = environment + self.nav_name = "nav_name" class FE_Gate(FE_Base): def render_GET(self, request: Request): @@ -86,7 +91,7 @@ class FE_Gate(FE_Base): else: err = 0 - template = self.environment.get_template("gate/gate.jinja") + template = self.environment.get_template("core/frontend/gate/gate.jinja") return template.render(title=f"{self.core_config.server.name} | Login Gate", error=err).encode("utf-16") def render_POST(self, request: Request): @@ -153,12 +158,12 @@ class FE_Gate(FE_Base): ac = request.args[b'ac'][0].decode() - template = self.environment.get_template("gate/create.jinja") + template = self.environment.get_template("core/frontend/gate/create.jinja") return template.render(title=f"{self.core_config.server.name} | Create User", code=ac).encode("utf-16") class FE_User(FE_Base): def render_GET(self, request: Request): - template = self.environment.get_template("user/index.jinja") + template = self.environment.get_template("core/frontend/user/index.jinja") return template.render().encode("utf-16") if b'session' not in request.cookies: return redirectTo(b"/gate", request) diff --git a/core/frontend/gate/create.jinja b/core/frontend/gate/create.jinja index f5b78ae..f8b5e51 100644 --- a/core/frontend/gate/create.jinja +++ b/core/frontend/gate/create.jinja @@ -1,4 +1,4 @@ -{% extends "index.jinja" %} +{% extends "core/frontend/index.jinja" %} {% block content %}

Create User

diff --git a/core/frontend/gate/gate.jinja b/core/frontend/gate/gate.jinja index 85b3ea5..760fbab 100644 --- a/core/frontend/gate/gate.jinja +++ b/core/frontend/gate/gate.jinja @@ -1,7 +1,7 @@ -{% extends "index.jinja" %} +{% extends "core/frontend/index.jinja" %} {% block content %}

Gate

-{% include "widgets/err_banner.jinja" %} +{% include "core/frontend/widgets/err_banner.jinja" %}

diff --git a/core/frontend/index.jinja b/core/frontend/index.jinja index c70688b..7e4a1ca 100644 --- a/core/frontend/index.jinja +++ b/core/frontend/index.jinja @@ -80,7 +80,7 @@ - {% include "widgets/topbar.jinja" %} + {% include "core/frontend/widgets/topbar.jinja" %} {% block content %}

{{ server_name }}

{% endblock content %} diff --git a/core/frontend/user/index.jinja b/core/frontend/user/index.jinja index dc62294..eabdd18 100644 --- a/core/frontend/user/index.jinja +++ b/core/frontend/user/index.jinja @@ -1,4 +1,4 @@ -{% extends "index.jinja" %} +{% extends "core/frontend/index.jinja" %} {% block content %}

testing

{% endblock content %} \ No newline at end of file diff --git a/core/frontend/widgets/topbar.jinja b/core/frontend/widgets/topbar.jinja index 5dbba5d..6bef3e3 100644 --- a/core/frontend/widgets/topbar.jinja +++ b/core/frontend/widgets/topbar.jinja @@ -1,6 +1,13 @@
Navigation
-
+
+   + {% for game in game_list %} +   + {% endfor %} +
+
+
\ No newline at end of file diff --git a/titles/wacca/frontend.py b/titles/wacca/frontend.py index a61d5f4..b7232b8 100644 --- a/titles/wacca/frontend.py +++ b/titles/wacca/frontend.py @@ -6,6 +6,7 @@ from core.frontend import FE_Base from core.config import CoreConfig from titles.wacca.database import WaccaData from titles.wacca.config import WaccaConfig +from titles.wacca.const import WaccaConstants class WaccaFrontend(FE_Base): def __init__(self, cfg: CoreConfig, environment: jinja2.Environment, cfg_dir: str) -> None: @@ -13,6 +14,11 @@ class WaccaFrontend(FE_Base): self.data = WaccaData(cfg) self.game_cfg = WaccaConfig() self.game_cfg.update(yaml.safe_load(open(f"{cfg_dir}/wacca.yaml"))) + self.nav_name = "Wacca" def render_GET(self, request: Request) -> bytes: - return b"" + template = self.environment.get_template("titles/wacca/frontend/wacca_index.jinja") + return template.render( + title=f"{self.core_config.server.name} | {self.nav_name}", + game_list=self.environment.globals["game_list"] + ).encode("utf-16") diff --git a/titles/wacca/frontend/wacca_index.jinja b/titles/wacca/frontend/wacca_index.jinja new file mode 100644 index 0000000..6a5f046 --- /dev/null +++ b/titles/wacca/frontend/wacca_index.jinja @@ -0,0 +1,4 @@ +{% extends "core/frontend/index.jinja" %} +{% block content %} +

Wacca

+{% endblock content %} \ No newline at end of file