Skip to content
Snippets Groups Projects
Commit 7eaa0a49 authored by Konrad Mohrfeldt's avatar Konrad Mohrfeldt :koala:
Browse files

refactor: use django_filters FilterSet for APINoteViewSet

This change re-implements all existing collection filters for the
APINoteViewSet with a FilterSet. No breaking changes are expected.
parent 29837872
No related branches found
No related tags found
1 merge request!20refactor collection filters with django_filters
...@@ -201,6 +201,32 @@ class TimeSlotFilterSet(filters.FilterSet): ...@@ -201,6 +201,32 @@ class TimeSlotFilterSet(filters.FilterSet):
return end or self.cleaned_data["start"] + datetime.timedelta(days=60) return end or self.cleaned_data["start"] + datetime.timedelta(days=60)
class NoteFilterSet(StaticFilterHelpTextMixin, filters.FilterSet):
ids = ModelMultipleChoiceFilter(
field_name="id",
queryset=models.Note.objects.all(),
help_text="Return only notes matching the specified id(s).",
)
owner = ModelMultipleChoiceFilter(
field_name="show__owners",
queryset=models.User.objects.all(),
help_text="Return only notes by show the specified owner(s): all notes the user may edit.",
)
class Meta:
model = models.Note
help_texts = {
"host": "Return only notes from the specified host.",
"user": "Return only notes created by the specified user.",
}
fields = [
"host",
"ids",
"owner",
"user",
]
class ActiveFilterSet(StaticFilterHelpTextMixin, filters.FilterSet): class ActiveFilterSet(StaticFilterHelpTextMixin, filters.FilterSet):
active = filters.BooleanFilter(field_name="is_active") active = filters.BooleanFilter(field_name="is_active")
......
...@@ -617,80 +617,28 @@ class APITimeSlotViewSet( ...@@ -617,80 +617,28 @@ class APITimeSlotViewSet(
class APINoteViewSet(viewsets.ModelViewSet): class APINoteViewSet(viewsets.ModelViewSet):
""" """
/notes/ returns all notes (GET) Returns a list of notes.
/notes/{pk} returns a single note (if owned) (GET)
/notes/?ids={...} returns given notes (if owned) (GET) Superusers may access and update all notes.
/notes/?host={host} returns notes assigned to a given host (GET)
/notes/?owner={owner} returns notes editable by a given user (GET)
/notes/?user={user} returns notes created by a given user (GET)
/shows/{show_pk}/notes returns all notes of a show (GET)
/shows/{show_pk}/notes/{pk} returns a note by its ID (GET)
/shows/{show_pk}/timeslots/{timeslot_pk}/note/ returns a note of the timeslot (GET)
/shows/{show_pk}/timeslots/{timeslot_pk}/note/{pk} returns a note by its ID (GET)
/shows/{show_pk}/schedules/{schedule_pk}/timeslots/{timeslot_pk}/note returns a note to the
timeslot (GET, POST).
/shows/{show_pk}/schedules/{schedule_pk}/timeslots/{timeslot_pk}/note/{pk} returns a note by
its ID (GET, PUT, DELETE)
Superusers may access and update all notes
""" """
queryset = Note.objects.none() queryset = Note.objects.all()
serializer_class = NoteSerializer serializer_class = NoteSerializer
permission_classes = [permissions.DjangoModelPermissionsOrAnonReadOnly] permission_classes = [permissions.DjangoModelPermissionsOrAnonReadOnly]
pagination_class = LimitOffsetPagination pagination_class = LimitOffsetPagination
filter_class = filters.NoteFilterSet
def get_queryset(self): def get_queryset(self):
timeslot_pk, show_pk = get_values(self.kwargs, "timeslot_pk", "show_pk") queryset = super().get_queryset()
# Endpoints
#
# /shows/1/schedules/1/timeslots/1/note
# /shows/1/timeslots/1/note
#
# Return a note to the timeslot
#
if show_pk and timeslot_pk:
notes = Note.objects.filter(show=show_pk, timeslot=timeslot_pk)
#
# /shows/1/notes
#
# Returns notes to the show
#
elif show_pk and timeslot_pk is None:
notes = Note.objects.filter(show=show_pk)
#
# /notes
#
# Returns all notes
#
else:
notes = Note.objects.all()
# Filters
if ids := self.request.query_params.get("ids"):
# Filter notes by their IDs
note_ids = list(map(int, ids.split(",")))
notes = notes.filter(id__in=note_ids)
if host := self.request.query_params.get("host"):
# Filter notes by host
notes = notes.filter(host=int(host))
if owner := self.request.query_params.get("owner"):
# Filter notes by show owner: all notes the user may edit
shows = Show.objects.filter(owners=int(owner))
notes = notes.filter(show__in=shows)
if user := self.request.query_params.get("user"): # subroute filters
# Filter notes by their creator show_pk, timeslot_pk = get_values(self.kwargs, "show_pk", "timeslot_pk")
notes = notes.filter(user=int(user)) if show_pk:
queryset = queryset.filter(show=show_pk)
if timeslot_pk:
queryset = queryset.filter(timeslot=timeslot_pk)
return notes return queryset
def create(self, request, *args, **kwargs): def create(self, request, *args, **kwargs):
"""Create a note""" """Create a note"""
......
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