handle empty results correctly in CampViewMixin, also add a text to the speaker list page when no speakers are found

This commit is contained in:
Thomas Steen Rasmussen 2018-07-18 00:46:38 +02:00
parent 025692d017
commit d46ccc530b
2 changed files with 35 additions and 26 deletions

View file

@ -16,36 +16,43 @@ class CampViewMixin(object):
def get_queryset(self): def get_queryset(self):
queryset = super().get_queryset() queryset = super().get_queryset()
if queryset:
camp_filter = self.model.get_camp_filter()
# Let us deal with eveything as a list # if this queryset is empty return it right away, because nothing for us to do
if isinstance(camp_filter, str): if not queryset:
camp_filter = [camp_filter] return queryset
for _filter in camp_filter: # get the camp_filter from the model
# add camp to the filter_dict camp_filter = self.model.get_camp_filter()
filter_dict = {_filter: self.camp}
# get pk from kwargs if we have it # Let us deal with eveything as a list
if hasattr(self, 'pk_url_kwarg'): if isinstance(camp_filter, str):
pk = self.kwargs.get(self.pk_url_kwarg) camp_filter = [camp_filter]
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 for _filter in camp_filter:
if hasattr(self, 'slug_url_kwarg'): # add camp to the filter_dict
slug = self.kwargs.get(self.slug_url_kwarg) filter_dict = {_filter: self.camp}
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 # get pk from kwargs if we have it
result = queryset.filter(**filter_dict) if hasattr(self, 'pk_url_kwarg'):
if result.exists(): pk = self.kwargs.get(self.pk_url_kwarg)
return result if pk is not None:
# We should also filter for the pk of the object
filter_dict['pk'] = pk
# Camp relation not found, or queryset is empty, return it unaltered # get slug from kwargs if we have it
return queryset 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
print("filter_dict is %s" % filter_dict)
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

View file

@ -17,5 +17,7 @@
{{ speaker.name }} {% for event in speaker.events.all %}<i class="fas fa-{{ event.event_type.icon }} fa-lg" style="color: {{ event.event_type.color }};" data-toggle="tooltip" title="{{ event.title }}"></i> {% endfor %}</a> {{ speaker.name }} {% for event in speaker.events.all %}<i class="fas fa-{{ event.event_type.icon }} fa-lg" style="color: {{ event.event_type.color }};" data-toggle="tooltip" title="{{ event.title }}"></i> {% endfor %}</a>
{% endfor %} {% endfor %}
</div> </div>
{% else %}
<p class="lead">No speakers found for {{ camp.title }}</p>
{% endif %} {% endif %}
{% endblock program_content %} {% endblock program_content %}