2016-12-25 14:52:55 +00:00
|
|
|
from camps.models import Camp
|
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
|
|
|
|
|
2016-12-28 23:15:13 +00:00
|
|
|
class CampViewMixin(object):
|
2017-05-24 05:49:32 +00:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
2016-12-25 14:52:55 +00:00
|
|
|
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)
|
2016-12-25 14:52:55 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
2016-12-28 23:15:13 +00:00
|
|
|
queryset = super(CampViewMixin, self).get_queryset()
|
2017-05-24 05:49:32 +00:00
|
|
|
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":
|
2017-05-24 05:49:32 +00:00
|
|
|
return queryset.filter(camp=self.camp)
|
|
|
|
|
|
|
|
# Camp relation not found, or queryset is empty, return it unaltered
|
|
|
|
return queryset
|