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

Adding request context to scribble context #115

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions scribbler/templatetags/scribbler_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def render(self, context):
scribble_template = template.engines['django'].from_string(self.raw)
else:
scribble_template = template.Template(self.raw)
scribble_context = build_scribble_context(scribble)
scribble_context = build_scribble_context(scribble, context)
content = scribble_template.render(scribble_context, request)
wrapper_template = template.loader.get_template('scribbler/scribble-wrapper.html')
context['scribble'] = scribble
Expand Down Expand Up @@ -175,7 +175,7 @@ def scribble_field(context, model_instance, field_name):
scribble_template = template.engines['django'].from_string(field_value)
else:
scribble_template = template.Template(field_value)
scribble_context = build_scribble_context(None)
scribble_context = build_scribble_context(None, context)
rendered_content = scribble_template.render(scribble_context, request)
context['rendered_scribble'] = rendered_content

Expand Down
6 changes: 3 additions & 3 deletions scribbler/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
from .utils import get_variables


def build_scribble_context(scribble):
def build_scribble_context(scribble, context={}):
"Create context for rendering a scribble or scribble preview."
context = {
context.update({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is modifying a mutable default argument. This has the potential to leak state between pages/requests/templates.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential alternative:

def build_scribble_context(scribble, extra_context=None):
    "Create context for rendering a scribble or scribble preview."
    context = {
        'scribble': scribble,
    }

    if extra_context:
        context.update(extra_context)

    return context

'scribble': scribble,
}
})

return context

Expand Down