system-pri/backend/Readme.md

118 lines
2.1 KiB
Markdown
Raw Normal View History

2022-05-15 21:20:05 +02:00
## Installation
Python 3.8 version is required.
```bash
python3 --version
```
Create virtual environment and install packages
```bash
virtualenv venv
source ./venv/bin/activate
pip install -r requirements.txt
```
***
## Usage
Create `.env` file and fill with similar data like `.env.example`.
Then run application:
```bash
flask run
```
***
## Testing
Run tests
```bash
pytest
```
Run all tests in specific python module
```bash
pytest ./tests/unit_tests/test_file.py
```
Run one test inside of python module
```bash
pytest ./tests/unit_tests/test_file.py::test_function_name
```
2022-05-15 21:20:05 +02:00
***
### Format code:
```bash
black path/to/file.py
isort path/to/file.py
flake8
```
2022-05-15 21:20:05 +02:00
### Useful commands:
Add new package
```bash
pip install NAME_OF_PACKAGE
```
Save new added package to requirements
```bash
pip freeze > requirements.txt
```
***
#### Flask commands:
Create the application structure directories
```bash
flask startapp NAME_OF_APP
```
Above command create package structure:
\
Create serializer in `schemas.py` file:
```python3
from marshmallow import Schema, fields
class ExampleSchema(Schema):
id = fields.Integer()
name = fields.Str()
2022-05-15 21:20:05 +02:00
```
\
Create models in `models.py` file:
2022-05-15 21:20:05 +02:00
```python3
from ..dependencies import db
2022-05-15 21:20:05 +02:00
class Example(db.Model):
__tablename__ = "examples"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
title = db.Column(db.String(255), unique=True)
```
\
Create api routes in ```routes.py``` file. You only have to register blueprint in `app/__init__.py`.
2022-05-15 21:20:05 +02:00
```python3
from flask import Blueprint, make_response, jsonify, Response
bp = Blueprint("{name}", __name__, url_prefix="/{name}")
@bp.route("/", methods=["GET"])
def index() -> Response:
return make_response(jsonify({{"hello": "{name}"}}), 200)
```
\
Print all your routes
```bash
flask routes
```
Create migration, but first you have to create model
```bash
flask db migrate
```
Apply your changes to database, use after migration command
```bash
flask db upgrade
```
If you want back changes in your databases, use below command
```bash
flask db downgrade
```
Create empty migration, you can write manually migration here.
```bash
flask db revision
```