From 9d5c7799ef66f479ead89ff5bfa05d4d1ae62b22 Mon Sep 17 00:00:00 2001 From: laund Date: Wed, 13 Nov 2024 07:04:30 +0100 Subject: [PATCH] Ensure fixture context always contains the key template_inject with a dict value (#944) * context always has a template_inject dict, remove now-redundant checks * fix tests on windows, and fix template test --------- Co-authored-by: Laurin Schmidt --- py4web/core.py | 8 +++----- py4web/utils/auth.py | 2 +- tests/test_action.py | 14 +++++++++++++- tests/test_template.py | 2 +- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/py4web/core.py b/py4web/core.py index a6f8c5d5..bbd86231 100644 --- a/py4web/core.py +++ b/py4web/core.py @@ -519,10 +519,7 @@ def on_success(self, context): output = context["output"] flash = self.local.flash or "" if isinstance(output, dict): - if "template_inject" in context: - context["template_inject"]["flash"] = flash - else: - context["template_inject"] = dict(flash=flash) + context["template_inject"]["flash"] = flash elif self.local.flash is not None: response.headers.setdefault("component-flash", json.dumps(flash)) @@ -616,7 +613,7 @@ def on_success(self, context): ctx = dict(request=request) ctx.update(HELPERS) ctx.update(URL=URL) - ctx.update(context.get("template_inject", {})) + ctx.update(context["template_inject"]) ctx.update(output) ctx["__vars__"] = output app_folder = os.path.join(os.environ["PY4WEB_APPS_FOLDER"], request.app_name) @@ -998,6 +995,7 @@ def wrapper(*args, **kwargs): "output": None, "exception": None, "processed": processed, + "template_inject": {}, } try: for fixture in fixtures: diff --git a/py4web/utils/auth.py b/py4web/utils/auth.py index b79a3f48..345a7974 100644 --- a/py4web/utils/auth.py +++ b/py4web/utils/auth.py @@ -348,7 +348,7 @@ def deny_action(self, action_name): def on_success(self, context): if self.inject: - context["template_inject"] = {"user": self.get_user()} + context["template_inject"]["user"] = self.get_user() def define_tables(self): """Defines the auth_user table""" diff --git a/tests/test_action.py b/tests/test_action.py index 1e229eb2..34917fef 100644 --- a/tests/test_action.py +++ b/tests/test_action.py @@ -2,6 +2,7 @@ import copy import multiprocessing import os +import sys import threading import time import unittest @@ -18,7 +19,18 @@ ) SECRET = str(uuid.uuid4()) -db = DAL("sqlite://storage_%s" % uuid.uuid4(), folder="/tmp/") +if sys.platform == "win32": + path = "./tmp/" +else: + path = "/tmp/" + +try: + os.mkdir(path) +except Exception: + pass +with open(path + "sql.log", "w"): + pass +db = DAL("sqlite://storage_%s" % uuid.uuid4(), folder=path) db.define_table("thing", Field("name")) session = Session(secret=SECRET) cache = Cache() diff --git a/tests/test_template.py b/tests/test_template.py index ff996493..025608ed 100644 --- a/tests/test_template.py +++ b/tests/test_template.py @@ -9,7 +9,7 @@ class TemplateTest(unittest.TestCase): def test_template(self): t = Template("index.html", path=PATH) - context = dict(output=dict(n=3)) + context = dict(output=dict(n=3), template_inject={}) t.on_success(context) output = context["output"] self.assertEqual(output, "0,1,2.\n")