PROJECT ROOT
├── api # App containing all project-specific apps
│ ├── apps.py
│ ├── authentication # Implements authentication app logic (register, login, session) logic
│ │ ├── apps.py
│ │ ├── backends.py # Handles the active session authentication
│ │ ├── migrations
│ │ ├── serializers
│ │ │ ├── login.py # Handles the proccess of login for an user
│ │ │ └── register.py # Handle the creation of a new user
│ │ ├── tests.py # Test for login, registration and session
│ │ └── viewsets
│ │ ├── active_session.py # Handles session check
│ │ ├── login.py # Handles login
│ │ ├── logout.py # Handles logout
│ │ └── register.py # Handles registration
| |
│ ├── fixtures # Package containg the project fixtures
│ ├── __init__.py
│ ├── routers.py # Define api routes
│ └── user # Implements user app logic
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ ├── serializers.py # Handle the serialization of user object
│ └── viewsets.py # Handles the modification of an user
|
├── core # Implements app logic
│ ├── asgi.py
│ ├── __init__.py
│ ├── settings.py # Django app bootstrapper
│ ├── test_runner.py # Custom test runner
│ ├── urls.py
│ └── wsgi.py
|
├── docker-compose.yml
├── Dockerfile
├── .env # Inject Configuration via Environment
├── manage.py # Starts the app
└── requirements.txt # Contains development packages
Used Patterns
Working with Django Rest Framework, the most common design pattern is the Template Method Pattern.
It mostly consists of providing base/skeleton for some features with the possibility to override/extends these skeletons.
For example, you can check the code in api/user/viewsets.py. The UserViewSet inherits of viewsets.GenericsViewSet and CreateModelMixin and UpdateModelMixin.
The UpdateModelMixin provides the logic to update an object using PUT.
We only need to rewrite the method which handles the updating and provides the serializer_class and the permission_classes.
How to use the API
POSTMAN usage
The API is actually built around these endpoints :
api/users/signup
api/users/login
api/users/edit
api/users/checkSession
api/users/logout
Register - api/users/register
POST api/users/registerContent-Type: application/json{"username":"test","password":"pass","email":"test@appseed.us"}
Response :
{"success": true,"userID": 1,"msg": "The user was successfully registered"}
Login - api/users/login
cd api && django-admin startapp transactions
Once it's done, rewrite the apps.py file with the following content.
A serializer allows us to convert complex Django complex data structures such as querysets or model instances in Python native objects that can be easily converted JSON/XML format, but a serializer also serializes JSON/XML to naive Python.
To avoid name collision with the default built-in method create , we are naming the method create_transaction. Hopefully, DRF provides the option to specify the url_path of the method.
Let's write the actions for api/transactions/get and api/transactions/get/id