-
Notifications
You must be signed in to change notification settings - Fork 14
Backend
The backend for the prevention-point application is a thin RESTful interface around several models that provides a webserver to interact with and edit those models. This is a simple problem that has been solved for and rebuilt countless times, and we found that the Django ecosystem provided out-of-the box solutions for building an application like this with the least amount of custom code.
The code for the backend can all be found in the core directory in this repository.
The backend is written using the Django web framework.
We leverage the Django REST Framework in order to conveniently set up a RESTful interface around several different models that are required to store Prevention Point's data (e.g. Participant, Visit, Service). Each model has full CRUD (Create/Read/Update/Delete) built around it using Django REST Framework's ModelViewSet construct.
All of our models can be found here. Each model has its own directory in the root core directory, which contains a serializer and a view. These are Django conventions.
The serializer for each model provides an implementation for Django to transform the data in the database into JSON HTTP responses, and vise versa. These are typically very simple to implement, we don't usually need to do any additional transformations for our use-case on top of the data in the database. For example, the serializer on top of services simply tells Django which fields to pass between the database and the user.
The view for each model is where we generally simply implement ModelViewSet, which gives us several API routes out of the box. For example, given a model called service and a ModelViewSet around that model, we would immediately have the following routes:
-
GET /api/service- Retrieve all services -
GET /api/service/<service_id>- Retrieve a service by ID -
POST /api/service- Create a service -
POST /api/service/<service_id>- Update a service by ID, given all information about that service -
PATCH /api/service/<service_id>- Partially update a service by ID -
DELETE /api/service/<service_id>- Delete a service by ID
Django comes with an authentication system out of the box, which we leverage to provide role-based authentication to our API endpoints. The documentation for Django's authentication system can be found here.