Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure fixture context always contains the key template_inject with a dict value #944

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions py4web/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,10 +514,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 @@ -611,7 +608,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 @@ -993,6 +990,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 @@ -339,7 +339,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")
Loading