bornhack-website/src/camps/mixins.py

25 lines
996 B
Python
Raw Normal View History

from camps.models import Camp
from django.shortcuts import get_object_or_404
2016-12-28 23:15:13 +00:00
class CampViewMixin(object):
"""
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 direct relation to the Camp model.
"""
def dispatch(self, request, *args, **kwargs):
2016-12-28 23:15:13 +00:00
self.camp = get_object_or_404(Camp, slug=self.kwargs['camp_slug'])
2017-03-12 14:43:41 +00:00
return super().dispatch(request, *args, **kwargs)
def get_queryset(self):
2016-12-28 23:15:13 +00:00
queryset = super(CampViewMixin, self).get_queryset()
if queryset:
# check if we have a foreignkey to Camp, filter if so
for field in queryset.model._meta.fields:
2018-03-04 14:58:20 +00:00
if field.name == "camp" and field.related_model._meta.label == "camps.Camp":
return queryset.filter(camp=self.camp)
# Camp relation not found, or queryset is empty, return it unaltered
return queryset