Skip to content
Snippets Groups Projects
Verified Commit 706567c7 authored by Ernesto Rico Schmidt's avatar Ernesto Rico Schmidt
Browse files

refactor: remove filter_active

parent 988b0d7d
No related branches found
No related tags found
No related merge requests found
......@@ -5,7 +5,7 @@ from django_filters import rest_framework as filters
from django_filters import widgets
from django import forms
from django.db.models import Exists, OuterRef, Q, QuerySet
from django.db.models import Exists, OuterRef, QuerySet
from django.utils import timezone
from program import models
......@@ -74,11 +74,7 @@ class ShowFilterSet(StaticFilterHelpTextMixin, filters.FilterSet):
)
is_active = filters.BooleanFilter(
field_name="is_active",
method="filter_active",
help_text=(
"Return only currently running shows (with timeslots in the future) if true "
"or past or upcoming shows if false."
),
help_text="Return only currently active/inactive shows.",
)
is_writable = filters.BooleanFilter(
method="filter_writable",
......@@ -121,34 +117,6 @@ class ShowFilterSet(StaticFilterHelpTextMixin, filters.FilterSet):
help_text="Return only shows of the given type slug.",
)
def filter_active(self, queryset: QuerySet, name: str, value: bool):
# Filter currently running shows
# Get currently running schedules to filter by first
# For single dates we test if there'll be one in the future (and ignore the until date)
# TODO: Really consider first_date? (=currently active, not just upcoming ones)
# Add limit for future?
show_ids = (
models.Schedule.objects.filter(
# not "once" schedules with first_date in the past and last_date in the future
Q(
rrule__freq__gt=0,
first_date__lte=timezone.now(),
last_date__gte=timezone.now(),
)
# "once" schedules with first_date in the future
| Q(rrule__freq=0, first_date__gte=timezone.now())
)
.distinct()
.values_list("show_id", flat=True)
)
if value:
# Filter active shows based on timeslots as well as on the is_active flag
# Even if there are future timeslots but is_active=True the show will be considered as
# inactive
return queryset.filter(id__in=show_ids, is_active=True)
else:
return queryset.exclude(id__in=show_ids, is_active=True)
def filter_writable(self, queryset: QuerySet, _: str, value: bool) -> QuerySet:
user = self.request.user if self.request.user.is_authenticated else None
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment