Library control system on Flask-Potion, Part 0: preparing everything you need

Introduction



In my work, I have been using Flask-Potion for some time now, a framework whose main advantages are: very convenient integration with SQLAlchemy models, autogeneration of crud endpoints, the presence of a potion-client (very convenient if you write an API service that you will need to use in another service).







I noticed that there is almost nothing about flask-potion in Russian, but I think this framework may seem interesting to some.







Instead of a simple review article on this framework, I decided to write several articles on creating a control system for the Furfur library based on Flask-Potion.







This system should be able to do the following:









In this system we will use the following tools:









Training



Skeleton



In order not to collect the skeleton for the project ourselves, we will use the Valefor cookiecutter-template, which includes all of the above dependencies and even more.







cookiecutter gh:lemegetonx/valefor
      
      





This template includes two applications:







  1. app is the main thing. It contains handler functions for jwt, mixin classes for potion resources and sqlalchemy models, as well as a package with configurations for the application.
  2. user - at the start of the template, contains only the user model.


Dependency Installation



The template uses poetry to resolve dependencies, but recently pip also supports pyproject.toml , so the choice is yours. I will take advantage of poetry.







 poetry install
      
      





Configuration



For simplified configuration, the template uses the sitri library. We will need to slightly modify the setting of the Sitri object.







  1. Change app / config / provider.py . We will replace SystemCredentialProvider with YamlCredentialProvider so that authorization data for third-party systems is taken from the credential.yaml file, which we will not add to commits:


 from sitri import Sitri from sitri.contrib.yaml import YamlConfigProvider, YamlCredentialProvider configuration = Sitri( config_provider=YamlConfigProvider(yaml_path="./config.yaml"), credential_provider=YamlCredentialProvider(yaml_path="./credential.yaml"), )
      
      





PS more about what is actually happening here is easier to read in the documentation , in short, now we just decided where we will get the data for configuration and authorization from.







  1. Since we essentially made the same providers, it is better to replace the underscores in the keys in the get_credential call with dots in database.py .


 DB_NAME = configuration.get_credential("db.name", path_mode=True) DB_HOST = configuration.get_credential("db.host", path_mode=True) DB_PASSWORD = configuration.get_credential("db.user.password", path_mode=True) DB_PORT = configuration.get_credential("db.port", path_mode=True) DB_USER = configuration.get_credential("db.user.name", path_mode=True)
      
      





So, the config.yaml file was already in the template, but credential.yaml should be written by yourself. In real life, such files are necessarily added to .gitignore, but I will add the credential.yaml template to the repository so that its structure is understandable to anyone who enters the project.







Base credential.yaml :







 db: name: furfur_db host: localhost port: 5432 user: password: passwd name: admin
      
      





Database



The next stage of our preparation is the deployment of the DBMS, in this case PostgreSQL. For convenience, I will make a stack.yaml file where I will describe the launch of the postgres container with the data we need.







 version: '3.1' services: db: image: postgres restart: always environment: POSTGRES_PASSWORD: passwd POSTGRES_USER: admin POSTGRES_DB: furfur_db ports: - 5432:5432
      
      





As mentioned earlier, the valefor template includes the basic User model needed for the JWT (handlers) to work, so the final step in preparing the database is migration (creating a user table).







Being at the root of the project, we execute the following commands:







 export FLASK_APP=furfur.app flask db init flask db migrate flask db upgrade
      
      





Everything, with the preparation of the database, as well as the overall basis for our system, we are done.







What's next?



In the next part, we will talk about how to organize a simple role system and JWT authentication.







Project repository: https://github.com/Egnod/furfur

Everything that is stated in this part: https://github.com/Egnod/furfur/releases/tag/0.0.2








All Articles