/agro-business-dashboard

Creating a simple CMS-style dashboard for a challenge

Primary LanguagePythonMIT LicenseMIT

Agro Business Api Challenge

Creating a simple API for dashboard to challenge

Python Version Django Version Django Rest Framework Version

Small challenge project, creating a REST API with Django, aimed at agrobusiness.

Installation

Environment Local

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 compiler python manage.py <command>.

Docker Build

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 the DEBUG 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.

Usage

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!

Formatters and Linters

Obs:

  • Programming with Python, we use the snake_case style for variables, functions and methods, and the PascalCase style for classes. Configuration variables should written in UPPERCASE.

Structure

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.

Tests

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>

Tips

In this session, we include several articles related to good practices, tools and more.

Resources and Documentations

Contributing

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.