project.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import time
  2. import uuid
  3. from typing import List
  4. from app import models, schemas
  5. from sqlalchemy.orm import Session
  6. from itsdangerous import BadSignature, SignatureExpired
  7. from itsdangerous import JSONWebSignatureSerializer as jws
  8. from constants.constants import SECRET_KEY
  9. from configs.globals import g
  10. # 生成新的project_token
  11. def generate_item_token(project: models.Project):
  12. s = jws(SECRET_KEY)
  13. return s.dumps({'project_id': project.id,'type':project.type}).decode('utf-8')
  14. # 验证Token方法
  15. def verify_item_token(db: Session, token: str):
  16. s = jws(SECRET_KEY)
  17. try:
  18. data = s.loads(token)
  19. except SignatureExpired:
  20. raise Exception("item_token验证失败")
  21. except BadSignature:
  22. raise Exception("item_token验证失败")
  23. project: models.Project = db.query(models.Project).filter(models.Project.id == data['project_id']).first()
  24. return project
  25. def create_project(db: Session, item: schemas.ProjectCreate):
  26. # 创建项目
  27. check_1 = db.query(models.Project).filter(models.Project.name == item.name).first()
  28. if check_1:
  29. raise Exception('项目名重复')
  30. project_code = str(uuid.uuid1()).replace('-','')
  31. db_item = models.Project(**{
  32. 'name': item.name,
  33. 'code': project_code,
  34. 'type': 1,
  35. 'create_time': int(time.time())
  36. })
  37. db.add(db_item)
  38. db.commit()
  39. db.refresh(db_item)
  40. project_token = generate_item_token(db_item)
  41. db_item.project_token = project_token
  42. db.commit()
  43. db.flush()
  44. db.refresh(db_item)
  45. project_dict = db_item.to_dict()
  46. # 创建超级管理员与项目的关系
  47. relation = models.ProjectUserRelation(**{
  48. 'user_id': g.user_id,
  49. 'project_id': db_item.id,
  50. 'role_id': 1,
  51. })
  52. db.add(relation)
  53. db.commit()
  54. db.refresh(relation)
  55. return project_dict
  56. def update_project(db: Session,item: schemas.ProjectUpdate):
  57. db_item: models.Project = db.query(models.Project).filter(models.Project.id == g.project_id).first()
  58. if not db_item:
  59. raise Exception('项目不存在')
  60. db_item.name = item.name
  61. db.commit()
  62. db.flush()
  63. db.refresh(db_item)
  64. return db_item
  65. def get_project_info(db: Session, id: int):
  66. project: models.Project = db.query(models.Project)\
  67. .filter(models.Project.id == id).first()
  68. if not project:
  69. raise Exception('该项目不存在')
  70. return project
  71. def switch_project_by_user(db: Session,project_id: int,user_id: int):
  72. project: models.Project = db.query(models.Project)\
  73. .filter(models.Project.id == project_id).first()
  74. if not project:
  75. raise Exception('项目不存在')
  76. relation: models.ProjectUserRelation = db.query(models.ProjectUserRelation)\
  77. .filter(models.ProjectUserRelation.project_id == project_id)\
  78. .filter(models.ProjectUserRelation.user_id == user_id).first()
  79. role: models.Roles = db.query(models.Roles)\
  80. .filter(models.Roles.id == relation.role_id).first()
  81. project_token = generate_item_token(project)
  82. project.project_token = project_token
  83. db.commit()
  84. db.flush()
  85. db.refresh(project)
  86. item = project.to_dict()
  87. item.update({
  88. 'role_id': role.id,
  89. 'role_name': role.name,
  90. 'role_code': role.code,
  91. })
  92. return item
  93. def get_project_list(db: Session, user_id: int):
  94. relations = get_relations_by_user(db, user_id)
  95. roles: List[models.Roles] = db.query(models.Roles).all()
  96. id_to_role = {r.id:r for r in roles}
  97. id_to_relation = { r.project_id:r for r in relations }
  98. projects: List[models.Project] = db.query(models.Project)\
  99. .filter(models.Project.id.in_(id_to_relation.keys())).all()
  100. res = []
  101. for project in projects:
  102. item = project.to_dict()
  103. role = id_to_role[id_to_relation[project.id].role_id]
  104. item.update({
  105. 'role_id': role.id,
  106. 'role_name': role.name,
  107. 'role_code': role.code,
  108. })
  109. res.append(item)
  110. return res
  111. def get_share_projects(db: Session):
  112. projects: List[models.Project] = db.query(models.Project)\
  113. .filter(models.Project.type==1).all()
  114. return projects
  115. def get_relations_by_user(db: Session, user_id: int):
  116. relations: List[models.ProjectUserRelation] = db.query(models.ProjectUserRelation)\
  117. .filter(models.ProjectUserRelation.user_id == user_id).all()
  118. return relations
  119. def get_relations_by_project(db: Session, project_id: int):
  120. relations: List[models.ProjectUserRelation] = db.query(models.ProjectUserRelation)\
  121. .filter(models.ProjectUserRelation.project_id == project_id).all()
  122. return relations