constant.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import time
  2. from typing import List
  3. from app import models, schemas
  4. from sqlalchemy.orm import Session
  5. from configs.settings import DefaultOption, config
  6. python_image = config.get('TASK_IMAGES', 'python')
  7. java_image = config.get('TASK_IMAGES', 'java')
  8. def create_constant(db: Session,item: schemas.ConstantCreate):
  9. db_item = models.Constant(**item.dict())
  10. db.add(db_item)
  11. db.commit()
  12. db.refresh(db_item)
  13. return db_item
  14. def get_constant_list(db: Session, type: str):
  15. if type == 'pythonImage':
  16. return [python_image]
  17. elif type == 'javaImage':
  18. return [java_image]
  19. else:
  20. res: List[models.Constant] = db.query(models.Constant).filter(models.Constant.type == type).all()
  21. return [r.value for r in res]
  22. def find_and_update(db: Session, type: str, value: str):
  23. res: List[models.Constant] = db.query(models.Constant)\
  24. .filter(models.Constant.type == type)\
  25. .filter(models.Constant.value == value).all()
  26. if len(res) == 0:
  27. db_item = models.Constant(**{
  28. 'type': type,
  29. 'value': value
  30. })
  31. db.add(db_item)
  32. db.commit()
  33. db.refresh(db_item)
  34. def delete_constant(db: Session, type: str, value: str):
  35. db_item = db.query(models.Constant)\
  36. .filter(models.Constant.type == type)\
  37. .filter(models.Constant.value == value).first()
  38. if not db_item:
  39. raise Exception('未找到该常量')
  40. else:
  41. db.query(models.Constant)\
  42. .filter(models.Constant.type == type)\
  43. .filter(models.Constant.value == value).delete()
  44. db.commit()
  45. db.flush()
  46. return db_item