From f54515ba0a2da86a50391d01d4565ff6f1b588bd Mon Sep 17 00:00:00 2001 From: Raphael Michel Date: Sun, 15 Mar 2015 23:50:30 +0100 Subject: [PATCH] Provide a new API for plugins to add HTML to the of the site --- doc/development/api/general.rst | 18 ++++++++++++++++ doc/development/api/index.rst | 1 + src/pretix/control/context.py | 11 +++++++++- src/pretix/control/signals.py | 7 +++++++ .../control/templates/pretixcontrol/base.html | 1 + src/pretix/presale/context.py | 21 +++++++++++++++++++ src/pretix/presale/signals.py | 9 ++++++++ .../templates/pretixpresale/event/base.html | 1 + src/pretix/settings.py | 1 + 9 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 doc/development/api/general.rst create mode 100644 src/pretix/presale/context.py create mode 100644 src/pretix/presale/signals.py diff --git a/doc/development/api/general.rst b/doc/development/api/general.rst new file mode 100644 index 0000000000..e5cc847103 --- /dev/null +++ b/doc/development/api/general.rst @@ -0,0 +1,18 @@ +.. highlight:: python + :linenothreshold: 5 + +General APIs +============ + +This page lists some general signals and hooks which do not belong to a +specific type of plugin but might come in handy for various plugins. + +HTML head injection +------------------- + +These two signals allow you to put code inside the HTML ```` tag +of every page. One signal is for the frontend, one for the backend. You +won't get any arguments and can return plain HTML. + +* ``pretix.presale.signals.html_head`` +* ``pretix.control.signals.html_head`` \ No newline at end of file diff --git a/doc/development/api/index.rst b/doc/development/api/index.rst index 1be6a3a8c1..8089265f83 100644 --- a/doc/development/api/index.rst +++ b/doc/development/api/index.rst @@ -9,3 +9,4 @@ Contents: plugins restriction payment + general diff --git a/src/pretix/control/context.py b/src/pretix/control/context.py index 6c4d7b5699..d318b56fef 100644 --- a/src/pretix/control/context.py +++ b/src/pretix/control/context.py @@ -1,14 +1,23 @@ from django.conf import settings from django.core.urlresolvers import resolve +from .signals import html_head def contextprocessor(request): """ Adds data to all template contexts """ + url = resolve(request.path_info) + if url.namespace != 'control': + return {} ctx = { - 'url_name': resolve(request.path_info).url_name, + 'url_name': url.url_name, 'settings': settings, } + _html_head = [] + if hasattr(request, 'event'): + for receiver, response in html_head.send(request.event): + _html_head.append(response) + ctx['html_head'] = "".join(_html_head) return ctx diff --git a/src/pretix/control/signals.py b/src/pretix/control/signals.py index fe50902d33..a44ce0192b 100644 --- a/src/pretix/control/signals.py +++ b/src/pretix/control/signals.py @@ -8,3 +8,10 @@ This signal is sent out to build configuration forms for all restriction formset restriction_formset = EventPluginSignal( providing_args=["item"] ) + +""" +This signal is sent out to include code into the HTML tag +""" +html_head = EventPluginSignal( + providing_args=[] +) diff --git a/src/pretix/control/templates/pretixcontrol/base.html b/src/pretix/control/templates/pretixcontrol/base.html index 136afa9f08..4c9f876a21 100644 --- a/src/pretix/control/templates/pretixcontrol/base.html +++ b/src/pretix/control/templates/pretixcontrol/base.html @@ -17,6 +17,7 @@ {% endcompress %} + {{ html_head|safe }}
diff --git a/src/pretix/presale/context.py b/src/pretix/presale/context.py new file mode 100644 index 0000000000..8f1883a06d --- /dev/null +++ b/src/pretix/presale/context.py @@ -0,0 +1,21 @@ +from django.core.urlresolvers import resolve +from .signals import html_head + + +def contextprocessor(request): + """ + Adds data to all template contexts + """ + url = resolve(request.path_info) + if url.namespace != 'presale': + return {} + + ctx = { + } + _html_head = [] + if hasattr(request, 'event'): + for receiver, response in html_head.send(request.event): + _html_head.append(response) + ctx['html_head'] = "".join(_html_head) + + return ctx diff --git a/src/pretix/presale/signals.py b/src/pretix/presale/signals.py new file mode 100644 index 0000000000..621100b2d9 --- /dev/null +++ b/src/pretix/presale/signals.py @@ -0,0 +1,9 @@ +from pretix.base.signals import EventPluginSignal + + +""" +This signal is sent out to include code into the HTML tag +""" +html_head = EventPluginSignal( + providing_args=[] +) diff --git a/src/pretix/presale/templates/pretixpresale/event/base.html b/src/pretix/presale/templates/pretixpresale/event/base.html index a94d056c0e..9c34b6e7fb 100644 --- a/src/pretix/presale/templates/pretixpresale/event/base.html +++ b/src/pretix/presale/templates/pretixpresale/event/base.html @@ -14,6 +14,7 @@ {% endcompress %} + {{ html_head|safe }}
diff --git a/src/pretix/settings.py b/src/pretix/settings.py index 40834c048d..2bd52da610 100644 --- a/src/pretix/settings.py +++ b/src/pretix/settings.py @@ -75,6 +75,7 @@ TEMPLATE_CONTEXT_PROCESSORS = ( "django.core.context_processors.tz", "django.contrib.messages.context_processors.messages", 'pretix.control.context.contextprocessor', + 'pretix.presale.context.contextprocessor', ) ROOT_URLCONF = 'pretix.urls'