guidance.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. from fastapi import APIRouter, Form, Depends, HTTPException, File, UploadFile
  2. from typing import List,Optional,Union
  3. from fastapi.responses import FileResponse
  4. from random import randint
  5. from fastapi.security import OAuth2PasswordRequestForm
  6. from app.models.models import Guidance_group
  7. from app.api import deps
  8. from sqlalchemy.orm import Session
  9. from typing import Any, Dict
  10. import secrets
  11. from fastapi_login.exceptions import InvalidCredentialsException
  12. from fastapi_login import LoginManager
  13. from datetime import timedelta,datetime
  14. from jose import jwt
  15. from emails.template import JinjaTemplate
  16. from tortoise.queryset import Q
  17. from fastapi.responses import HTMLResponse
  18. guidance = APIRouter()
  19. @guidance.get("/get_guidance_group")
  20. async def get_guidance_group(
  21. guidance_group_id : Optional[int] = None
  22. ):
  23. try:
  24. if guidance_group_id :
  25. news_list = await Guidance_group.filter(id=guidance_group_id).all()
  26. else:
  27. news_list = await Guidance_group.all()
  28. news_objs = []
  29. for news_obj in news_list:
  30. news_tmp = {
  31. "guidance_group_id": news_obj.id,
  32. "name": news_obj.name,
  33. "contact_person": news_obj.contact_person,
  34. "contact_phone" : news_obj.contact_phone,
  35. "line" : news_obj.line,
  36. "remark" : news_obj.remark,
  37. "address" : news_obj.address,
  38. "email" : news_obj.email,
  39. "information" : news_obj.information,
  40. "request" : news_obj.request,
  41. "schedule": news_obj.schedule,
  42. "result" : news_obj.result,
  43. "person" : news_obj.person
  44. }
  45. news_objs.append(news_tmp)
  46. return {"msg": "success", "code": 200, "Guidance_groups": news_objs}
  47. except Exception as e:
  48. return {"msg": str(e), "code": 500}
  49. @guidance.post("/insert_guidance_group")
  50. async def insert_news(
  51. name : str = Form(default=''),
  52. contact_person : str = Form(default=''),
  53. contact_phone :str = Form(default=''),
  54. line : int = Form(default=0),
  55. remark : str = Form(default=''),
  56. address : str = Form(default=''),
  57. email : str = Form(default=''),
  58. information : str = Form(default=''),
  59. request : str = Form(default=''),
  60. schedule : str = Form(default=''),
  61. result : str = Form(default=''),
  62. person : str = Form(default='')
  63. ):
  64. try:
  65. new_news = await Guidance_group.create(
  66. name=name,
  67. contact_person=contact_person,
  68. contact_phone=contact_phone,
  69. line = line,
  70. email = email,
  71. remark =remark,
  72. address = address,
  73. information = information,
  74. request = request,
  75. schedule = schedule ,
  76. result = result,
  77. person = person
  78. )
  79. return {"msg": "success", "code": 200, "new_Guidance_group": new_news.id}
  80. except Exception as e:
  81. return {"msg": str(e), "code": 500}
  82. @guidance.post("/update_guidance_group")
  83. async def update_news(
  84. guidance_group_id : int = Form(default=None),
  85. name : str = Form(default=''),
  86. contact_person : str = Form(default=''),
  87. contact_phone :str = Form(default=''),
  88. line : int = Form(default=None),
  89. remark : str = Form(default=''),
  90. address : str = Form(default=''),
  91. email : str = Form(default=''),
  92. information : str = Form(default=''),
  93. request : str = Form(default=''),
  94. schedule : str = Form(default=''),
  95. result : str = Form(default=''),
  96. person : str = Form(default='')
  97. ):
  98. try:
  99. if not guidance_group_id :
  100. return {"msg": "no ID"}
  101. news_obj = await Guidance_group.get(id=guidance_group_id)
  102. if name.strip() != '':
  103. news_obj.name = name
  104. if contact_person.strip() != '':
  105. news_obj.contact_person = contact_person
  106. if contact_phone.strip() != '':
  107. news_obj.contact_phone = contact_phone
  108. if line != None:
  109. news_obj.line = line
  110. if email != None:
  111. news_obj.email = email
  112. if information != None:
  113. news_obj.information = information
  114. if request != None:
  115. news_obj.request = request
  116. if schedule != None:
  117. news_obj.schedule = schedule
  118. if result != None:
  119. news_obj.result = result
  120. if person != None:
  121. news_obj.person = person
  122. if remark.strip() != '':
  123. news_obj.remark = remark
  124. if address.strip() != '':
  125. news_obj.address = address
  126. await news_obj.save()
  127. return {"msg": "success", "code": 200}
  128. except Exception as e:
  129. return {"msg": str(e), "code": 500}
  130. @guidance.post("/delete_guidance_group")
  131. async def delete_guidance_group(guidance_group_id: Optional[int] = None):
  132. if guidance_group_id :
  133. await Guidance_group.filter(id=guidance_group_id).delete()
  134. return {"msg": "success", "code": 200}
  135. else :
  136. return {"msg": "please input ID", "code": 200}