Make each model which inherit from CampRelatedModel but doesn not have a direct relation, define a camp_filter. (#240)

This commit is contained in:
Víðir Valberg Guðmundsson 2018-06-20 22:03:29 +02:00 committed by Thomas Steen Rasmussen
parent b94e737e09
commit 4c60415336
6 changed files with 27 additions and 13 deletions

View file

@ -9,25 +9,16 @@ class CampViewMixin(object):
It also filters out objects that belong to other camps when the queryset has It also filters out objects that belong to other camps when the queryset has
a direct relation to the Camp model. a direct relation to the Camp model.
""" """
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
self.camp = get_object_or_404(Camp, slug=self.kwargs['camp_slug']) self.camp = get_object_or_404(Camp, slug=self.kwargs["camp_slug"])
return super().dispatch(request, *args, **kwargs) return super().dispatch(request, *args, **kwargs)
def get_queryset(self): def get_queryset(self):
queryset = super(CampViewMixin, self).get_queryset() queryset = super(CampViewMixin, self).get_queryset()
if queryset: if queryset:
# check if we have a foreignkey to Camp, filter if so camp_filter = {self.model.get_camp_filter(): self.camp}
for field in queryset.model._meta.fields: return queryset.filter(**camp_filter)
if field.name == "camp" and field.related_model._meta.label == "camps.Camp":
return queryset.filter(camp=self.camp)
# check if we have a camp property or cached_property, filter if so
if hasattr(queryset[0], 'camp') and (isinstance(getattr(type(queryset[0]), 'camp', None), property) or isinstance(getattr(type(queryset[0]), 'camp', None), cached_property)):
for item in queryset:
if item.camp != self.camp:
queryset = queryset.exclude(pk=item.pk)
return queryset
# Camp relation not found, or queryset is empty, return it unaltered # Camp relation not found, or queryset is empty, return it unaltered
return queryset return queryset

View file

@ -71,6 +71,8 @@ class InfoItem(CampRelatedModel):
def camp(self): def camp(self):
return self.category.camp return self.category.camp
camp_filter = 'category__camp'
def clean(self): def clean(self):
if InfoCategory.objects.filter(camp=self.category.camp, anchor=self.anchor).exists(): if InfoCategory.objects.filter(camp=self.category.camp, anchor=self.anchor).exists():
# this anchor is already in use on a category, so it cannot be used here (they must be unique on the entire page) # this anchor is already in use on a category, so it cannot be used here (they must be unique on the entire page)

View file

@ -146,6 +146,8 @@ class Url(CampRelatedModel):
def camp(self): def camp(self):
return self.owner.camp return self.owner.camp
camp_filter = 'owner__camp'
############################################################################### ###############################################################################
@ -343,6 +345,8 @@ class EventProposal(UserSubmittedModel):
def camp(self): def camp(self):
return self.track.camp return self.track.camp
camp_filter = 'track__camp'
@property @property
def headline(self): def headline(self):
return self.title return self.title
@ -611,6 +615,8 @@ class Event(CampRelatedModel):
def camp(self): def camp(self):
return self.track.camp return self.track.camp
camp_filter = 'track__camp'
@property @property
def speakers_list(self): def speakers_list(self):
if self.speakers.exists(): if self.speakers.exists():
@ -680,6 +686,8 @@ class EventInstance(CampRelatedModel):
def camp(self): def camp(self):
return self.event.camp return self.event.camp
camp_filter = 'event__track__camp'
@property @property
def schedule_date(self): def schedule_date(self):
""" """

View file

@ -43,6 +43,8 @@ class Sponsor(CampRelatedModel):
def camp(self): def camp(self):
return self.tier.camp return self.tier.camp
camp_filter = 'tier__camp'
class SponsorTier(CampRelatedModel): class SponsorTier(CampRelatedModel):
name = models.CharField( name = models.CharField(

View file

@ -255,6 +255,8 @@ class TeamMember(CampRelatedModel):
""" All CampRelatedModels must have a camp FK or a camp property """ """ All CampRelatedModels must have a camp FK or a camp property """
return self.team.camp return self.team.camp
camp_filter = 'team__camp'
class TeamTask(CampRelatedModel): class TeamTask(CampRelatedModel):
team = models.ForeignKey( team = models.ForeignKey(
@ -288,6 +290,8 @@ class TeamTask(CampRelatedModel):
""" All CampRelatedModels must have a camp FK or a camp property """ """ All CampRelatedModels must have a camp FK or a camp property """
return self.team.camp return self.team.camp
camp_filter = 'team__camp'
def save(self, **kwargs): def save(self, **kwargs):
# generate slug if needed # generate slug if needed
if not self.slug: if not self.slug:

View file

@ -51,6 +51,9 @@ class CreatedUpdatedModel(CleanedModel):
class CampRelatedModel(CreatedUpdatedModel): class CampRelatedModel(CreatedUpdatedModel):
camp_filter = 'camp'
class Meta: class Meta:
abstract = True abstract = True
@ -70,6 +73,10 @@ class CampRelatedModel(CreatedUpdatedModel):
super().delete(**kwargs) super().delete(**kwargs)
@classmethod
def get_camp_filter(cls):
return cls.camp_filter
class OutgoingEmail(CreatedUpdatedModel): class OutgoingEmail(CreatedUpdatedModel):
subject = models.CharField(max_length=500) subject = models.CharField(max_length=500)