Added documentation on custom views

This commit is contained in:
Raphael Michel
2016-07-26 12:16:09 +02:00
parent 310a14a393
commit 03a56f81ca
3 changed files with 109 additions and 1 deletions

View File

@@ -0,0 +1,107 @@
.. highlight:: python
:linenothreshold: 5
Creating custom views
=====================
This page describes how to provide a custom view from within your plugin. Before you start
reading this page, please read and understand how :ref:`URL handling <urlconf>` works in
pretix.
Control panel views
-------------------
If you want to add a custom view to the control area of an event, just register an URL in your
``maindomain_urls.py`` that lives in the ``/control/`` subpath::
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^control/event/(?P<organizer>[^/]+)/(?P<event>[^/]+)/mypluginname/',
views.admin_view, name='backend'),
]
It is required that your URL paramaters are called ``organizer`` and ``event``. If you want to
install a view on organizer level, you can leave out the ``event``.
You can then implement the view as you would normally do. Our middleware will automatically
detect the ``/control/`` subpath and will ensure the following things if this is an URL with
both the ``event`` and ``organizer`` parameters:
* The user is logged in
* The ``request.event`` attribute contains the current event
* The ``request.organizer`` attribute contains the event's organizer
* The user has permission to access view the current event
If only the ``organizer`` parameter is present, it will be ensured that:
* The user is logged in
* The ``request.organizer`` attribute contains the event's organizer
* The user has permission to access view the current organizer
If you want to require specific permission types, we provide you with a decorator or a mixin for
your views::
from pretix.control.permissions import (
event_permission_required, EventPermissionRequiredMixin
)
class AdminView(EventPermissionRequiredMixin, View):
permission = 'can_view_orders'
...
@event_permission_required('can_view_orders')
def admin_view(request, organizer, event):
...
Similarly, there is ``organizer_permission_required`` and ``OrganizerPermissionRequiredMixin``.
Frontend views
--------------
Including a custom view into the participant-facing frontend is a little bit different as there is
no path prefix like ``control/``.
First, define your URL in your ``maindomain_urls.py``::
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^(?P<organizer>[^/]+)/(?P<event>[^/]+)/mypluginname/',
views.frontend_view, name='frontend'),
]
And for the case of custom domains in your ``subdomain_urls.py``::
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^(?P<event>[^/]+)/mypluginname/',
views.frontend_view, name='frontend'),
]
You can then implement a view as you would normally do, but you need to apply a decorator to your
view if you want pretix's default behavior::
from pretix.presale.utils import event_view
@event_view
def event_view(request, *args, **kwargs):
...
This decorator will check the URL arguments for their ``event`` and ``organizer`` parameters and
correctly ensure that:
* The requested event exists and is activated
* The event is accessed via the domain it should be accessed
* The ``request.event`` attribute contains the correct ``Event`` object
* The ``request.organizer`` attribute contains the correct ``Organizer`` object
* The locale is set correctly

View File

@@ -10,4 +10,5 @@ Contents:
payment
ticketoutput
exporter
customview
general

View File

@@ -32,7 +32,7 @@ class PermissionMiddleware:
# This middleware should only touch the /control subpath
return
if hasattr(request, 'domain'):
# If the user is on a organizer's subdomain, he sould be redirected to pretix
# If the user is on a organizer's subdomain, he should be redirected to pretix
return redirect(urljoin(settings.SITE_URL, request.get_full_path()))
if url_name in self.EXCEPTIONS:
return