constant.py 890 B

1234567891011121314151617181920212223242526272829
  1. import time
  2. from typing import List
  3. from app import models, schemas
  4. from sqlalchemy.orm import Session
  5. def create_constant(db: Session,item: schemas.ConstantCreate):
  6. db_item = models.Constant(**item.dict())
  7. db.add(db_item)
  8. db.commit()
  9. db.refresh(db_item)
  10. return db_item
  11. def get_constant_list(db: Session, type: str):
  12. res: List[models.Constant] = db.query(models.Constant).filter(models.Constant.type == type).all()
  13. return [r.value for r in res]
  14. def find_and_update(db: Session, type: str, value: str):
  15. res: List[models.Constant] = db.query(models.Constant)\
  16. .filter(models.Constant.type == type)\
  17. .filter(models.Constant.value == value).all()
  18. if len(res) == 0:
  19. db_item = models.Constant(**{
  20. 'type': type,
  21. 'value': value
  22. })
  23. db.add(db_item)
  24. db.commit()
  25. db.refresh(db_item)