Skip to content

Backend

Matt Usifer edited this page Oct 20, 2019 · 4 revisions

Back End Structure

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.

Django

The backend is written using the Django web framework.

API

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.

Serializers

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.

Views

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:

  1. GET /api/service - Retrieve all services
  2. GET /api/service/<service_id> - Retrieve a service by ID
  3. POST /api/service - Create a service
  4. POST /api/service/<service_id> - Update a service by ID, given all information about that service
  5. PATCH /api/service/<service_id> - Partially update a service by ID
  6. DELETE /api/service/<service_id> - Delete a service by ID

Authentication

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.

Database

We currently use PostgreSQL to store our data, but Django can easily be pointed at a different supported database system and all of this backend code should work seamlessly. Supported database systems can be found here.

Pipenv

We manage our python dependencies using pipenv. This allows us to lock our dependencies to specific versions in order to replicate successful builds, preventing unintended dependency updates. Therefore, the exact versions of all of our dependencies can be found in Pipfile.lock, and they are managed in the Pipfile.