Problem statement
In this Django project, consolidate the recipe API into one ViewSet and one router registration. Replace the five hand-written function views in app/views.py and the six hand-written patterns in app/urls.py, and add the two operations that are not plain CRUD. Today one resource is described in six places: each pattern names its own view and invents its own route name, each view repeats the same lookup, serializer call and status code, and publishing never made it into the recipe paths at all - it lives at api/mark-published/<pk>/, answers to GET as happily as to POST, and replies with a status note instead of the recipe.
Requirements
Implement:
- the recipe ViewSet in app/views.py
- the router registration in app/urls.py
The following files are already implemented and provided: app/models.py, app/serializers.py and project/urls.py. Recipe holds title (CharField(max_length=80)), servings (PositiveIntegerField(default=1)), published (BooleanField(default=False)) and created_at (DateTimeField(default=timezone.now), an ordinary column so a row can be stored with any timestamp); the module also exports RECENT_WINDOW, a timedelta of seven days. RecipeSerializer is a complete ModelSerializer over the five keys id, title, servings, published and created_at, with id and created_at rendered but never accepted. project/urls.py includes app/urls.py at the project root, so the path the router is mounted under is up to you. home, the ordinary Django view answering / with the running server's port, is the page the Run button shows: it and its "" route stay exactly as they are, and every other recipe view and recipe pattern is yours to delete.
1. One ViewSet for the resource
- A single ModelViewSet, built on every recipe and on the given RecipeSerializer, serves the collection and the detail route: both routes resolve to a view generated from that one class.
- The list route dispatches list and create; the detail route dispatches retrieve, update and destroy.
- A collection GET answers 200 with every stored row, each row carrying exactly the serializer's five keys. A POST answers 201 and stores the row, unpublished. A detail GET answers 200 with the row it names. A PUT and a PATCH each answer 200, and a PATCH changes only the fields it carries. A DELETE answers 204 and removes that one row.
2. The router registration
- A DefaultRouter replaces the hand-written patterns, with the viewset registered under the prefix recipes and the explicit basename recipe.
- The generated route names are therefore recipe-list and recipe-detail, and the detail route hangs off the list route and carries the lookup value.
- The router's own API root answers 200 and lists the resource under the key recipes, pointing at the collection route.
- The router is not mounted at "": the landing page answers /.
3. A publish action on one recipe
- A publish action acts on a single recipe, so its route is named recipe-publish and hangs off that recipe's detail route.
- It accepts POST only. It sets published to true on that recipe, stores the change, and leaves every other recipe as it was.
- It answers 200 with that recipe's full representation - the five serializer keys, with published now true - and not with a status message.
- GET, PUT, PATCH and DELETE on that route are refused with 405, and the Allow header on the refusal names exactly POST and OPTIONS.
4. A recent action on the collection
- A recent action acts on the collection, so its route is named recipe-recent and hangs off the list route.
- It accepts GET only, and answers 200 with the recipes whose created_at falls inside RECENT_WINDOW of now, newest first, and with nothing else. A recipe older than the window is still in the table and still in the full listing: it is only missing from this one.
- POST, PUT, PATCH and DELETE on that route are refused with 405, and the Allow header on the refusal names exactly GET, HEAD and OPTIONS.
- Read the window from RECENT_WINDOW rather than writing the number again.
Notes
- A ViewSet carries a method per action - list, create, retrieve, update, partial_update, destroy - rather than per HTTP verb, and the router decides which action a request reaches. ModelViewSet ships all six on a queryset and a serializer.
- The prefix and the basename in router.register() do different jobs: the prefix is the URL segment, the basename is the stem of every route name the registration generates. The tests never write a path out as a string; they look every route up by name, because a route reached by a hardcoded path proves nothing about the registration.
- A DefaultRouter adds one route a SimpleRouter does not: an API root named api-root, keyed by the prefixes the resources were registered under. That root is part of what this question asks for.
- An operation that is not one of the six gets an @action method, and the router routes it too. detail=True puts the route under one object and hands the method the lookup value; detail=False puts it on the collection. The method name is the route segment and the tail of its route name, and methods is the whole list it answers - every other method on that route is refused with 405 and an Allow header, with no code of yours.
- self.get_object() does the detail lookup with the same 404 the detail route gives, and self.get_serializer() builds the viewset's serializer: returning its .data is what makes an action answer with the resource rather than with a note about it.
- Only app/views.py and app/urls.py change. Do not add authentication classes, permission classes, pagination, filtering or throttling: none of them belongs in this question.
