Make each model which inherit from CampRelatedModel but doesn not have a direct relation, define a camp_filter. (#240)
This commit is contained in:
parent
b94e737e09
commit
4c60415336
|
@ -9,25 +9,16 @@ class CampViewMixin(object):
|
|||
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):
|
||||
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)
|
||||
|
||||
def get_queryset(self):
|
||||
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:
|
||||
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_filter = {self.model.get_camp_filter(): self.camp}
|
||||
return queryset.filter(**camp_filter)
|
||||
|
||||
# Camp relation not found, or queryset is empty, return it unaltered
|
||||
return queryset
|
||||
|
||||
|
|
|
@ -71,6 +71,8 @@ class InfoItem(CampRelatedModel):
|
|||
def camp(self):
|
||||
return self.category.camp
|
||||
|
||||
camp_filter = 'category__camp'
|
||||
|
||||
def clean(self):
|
||||
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)
|
||||
|
|
|
@ -146,6 +146,8 @@ class Url(CampRelatedModel):
|
|||
def camp(self):
|
||||
return self.owner.camp
|
||||
|
||||
camp_filter = 'owner__camp'
|
||||
|
||||
|
||||
###############################################################################
|
||||
|
||||
|
@ -343,6 +345,8 @@ class EventProposal(UserSubmittedModel):
|
|||
def camp(self):
|
||||
return self.track.camp
|
||||
|
||||
camp_filter = 'track__camp'
|
||||
|
||||
@property
|
||||
def headline(self):
|
||||
return self.title
|
||||
|
@ -611,6 +615,8 @@ class Event(CampRelatedModel):
|
|||
def camp(self):
|
||||
return self.track.camp
|
||||
|
||||
camp_filter = 'track__camp'
|
||||
|
||||
@property
|
||||
def speakers_list(self):
|
||||
if self.speakers.exists():
|
||||
|
@ -680,6 +686,8 @@ class EventInstance(CampRelatedModel):
|
|||
def camp(self):
|
||||
return self.event.camp
|
||||
|
||||
camp_filter = 'event__track__camp'
|
||||
|
||||
@property
|
||||
def schedule_date(self):
|
||||
"""
|
||||
|
|
|
@ -43,6 +43,8 @@ class Sponsor(CampRelatedModel):
|
|||
def camp(self):
|
||||
return self.tier.camp
|
||||
|
||||
camp_filter = 'tier__camp'
|
||||
|
||||
|
||||
class SponsorTier(CampRelatedModel):
|
||||
name = models.CharField(
|
||||
|
|
|
@ -255,6 +255,8 @@ class TeamMember(CampRelatedModel):
|
|||
""" All CampRelatedModels must have a camp FK or a camp property """
|
||||
return self.team.camp
|
||||
|
||||
camp_filter = 'team__camp'
|
||||
|
||||
|
||||
class TeamTask(CampRelatedModel):
|
||||
team = models.ForeignKey(
|
||||
|
@ -288,6 +290,8 @@ class TeamTask(CampRelatedModel):
|
|||
""" All CampRelatedModels must have a camp FK or a camp property """
|
||||
return self.team.camp
|
||||
|
||||
camp_filter = 'team__camp'
|
||||
|
||||
def save(self, **kwargs):
|
||||
# generate slug if needed
|
||||
if not self.slug:
|
||||
|
|
|
@ -51,6 +51,9 @@ class CreatedUpdatedModel(CleanedModel):
|
|||
|
||||
|
||||
class CampRelatedModel(CreatedUpdatedModel):
|
||||
|
||||
camp_filter = 'camp'
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
|
@ -70,6 +73,10 @@ class CampRelatedModel(CreatedUpdatedModel):
|
|||
|
||||
super().delete(**kwargs)
|
||||
|
||||
@classmethod
|
||||
def get_camp_filter(cls):
|
||||
return cls.camp_filter
|
||||
|
||||
|
||||
class OutgoingEmail(CreatedUpdatedModel):
|
||||
subject = models.CharField(max_length=500)
|
||||
|
|
Loading…
Reference in a new issue