base.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. """
  2. Base logic module
  3. Not specialize in a specific user
  4. """
  5. from typing import Any
  6. from aiogram.dispatcher import FSMContext
  7. from app.models.db import BaseModel
  8. async def set_state_data(state: FSMContext, **state_data) -> None:
  9. """
  10. Set data into user's state obj
  11. :param state: user's state obj
  12. :keywords state_data data that will be set into the state obj
  13. :returns: None
  14. """
  15. await state.update_data(**state_data)
  16. async def set_data_to_db(model: BaseModel, **data):
  17. """
  18. Set data to db through the model
  19. :param model: model with which we will set data
  20. :keywords data: data that will be set to db
  21. :returns: None
  22. """
  23. await model.update(**data).apply()
  24. async def get_model_obj_from_db_by_id(model: BaseModel, id: Any):
  25. """
  26. Get model object from db by id
  27. Used for getting solution (photo) object
  28. :param model: model that contains `id` column
  29. :param id: used fot getting object by id
  30. :returns: model object
  31. """
  32. return await model.get(id)