tgc.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 Tgc_pic,Tgc_report
  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. tgc = APIRouter()
  19. IMAGEDIR = "/var/www/ntcri/assets/tgc_files/"
  20. IMAGEDIR_short = "assets/tgc_files/"
  21. @tgc.post("/upload_tgc_pic")
  22. async def create_upload_files(files:Optional[List[UploadFile]] = File(None)):
  23. files_url = []
  24. if files :
  25. for file in files:
  26. contents = await file.read()
  27. #save the file
  28. with open(f"{IMAGEDIR}{file.filename}", "wb") as f:
  29. f.write(contents)
  30. files_url.append(f"{IMAGEDIR_short}{file.filename}")
  31. return files_url
  32. @tgc.get("/get_tgc_report")
  33. async def get_tgc_report(
  34. id : Optional[int] = None,
  35. keyword : Optional[str] = None,
  36. page_num : Optional[int] = None,
  37. page_amount : Optional[int] = None
  38. ):
  39. try:
  40. report_list = Tgc_report.all()
  41. if id :
  42. report_list = report_list.filter(id = id).all()
  43. if keyword:
  44. for word in keyword.split(","):
  45. report_list = report_list.filter(title__icontains = word).all()
  46. count = await report_list.all().count()
  47. if page_num and page_amount:
  48. report_list = report_list.offset((page_num-1)*page_amount).limit(page_amount)
  49. report_list = await report_list.all().order_by("-create_time")
  50. result = []
  51. for report_obj in report_list:
  52. try :
  53. result.append(report_obj.show_data())
  54. except Exception as e:
  55. result.append({"msg": str(e)})
  56. return {"msg": "success", "code": 200, "total_num":count,"tgc_report": result }
  57. except Exception as e:
  58. return {"msg": str(e), "code": 500}
  59. @tgc.post("/input_tgc_report")
  60. async def input_tgc_report(
  61. title : str = Form(default=''),
  62. create_time : str = Form(default=datetime.now()),
  63. url : str = Form(default='')
  64. ):
  65. try:
  66. new_report = await Tgc_report.create(
  67. title = title,
  68. create_time = create_time,
  69. url = url
  70. )
  71. return {"msg": "success", "code": 200, "new_report": new_report.id}
  72. except Exception as e:
  73. return {"msg": str(e), "code": 500}
  74. @tgc.get("/get_tgc_img")
  75. async def get_tgc_img(
  76. id : Optional[int] = None,
  77. page_num : Optional[int] = None,
  78. page_amount : Optional[int] = None
  79. ):
  80. try:
  81. tgc_pic_list = Tgc_pic.all()
  82. Q_word=Q()
  83. if id :
  84. Q_word = Q(id=id)
  85. tgc_pic_list = tgc_pic_list.filter(Q_word)
  86. count = await tgc_pic_list.all().count()
  87. if page_num and page_amount:
  88. tgc_pic_list = tgc_pic_list.offset((page_num-1)*page_amount).limit(page_amount)
  89. tgc_pic_list= await tgc_pic_list.all()
  90. result = []
  91. for obj in tgc_pic_list:
  92. result.append(obj.show_data())
  93. return {"msg": "success", "code": 200, "total_num":count,"tgc_pic": result }
  94. except Exception as e:
  95. return {"msg": str(e), "code": 500}