From 57dce1f887c6704616ef4127014a8af7df0a2ec1 Mon Sep 17 00:00:00 2001 From: Thomas Steen Rasmussen Date: Sat, 25 Apr 2020 23:25:56 +0200 Subject: [PATCH] change the backoffice proxyview so it doesn't use a form, to make it easier to link to stuff --- .../templates/backoffice_proxy.html | 13 +++--- src/backoffice/urls.py | 1 + src/backoffice/views.py | 45 ++++++++----------- 3 files changed, 25 insertions(+), 34 deletions(-) diff --git a/src/backoffice/templates/backoffice_proxy.html b/src/backoffice/templates/backoffice_proxy.html index 4c50442f..37508c0b 100644 --- a/src/backoffice/templates/backoffice_proxy.html +++ b/src/backoffice/templates/backoffice_proxy.html @@ -5,14 +5,11 @@

See proxied content

-
- {% csrf_token %} - {% bootstrap_form form %} - - Cancel -
+
- {% endblock content %} - diff --git a/src/backoffice/urls.py b/src/backoffice/urls.py index 74189284..af361eec 100644 --- a/src/backoffice/urls.py +++ b/src/backoffice/urls.py @@ -39,6 +39,7 @@ urlpatterns = [ path("", BackofficeIndexView.as_view(), name="index"), # proxy view path("proxy/", BackofficeProxyView.as_view(), name="proxy"), + path("proxy//", BackofficeProxyView.as_view(), name="proxy"), # facility feedback path( "feedback/facilities//", diff --git a/src/backoffice/views.py b/src/backoffice/views.py index acaf91d5..b94aea41 100644 --- a/src/backoffice/views.py +++ b/src/backoffice/views.py @@ -4,7 +4,6 @@ from itertools import chain import requests from camps.mixins import CampViewMixin -from django import forms from django.conf import settings from django.contrib import messages from django.contrib.auth.mixins import LoginRequiredMixin @@ -13,7 +12,7 @@ from django.core.exceptions import PermissionDenied from django.core.files import File from django.db.models import Sum from django.forms import modelformset_factory -from django.http import HttpResponse +from django.http import Http404, HttpResponse from django.shortcuts import get_object_or_404, redirect from django.urls import reverse from django.utils import timezone @@ -776,39 +775,33 @@ class ShopTicketOverview(LoginRequiredMixin, CampViewMixin, ListView): return super().get_context_data(object_list=object_list, **kwargs) -class BackofficeProxyView(CampViewMixin, RaisePermissionRequiredMixin, FormView): +class BackofficeProxyView(CampViewMixin, RaisePermissionRequiredMixin, TemplateView): """ Show proxied stuff, only for simple HTML pages with no external content - Define URLs in settings.BACKOFFICE_PROXY_URLS as a dict of description: url pairs + Define URLs in settings.BACKOFFICE_PROXY_URLS as a dict of slug: (description, url) pairs """ permission_required = "camps.backoffice_permission" template_name = "backoffice_proxy.html" - def setup(self, *args, **kwargs): - super().setup(*args, **kwargs) - self.form_class = forms.Form + def dispatch(self, request, *args, **kwargs): + """ Perform the request and return the response if we have a slug """ + # list available stuff if we have no slug + if "proxy_slug" not in kwargs: + return super().dispatch(request, *args, **kwargs) - def get_form(self, *args, **kwargs): - form = super().get_form(*args, **kwargs) - form.fields["url"] = forms.ChoiceField( - choices=[ - (url, desc) for desc, url in settings.BACKOFFICE_PROXY_URLS.items() - ], - widget=forms.RadioSelect, - help_text="Pick the URL you wish to see", - ) - return form + # is the slug valid? + if kwargs["proxy_slug"] not in settings.BACKOFFICE_PROXY_URLS.keys(): + raise Http404 - def form_valid(self, form): - """ Perform the request and return the response """ - if form.cleaned_data["url"] not in settings.BACKOFFICE_PROXY_URLS.values(): - # this is not one of the urls from settings - messages.error(self.request, "Unknown URL") - return redirect( - reverse("backoffice:proxy", kwargs={"camp_slug": self.camp.slug}) - ) # perform the request - r = requests.get(form.cleaned_data["url"]) + description, url = settings.BACKOFFICE_PROXY_URLS[kwargs["proxy_slug"]] + r = requests.get(url) + # return the response, keeping the status code but no headers return HttpResponse(r.content, status=r.status_code) + + def get_context_data(self, *args, **kwargs): + context = super().get_context_data(*args, **kwargs) + context["urls"] = settings.BACKOFFICE_PROXY_URLS + return context