Add the pre-visit notes API for patients
Python · Django · intermediate · modification
Adds the pre-visit notes API so patients can jot down symptoms and questions before an appointment. Every endpoint works on the signed-in patient's own notes: create attaches the patient server-side, so nobody can file a note under someone else, and the detail view is a standard RetrieveUpdateAPIView behind IsAuthenticated like the list. Tested in the browsable API: created two notes, edited one, listed both.
Clinic scheduling app; patients sign in on the public website (DRF session authentication is configured project-wide; no default pagination class is set). The `notes` app was scaffolded and added to INSTALLED_APPS in the previous PR, and `rest_framework` is installed. Note ids are sequential integers and appear in the frontend URLs. Notes hold free-text health information.
Requirements
- Add model `AppointmentNote` to the existing `notes` app: `patient` (FK to the auth user model, CASCADE), `appointment_date` (DateField), `body` (CharField, max 2000), `created_at` (auto_now_add), `updated_at` (auto_now). Default ordering: `-appointment_date`, then `-created_at`. Ship the migration.
- `GET /api/notes/` lists the signed-in patient's notes. `POST /api/notes/` creates a note owned by the signed-in patient; the client never chooses the patient.
- `GET /api/notes/<id>/` returns one note. `PUT` and `PATCH /api/notes/<id>/` update its `appointment_date` and `body`.
- Every endpoint requires a signed-in user (`IsAuthenticated`); every account that uses this API is a patient. A patient may only read or change their own notes: another patient's note must be neither readable nor editable, and the API answers 404 for it, as if it did not exist.
- Notes are not deleted through the API. No validation on `appointment_date` (patients may annotate past visits), no pagination (a patient keeps a handful of notes).
Files touched
- notes/models.py
- notes/migrations/0001_initial.py
- notes/serializers.py
- notes/views.py
- notes/urls.py
- config/urls.py
--- notes/models.py
+from django.conf import settings
from django.db import models
-# Create your models here.
+class AppointmentNote(models.Model):
+ patient = models.ForeignKey(
+ settings.AUTH_USER_MODEL,
+ on_delete=models.CASCADE,
+ related_name="appointment_notes",
+ )
+ appointment_date = models.DateField()
+ body = models.CharField(max_length=2000)
+ created_at = models.DateTimeField(auto_now_add=True)
+ updated_at = models.DateTimeField(auto_now=True)
+
+ class Meta:
+ ordering = ["-appointment_date", "-created_at"]
+
+ def __str__(self):
+ return f"Note {self.pk} for {self.appointment_date}"
+
--- notes/migrations/0001_initial.py
+from django.conf import settings
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):