Skip to content

Commit

Permalink
Ensure fixture context always contains the key template_inject with …
Browse files Browse the repository at this point in the history
…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 <[email protected]>
  • Loading branch information
laundmo and Laurin Schmidt authored Nov 13, 2024
1 parent cedc5d6 commit 9d5c779
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
8 changes: 3 additions & 5 deletions py4web/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -998,6 +995,7 @@ def wrapper(*args, **kwargs):
"output": None,
"exception": None,
"processed": processed,
"template_inject": {},
}
try:
for fixture in fixtures:
Expand Down
2 changes: 1 addition & 1 deletion py4web/utils/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
14 changes: 13 additions & 1 deletion tests/test_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import copy
import multiprocessing
import os
import sys
import threading
import time
import unittest
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 9d5c779

Please sign in to comment.