bornhack-website/src/camps/mixins.py
Thomas Steen Rasmussen 12a1c9a0ce
Facilities (#458)
* update font-awesome to 5.12.1

* prefetch members to considerably lower number of SQL queries for team list view

* add facilities app with facility feeedback functionality, working on #383

* Add GeoDjango (django.contrib.gis) and switch to PostGIS db backend. Add location field for Facility model. Add django-leaflet to requirements.

* better migration names

* tweaking travis config, we use py3.7 now, and add postgis

* Add qr code support for facilities (visible in the admin). Make facitilies browsable without logging in. Feedback can be submitted without logging in, given the facility UUID, which is not revealed to unauthenticated users.

* show quickfeedback icons when creating and when reading feedback

* only show anon option if user is logged in

* django-reversion somehow went missing from requirements
2020-02-24 23:28:52 +01:00

59 lines
2.1 KiB
Python

from camps.models import Camp
from django.shortcuts import get_object_or_404
class CampViewMixin:
"""
This mixin makes sure self.camp is available (taken from url kwarg camp_slug)
It also filters out objects that belong to other camps when the queryset has a camp_filter
"""
def setup(self, *args, **kwargs):
super().setup(*args, **kwargs)
self.camp = get_object_or_404(Camp, slug=self.kwargs["camp_slug"])
def get_queryset(self):
queryset = super().get_queryset()
# if this queryset is empty return it right away, because nothing for us to do
if not queryset:
return queryset
# do we have a camp_filter on this model
if not hasattr(self.model, "camp_filter"):
return queryset
# get the camp_filter from the model
camp_filter = self.model.get_camp_filter()
# Let us deal with eveything as a list
if isinstance(camp_filter, str):
camp_filter = [camp_filter]
for _filter in camp_filter:
# add camp to the filter_dict
filter_dict = {_filter: self.camp}
# get pk from kwargs if we have it
if hasattr(self, "pk_url_kwarg"):
pk = self.kwargs.get(self.pk_url_kwarg)
if pk is not None:
# We should also filter for the pk of the object
filter_dict["pk"] = pk
# get slug from kwargs if we have it
if hasattr(self, "slug_url_kwarg"):
slug = self.kwargs.get(self.slug_url_kwarg)
if slug is not None and (pk is None or self.query_pk_and_slug):
# we should also filter for the slug of the object
filter_dict[self.get_slug_field()] = slug
# do the filtering and return the result
result = queryset.filter(**filter_dict)
if result.exists():
# we got some results with this camp_filter, return now
return result
# no camp_filter returned any results, return an empty queryset
return result