utils.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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, *, filters: Union[List[Any], Dict[str, Any]]):
  17. if obj_type not in CRUD_DICT:
  18. return None
  19. obj = await CRUD_DICT[obj_type].find_one(db, filters=filters)
  20. return obj
  21. async def check_row(values: List[Any], length: int) -> Any:
  22. _val = []
  23. for x in values[:length]:
  24. if isinstance(x, str):
  25. _val.append(x.strip())
  26. else:
  27. _val.append(x)
  28. if not any(_val):
  29. return None
  30. elif len(_val) != length:
  31. return False
  32. return _val
  33. def check_filetype(filename: str, suffix: str) -> bool:
  34. return filename.endswith(suffix)