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

html_head signal not fired for non-event pages in backend #2988

Open
henryk opened this issue Dec 20, 2022 · 3 comments
Open

html_head signal not fired for non-event pages in backend #2988

henryk opened this issue Dec 20, 2022 · 3 comments

Comments

@henryk
Copy link
Contributor

henryk commented Dec 20, 2022

Problem and impact

I'm developing a plugin and want to include additional scripts and styles in the backend, on all pages. The documentation lists two signal with virtually identical description:

The issue is here

if hasattr(request, 'event') and request.user.is_authenticated:
and has been present ever since the signal was introduced in 2015. Compared to that, the html_page_start signal is implemented in
{% signal "pretix.control.signals.html_page_start" request %}
and not subject to an event restriction.

Expected behaviour

The wording "every page in the backend" in the documentation particularly suggests that these two signals would equally apply to, well, every page in the backend.
(Alternatively the documentation needs to highlight the difference. :)

Steps to reproduce

  1. Create a globally active plugin that implements pretix.control.signals.html_head and pretix.control.signals.html_page_start. Let the plugin return some dummy content.
  2. Visit /control/: The html_page_start signal is fired and the content visible at the start of the main content.
  3. Create an event and visit /control/event/...: The html_page_start and html_ head signals are fired. Content is visible both in page main and in <head>.

Screenshots

No response

Link

No response

Browser (software, desktop or mobile?) and version

No response

Operating system, dependency versions

No response

Version

No response

@raphaelm
Copy link
Member

Indeed one of them is declared to be a plain Signal and one to be an EventPluginSignal. I can't recall why that happened but I'm also not sure how careful we need to be with changing this for compatibility

@henryk
Copy link
Contributor Author

henryk commented Dec 20, 2022

Oh, you're right. I hadn't even noticed that since (at this point) I don't care about the event. I agree that sending None for a parameter that was previously documented as "will contain the event" is a no-go. And inventing a DummyEvent (like Django's AnonymousUser) is probably overkill. A compromise would be a new signal only for non-event pages?

In the mean time I do have a workaround, but it's not fit for general consumption. I'm basing this on the fact that html_head is "" in the event case.

Danger: Don't do this unless you're absolutely sure that you're the only plugin using this trick

In signals:

@receiver(html_head)
def my_html_head(sender, request=None, **kwargs):
    return "bla"

def inject_head(request):
    # This hack overrides `html_head` in backend *only* if not in event context
    from django.urls import get_script_prefix
    if not request.path.startswith(get_script_prefix() + 'control') or not hasattr(request, 'user'):
        return {}
    if request.user.is_authenticated and not hasattr(request, 'event'):
        return {"html_head": my_html_head(None, request)}
    return {}

and in PluginApp.ready():

from django.conf import settings

settings.TEMPLATES[0]['OPTIONS']['context_processors'].append('...signals.inject_head')

@raphaelm
Copy link
Member

Yeah, the underlying conceptional issue is that pretix historically only has a concept of plugins on event level and every plugin that operates on organizer and global level is basically some level of hack or workaround. We need to fix that at some point, but it's unclear how to do it without creating a hard-to-understand user experience.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants