data_management.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from re import S
  2. import time
  3. from typing import List
  4. from app import models, schemas
  5. from sqlalchemy.orm import Session
  6. from configs.globals import g
  7. def create_data_management(db: Session, item: schemas.DataManagementCreate, table_name: str, af_run_id: str):
  8. create_time: int = int(time.time())
  9. db_item = models.DataManagement(**{
  10. 'name': item.name,
  11. 'table_name': table_name,
  12. 'create_time': create_time,
  13. 'user_name': g.user_name,
  14. 'user_id': g.user_id,
  15. 'project_id': g.project_id,
  16. 'af_run_id': af_run_id,
  17. 'status': 1
  18. })
  19. db.add(db_item)
  20. db.commit()
  21. db.refresh(db_item)
  22. return db_item
  23. def update_data_management_status(db: Session, d_id: int, status: int):
  24. db_item: models.DataManagement = db.query(models.DataManagement).filter(models.DataManagement.id == d_id).first()
  25. db_item.status = status
  26. db.commit()
  27. db.flush()
  28. db.refresh(db_item)
  29. return db_item
  30. def get_data_managements(db: Session, user_id: str, project_id: str):
  31. res: List[models.DataManagement] = db.query(models.DataManagement)\
  32. .filter(models.DataManagement.project_id == project_id).all()
  33. return res
  34. def get_data_management_info(db: Session, d_id: int):
  35. db_item: models.DataManagement = db.query(models.DataManagement).filter(models.DataManagement.id == d_id).first()
  36. if not db_item:
  37. raise Exception('该数据不存在')
  38. return db_item
  39. def delete_data_management(db: Session, d_id: int):
  40. dm_item = db.query(models.DataManagement).filter(models.DataManagement.id == d_id).first()
  41. if not dm_item:
  42. raise Exception("Data management not found")
  43. db.delete(dm_item)
  44. db.commit()
  45. db.flush()
  46. return dm_item