change the backoffice proxyview so it doesn't use a form, to make it easier to link to stuff

This commit is contained in:
Thomas Steen Rasmussen 2020-04-25 23:25:56 +02:00
parent ab338e8652
commit 57dce1f887
3 changed files with 25 additions and 34 deletions

View file

@ -5,14 +5,11 @@
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-heading"><h3 class="panel-title">See proxied content</h3></div> <div class="panel-heading"><h3 class="panel-title">See proxied content</h3></div>
<div class="panel-body"> <div class="panel-body">
<form method="POST"> <ul class="list-group">
{% csrf_token %} {% for slug, urltuple in urls.items %}
{% bootstrap_form form %} <a class="list-group-item" href="{% url 'backoffice:proxy' camp_slug=camp.slug proxy_slug=slug %}"><span class="h4">{{ urltuple.0 }}</span><span class="pull-right">{{ urltuple.1 }}</span></a>
<button type="submit" class="btn btn-primary"><i class='fas fa-check'></i> Show me!</button> {% endfor %}
<a href="{% url 'backoffice:index' camp_slug=camp.slug %}" class="btn btn-default"><i class="fas fa-undo"></i> Cancel</a> </ul>
</form>
</div> </div>
</div> </div>
{% endblock content %} {% endblock content %}

View file

@ -39,6 +39,7 @@ urlpatterns = [
path("", BackofficeIndexView.as_view(), name="index"), path("", BackofficeIndexView.as_view(), name="index"),
# proxy view # proxy view
path("proxy/", BackofficeProxyView.as_view(), name="proxy"), path("proxy/", BackofficeProxyView.as_view(), name="proxy"),
path("proxy/<slug:proxy_slug>/", BackofficeProxyView.as_view(), name="proxy"),
# facility feedback # facility feedback
path( path(
"feedback/facilities/<slug:team_slug>/", "feedback/facilities/<slug:team_slug>/",

View file

@ -4,7 +4,6 @@ from itertools import chain
import requests import requests
from camps.mixins import CampViewMixin from camps.mixins import CampViewMixin
from django import forms
from django.conf import settings from django.conf import settings
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin 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.core.files import File
from django.db.models import Sum from django.db.models import Sum
from django.forms import modelformset_factory 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.shortcuts import get_object_or_404, redirect
from django.urls import reverse from django.urls import reverse
from django.utils import timezone from django.utils import timezone
@ -776,39 +775,33 @@ class ShopTicketOverview(LoginRequiredMixin, CampViewMixin, ListView):
return super().get_context_data(object_list=object_list, **kwargs) 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 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" permission_required = "camps.backoffice_permission"
template_name = "backoffice_proxy.html" template_name = "backoffice_proxy.html"
def setup(self, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
super().setup(*args, **kwargs) """ Perform the request and return the response if we have a slug """
self.form_class = forms.Form # 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): # is the slug valid?
form = super().get_form(*args, **kwargs) if kwargs["proxy_slug"] not in settings.BACKOFFICE_PROXY_URLS.keys():
form.fields["url"] = forms.ChoiceField( raise Http404
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
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 # 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 the response, keeping the status code but no headers
return HttpResponse(r.content, status=r.status_code) 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