Problem statement
In this Django project, build the team task board's whole API surface: one ViewSet on a router for the task resource, plus one APIView for the board summary. Replace the seven plain Django views in app/views.py and the hand-written patterns in app/urls.py. Today every view hand-builds a dict and passes it to JsonResponse, so a create, a blank title and a task id that does not exist all come back 200; the creator is read straight out of the posted body, which lets a client file a task in somebody else's name; and nothing declares which HTTP methods an endpoint answers, so completing a task works just as well with a GET.
Requirements
Implement:
- the task ViewSet and the board summary view in app/views.py
- the router registration and the summary route in app/urls.py
The following files are already implemented and provided: app/models.py, app/serializers.py, project/settings.py and project/urls.py. Task holds title (CharField(max_length=120)), details (TextField), due_on (DateField, the calendar day the task is due), completed (BooleanField(default=False)), creator (a ForeignKey to the user model, related_name="tasks") and created_at (DateTimeField(default=timezone.now), an ordinary column so a row can be stored with any timestamp). TaskSerializer is a complete ModelSerializer over the seven keys id, title, details, due_on, completed, creator and created_at, with id, creator and created_at rendered but never accepted, so a creator a client sends never reaches validated_data and whoever saves the object has to supply one; title, details and due_on have no model default and are required, while completed has one and is not. project/settings.py already configures the API-wide policy: reading is open to anyone and writing requires a signed-in account. project/urls.py includes app/urls.py at the project root, so the paths things are mounted under are 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 task view and task pattern is yours to delete.
1. One ViewSet for the task resource
- A single ModelViewSet, built on every task and on the given TaskSerializer, 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, partial_update and destroy.
- A collection GET answers 200 with every stored row, each row carrying exactly the serializer's seven keys.
- A POST answers 201 with the created task's representation, stores one row, and that row is incomplete.
- A POST the serializer refuses answers 400, stores nothing, and its body is the serializer's errors themselves.
- 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 with an empty body.
- GET, PUT, PATCH and DELETE on a task id that is not in the table all answer 404, and nothing is created or removed while answering it.
2. The router registration
- A DefaultRouter replaces the hand-written task patterns, with the viewset registered under the prefix tasks and the explicit basename task.
- The generated route names are therefore task-list and task-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 tasks, pointing at the task collection route.
- The router is not mounted at "": the landing page answers /.
3. The creator comes from the request
- A POST stores the task against the account that made the request, and the 201 renders that account's id under creator.
- A creator in the posted body is ignored: the rest of that body is still honoured, but the task belongs to the requesting account.
- The creator is set in the view, not taken from the request body.
4. A complete action on one task
- A complete action acts on a single task, so its route is named task-complete and hangs off that task's detail route. It accepts POST only.
- It sets completed to true on that task, stores the change, leaves every other task as it was, and answers 200 with that task's full representation - the seven serializer keys, with completed now true - and not with a status message.
- GET, PUT, PATCH and DELETE on that route are refused with 405, with an Allow header that names POST and names none of GET, PUT, PATCH or DELETE. None of the refused methods completes the task.
- A task id that is not in the table answers 404, the same as the detail route gives.
- Completing a task that is already complete changes nothing and answers 400 with exactly this body:
{"detail": "This task is already complete."}
5. An overdue action on the collection
- An overdue action acts on the collection, so its route is named task-overdue and hangs off the list route. It accepts GET only.
- It answers 200 with exactly the tasks whose due_on is before today and whose completed is false, each as the serializer's seven keys. A task past its date but already finished is not overdue, and neither is one due today.
- The tasks it leaves out stay in the table and in the full listing: this is a subset, not a deletion.
- POST, PUT, PATCH and DELETE on that route are refused with 405, with an Allow header that names GET and names none of POST, PUT, PATCH or DELETE.
6. The board summary
- The summary is not a task and has no row of its own, so it is an APIView on a route named board-summary, not a viewset action.
- A GET answers 200 with exactly these four keys, each a count over the board as it stands when the request arrives, recomputed per request rather than captured once:
{"total": 7, "completed": 3, "overdue": 2, "due_today": 1}
- total is every task on the board. completed is the tasks whose completed is true. overdue is the tasks whose due_on is before today and whose completed is false, the same rule as the overdue action. due_today is the tasks whose due_on is today and whose completed is false.
- POST, PUT, PATCH and DELETE are refused with 405, with an Allow header that names GET and names none of POST, PUT, PATCH or DELETE, and none of them changes the board.
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, with the status codes already correct.
- 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.
- perform_create is the hook the create action calls instead of saving directly, and anything passed to serializer.save() there is written onto the object alongside the validated body. self.request.user is the account the request arrived as, which is how a server-managed column stays server-managed: the serializer refuses to accept it and the view supplies it. The request user is supplied directly in the tests, so there is no sign-in step to build.
- 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.
- A GET-only DRF endpoint answers Allow: GET, HEAD, OPTIONS, because HEAD is served by the same code as GET and OPTIONS describes the endpoint. What matters is that the unsafe verbs are not in that list.
- Compare today's date with django.utils.timezone.localdate(), so overdue and due-today agree with the project's time zone.
- 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.
