guidance.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. return {"msg": "success", "code": 200, "Guidance_groups": news_list}
  29. except Exception as e:
  30. return {"msg": str(e), "code": 500}
  31. @guidance.post("/insert_guidance_group")
  32. async def insert_news(
  33. name : str = Form(default=''),
  34. contact_person : str = Form(default=''),
  35. contact_phone :str = Form(default=''),
  36. line : int = Form(default=0),
  37. remark : str = Form(default=''),
  38. address : str = Form(default=''),
  39. email : str = Form(default=''),
  40. information : str = Form(default=''),
  41. request : str = Form(default=''),
  42. schedule : str = Form(default=''),
  43. result : str = Form(default=''),
  44. person : str = Form(default=''),
  45. create_time :str = Form(default=datetime.now())
  46. ):
  47. try:
  48. new_news = await Guidance_group.create(
  49. name=name,
  50. contact_person=contact_person,
  51. contact_phone=contact_phone,
  52. line = line,
  53. email = email,
  54. remark =remark,
  55. address = address,
  56. information = information,
  57. request = request,
  58. schedule = schedule ,
  59. result = result,
  60. person = person,
  61. create_time = create_time
  62. )
  63. return {"msg": "success", "code": 200, "new_Guidance_group": new_news.id}
  64. except Exception as e:
  65. return {"msg": str(e), "code": 500}
  66. @guidance.post("/update_guidance_group")
  67. async def update_news(
  68. guidance_group_id : int = Form(default=None),
  69. name : str = Form(default=''),
  70. contact_person : str = Form(default=''),
  71. contact_phone :str = Form(default=''),
  72. line : int = Form(default=None),
  73. remark : str = Form(default=''),
  74. address : str = Form(default=''),
  75. email : str = Form(default=''),
  76. information : str = Form(default=''),
  77. request : str = Form(default=''),
  78. schedule : str = Form(default=''),
  79. result : str = Form(default=''),
  80. person : str = Form(default='')
  81. ):
  82. try:
  83. if not guidance_group_id :
  84. return {"msg": "no ID"}
  85. news_obj = await Guidance_group.get(id=guidance_group_id)
  86. if name.strip() != '':
  87. news_obj.name = name
  88. if contact_person.strip() != '':
  89. news_obj.contact_person = contact_person
  90. if contact_phone.strip() != '':
  91. news_obj.contact_phone = contact_phone
  92. if line != None:
  93. news_obj.line = line
  94. if email != None:
  95. news_obj.email = email
  96. if information != None:
  97. news_obj.information = information
  98. if request != None:
  99. news_obj.request = request
  100. if schedule != None:
  101. news_obj.schedule = schedule
  102. if result != None:
  103. news_obj.result = result
  104. if person != None:
  105. news_obj.person = person
  106. if remark.strip() != '':
  107. news_obj.remark = remark
  108. if address.strip() != '':
  109. news_obj.address = address
  110. await news_obj.save()
  111. return {"msg": "success", "code": 200}
  112. except Exception as e:
  113. return {"msg": str(e), "code": 500}
  114. @guidance.post("/delete_guidance_group")
  115. async def delete_guidance_group(guidance_group_id: Optional[int] = None):
  116. if guidance_group_id :
  117. await Guidance_group.filter(id=guidance_group_id).delete()
  118. return {"msg": "success", "code": 200}
  119. else :
  120. return {"msg": "please input ID", "code": 200}