utils.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from typing import Dict, Union, List, Any, Optional
  4. from sqlalchemy.ext.asyncio import AsyncSession
  5. from crud.school import crud_school, crud_class, crud_grade
  6. from crud.sysdata.role import crud_role
  7. from crud.user import crud_teacher, crud_student
  8. CRUD_DICT = {
  9. "school": crud_school,
  10. "grade": crud_grade,
  11. "class": crud_class,
  12. "teacher": crud_teacher,
  13. "student": crud_student,
  14. "role": crud_role
  15. }
  16. async def is_existed(db: AsyncSession, obj_type: str, *,
  17. filters: Union[List[Any], Dict[str, Any]]):
  18. if obj_type not in CRUD_DICT:
  19. return None
  20. obj = await CRUD_DICT[obj_type].find_one(db, filters=filters)
  21. return obj
  22. async def check_row(values: List[Any], length: int) -> Any:
  23. _val = []
  24. for x in values[:length]:
  25. if isinstance(x, str):
  26. _val.append(x.strip())
  27. else:
  28. _val.append(x)
  29. if not _val:
  30. return None
  31. elif len(_val) != length:
  32. return False
  33. return _val