diff --git a/program/filters.py b/program/filters.py index 9be299df8f04c83fa1c5fe765e4624a6dceb491b..e05e58d70c58d55ae4171ceb22055487f03bfe8d 100644 --- a/program/filters.py +++ b/program/filters.py @@ -201,6 +201,32 @@ class TimeSlotFilterSet(filters.FilterSet): 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): active = filters.BooleanFilter(field_name="is_active") diff --git a/program/views.py b/program/views.py index 2ef6198ae9af88c1aa32891d3b607a265d835ae3..51f95b55d2c2e7426e0aecc4ee27cdbae12f93b8 100644 --- a/program/views.py +++ b/program/views.py @@ -617,80 +617,28 @@ class APITimeSlotViewSet( class APINoteViewSet(viewsets.ModelViewSet): """ - /notes/ returns all notes (GET) - /notes/{pk} returns a single note (if owned) (GET) - /notes/?ids={...} returns given notes (if owned) (GET) - /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 + Returns a list of notes. + + Superusers may access and update all notes. """ - queryset = Note.objects.none() + queryset = Note.objects.all() serializer_class = NoteSerializer permission_classes = [permissions.DjangoModelPermissionsOrAnonReadOnly] pagination_class = LimitOffsetPagination + filter_class = filters.NoteFilterSet def get_queryset(self): - timeslot_pk, show_pk = get_values(self.kwargs, "timeslot_pk", "show_pk") - - # 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) + queryset = super().get_queryset() - if user := self.request.query_params.get("user"): - # Filter notes by their creator - notes = notes.filter(user=int(user)) + # subroute filters + show_pk, timeslot_pk = get_values(self.kwargs, "show_pk", "timeslot_pk") + 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): """Create a note"""