Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create JSON endpoint for Service and Genre list and detail views #1612

Merged
merged 2 commits into from
Aug 26, 2024

Conversation

dchiller
Copy link
Contributor

@dchiller dchiller commented Aug 21, 2024

Returns JSON responses to requests to Service and Genre urls if the request's Accept header is application/json.

Implements a new class JSONResponseMixin that handles the content negotiation according to the Accept header. Classes inheriting from this mixin should define a json_fields attribute that lists the fields on the model that should be returned in the JSON response.

JSON responses are in the form of a object with a key being the model name (singular, in the case of a DetailView and plural in the case of a ListView -- this matches the CantusDB standard for urls).

For example, a request to /services with Accept: application/json will return:

{
  services : [
    { object with json_fields for service 1 },
    {  object with json_fields for service 2 },
  ]
}

A request to /service/1234 with Accept: application/json will return:

{
  service : { object with json_fields for service 1 }
}

The PR also adds appropriate tests for the GenreDetail, GenreList, ServiceDetail, and ServiceList views. These tests weren't really making use of Django's TestCase functionality for setting up test data, so I did some refactoring there too.

Finally, the GenreListView was inheriting from a SearchableListMixin but was not making use of that mixin's functionality (the filtering for that view was implemented in the overwritten get_queryset method), so I removed that inheritance.

Closes #1609.

The JSONResponseMixin reimplements the render_to_result method
on generic DetailViews and ListViews to check for the request's
Accept header. If the Accept header is "application/json" then
json for the object or list of objects is provided. If not, renders
the template as usual.
…vice views

- Uses the JSONResponseMixin to return JSON results in appropriate places
for the GenreList, GenreDetail, ServiceList, and ServiceDetail views.
- Adds appropriate tests for the JSON response
- Refactors some relevant tests to more efficiently create and use fake
objects
- Allows all aspects of a fake genre to be set with the make_fake_genre
function
@dchiller dchiller requested a review from lucasmarchd01 August 21, 2024 13:54
@dchiller dchiller merged commit 13b6c74 into DDMAL:develop Aug 26, 2024
1 check passed
@dchiller dchiller deleted the i1609-genre-office-endpoints branch August 26, 2024 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide genre and office json endpoints
2 participants