Creating a simple API for dashboard to challenge
Small challenge project, creating a REST API with Django, aimed at agrobusiness.
Using your dependency manager, create a python environment, follow a link talking about the managers!
Access the project folder and using the pip manager, inside the python env, apply the command below:
pip install --upgrade pip && pip install --require-hashes -r requirements/dev.txt
After installing all dependencies, you must create a root user and apply the database migrations. The command to execute is:
./manage.py migrate && ./manage.py loaddata test_data.json
To compile the project, apply the command below:
./manage.py runserver
Obs:
-
The database configured in the project is sqlite, to configure another just follow this link
-
Remember that you must not reconfigure the settings.py module, so you must follow good practices and create a local_settings module to run locally or for development and testing!
-
Based on the good security practices of the projects, we are using pip-tools as a tool to generate the hashes. If you want to know a little more about it, see the documentation and links in the tips!
-
Depending on your OS version, you can run django by just calling the file like this
./manage.py <command>
or using the python compilerpython manage.py <command>
.
You will need to have docker compose, and finally apply the command:
docker compose up --build
If you need to execute another command for django and python, just follow structure below:
docker compose exec backend python manage.py <command you want>
Obs:
- The project preloads some basic data, such as a list of states and a default admin, for testing purposes. The admin login and password are
admin
,12345
. - For local tests, simply copy
env.example
to the.env
file in the project's root folder and run the application if building, don't forget to remove the instruction from theDEBUG
context. - To run the unit tests, just follow the pytest instructions.
- If the swagger doesn't work to download the copy of the file, just access its
api/schema
route and it will download the file to be used in an APIClient of your choice.
In order to be able to normalize, we add the best practices in this project, aiming to respect the principles with example Clean Code, SOLID and others. For more details, see the tip links!
Obs:
- Programming with Python, we use the
snake_case
style for variables, functions and methods, and thePascalCase
style for classes. Configuration variables should written inUPPERCASE
.
We use the MTV(Model-Template-Views) architecture patterns, associated with SOA(Service Oriented Architecture) and Clean Code principles, to create API resources. To example, see the content:
#For testing the APIs, follow the pattern in this block
├── agrobusiness/
│ ├── migrations/
│ │ ├── 0001_initial.py
│ │ └── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── models.py
│ ├── serializers.py
│ ├── urls.py
│ └── views.py
├── core/
│ ├── asgi.py
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── fixtures/
│ ├── adminuser.json
│ └── state.json
├── requirements/
│ ├── base.in*
│ ├── base.txt
│ ├── dev.in*
│ └── dev.txt
├── scripts/
│ └── nginx.conf
├── tests/
│ ├── agrobusiness/
│ │ ├── conftest.py
│ │ ├── __init__.py
│ │ ├── test_customer_endpoints.py
│ │ ├── test_farm_endpoints.py
│ │ ├── test_models.py
│ │ ├── test_planting_endpoints.py
│ │ ├── test_state_endpoints.py
│ │ └── test_token.py
│ └── __init__.py
├── docker-compose.yml*
├── Dockerfile*
├── env.example*
├── LICENSE
├── manage.py*
├── .pre-commit-config.yaml #Settings file for pre-commits hook
├── .editorconfig #Editorconfig settings file
├── pyproject.toml* #Formatters and project configuration file
└── README.md
Obs:
- For normalization of routes, it is ideal to use routers and viewsets from DRF.
- To include a new lib (package), insert it in the appropriate file (base.in or dev.in) and finally update it with pip-tools.
The django test platform is based on pytest, therefore following the basic principles for implementation and run, follow the relative documentation link. Based on this, we have a practical example of how to run the tests, taken from the documentation:
# Run all the tests in the animals.tests module
$ pytest tests
# Run all the tests found within the 'animals' package
$ pytest tests/<module-you-want-test>
# Run just one test case
$ pytest tests/<module-you-want-test>::<function-you-want-test>
Obs:
- To run the tests inside a docker container, you just need to apply the following command prefix
docker compose exec backend pytest tests <same commands above>
In this session, we include several articles related to good practices, tools and more.
- Tips for pip-tools and multple environments
- Locking dependency with pip-tools
- Tips for environments python
- SOLID Principle
- Clean Code and principles
- Environments python, what is have choosen?
- Overview of python dependency management tools
- Best pratices for development environment with Django
- Types of development styles
- Best pratices with Django
- Django Translation messages
- Best pratices in Python
- Flake 8 best configurations
- Black tips
- Multiple database configuration in settings
- Defining database in settings
- DRF Routers
- DRF Viewsets
- Difference between Views, Generics Views and Viewsets
- ReDocs and Swagger with DRF
- Tips with DRF
- Best pratices with DRF
- TDD with Django
- How to use Django Debug Toolbar?
- Pequeno tutorial sobre Django Debug Toolbar
- Django and Architeture MTV Pattern
- Tips for Scaling Django Apps
- EditorConfig tips
- Pre-commits tips
- Pre-commits Automating
- Why use Gitignore Global?
- Pip (Package Installer Python)
- Pip Tools
- Django
- Django Rest Framework
- Django Cors Headers
- Django Extensions
- Django Debug Toolbar
- Linter Flake8
- Formatter Black
- Daphne
- Gunicorn
- Docker
- Docker Compose
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.