registration.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. from fastapi_login import LoginManager
  2. from fastapi import APIRouter, Form, Depends, HTTPException, File, UploadFile
  3. from typing import List,Optional,Union
  4. from fastapi.responses import FileResponse
  5. from random import randint
  6. from fastapi.security import OAuth2PasswordRequestForm
  7. from app.models.models import Registration,User,User_information,Class_list
  8. from app.api import deps
  9. from sqlalchemy.orm import Session
  10. from typing import Any, Dict
  11. import secrets
  12. from fastapi_login.exceptions import InvalidCredentialsException
  13. from fastapi_login import LoginManager
  14. from datetime import timedelta,datetime,date
  15. from jose import jwt
  16. from emails.template import JinjaTemplate
  17. from tortoise.queryset import Q
  18. from fastapi.responses import HTMLResponse
  19. from app.api.users import manager
  20. registration = APIRouter()
  21. async def check_token(access_token: str):
  22. result = await User.filter(token=access_token).first()
  23. if not result:
  24. print("no access")
  25. return None
  26. user_id = result.id
  27. return user_id
  28. async def check_if_id_exeit(
  29. table_name : str,
  30. id : int
  31. ):
  32. exec('check_list = await {}.filter(id=id).all()'.format(table_name))
  33. if check_list == []:
  34. return True
  35. else:
  36. False
  37. async def check_permissions(user_id):
  38. user = await User.get(id=user_id)
  39. if user.is_superuser:
  40. return True
  41. else:
  42. return False
  43. # @registration.get("/protected")
  44. # def protected_route(user_id=Depends(check_token)):
  45. # if not user_id:
  46. # return {"message": "no access"}
  47. # return {'user': user_id}
  48. @registration.get("/get_registration")
  49. async def get_registration(
  50. user_id = Depends(check_token),
  51. get_all : Optional[int] = None,
  52. event_id : Optional[int] = None,
  53. registration_id : Optional[int] = None,
  54. is_check : Optional[int] = None
  55. ):
  56. try :
  57. if not user_id :
  58. return {"msg": "please log in", "code": 200}
  59. if get_all:
  60. inform_list = Registration.all()
  61. else:
  62. inform_list = Registration.filter(user_id=user_id).all()
  63. if event_id:
  64. inform_list = inform_list.filter(event_id=event_id)
  65. if is_check != None:
  66. inform_list = inform_list.filter(reg_confirm=is_check)
  67. if registration_id:
  68. reg_list_tmp = await inform_list.filter(id=registration_id,is_del=0).all()
  69. else:
  70. reg_list_tmp = await inform_list.filter(is_del=0).all()
  71. reg_list = []
  72. for infor in reg_list_tmp:
  73. try :
  74. reg_data = {
  75. "Registration_id" : infor.id,
  76. "event_id" : infor.event_id,
  77. "user_id" : infor.user_id,
  78. "reg_confirm" : infor.reg_confirm,
  79. "create_time" : infor.create_time
  80. }
  81. except:
  82. reg_data = {
  83. "msg" : "fail to get data"
  84. }
  85. reg_list.append(reg_data)
  86. return {"msg": "success", "code": 200,"registrations":reg_list}
  87. except Exception as e:
  88. return {"msg": str(e), "code": 500}
  89. @registration.post("/input_information")
  90. async def input_information(
  91. user_id = Depends(check_token),
  92. name : str = Form(default=''),
  93. display_name : str = Form(default=''),
  94. birthday : date = Form(default=datetime.now().date()),
  95. gender : str = Form(default=''),
  96. phone : str = Form(default=''),
  97. address : str = Form(default='')
  98. ):
  99. try :
  100. if not user_id :
  101. return {"msg": "no access", "code": 200}
  102. inform = await User_information.get_or_create(
  103. user_id=user_id,
  104. defaults={
  105. 'name': name,
  106. 'birthday' :birthday,
  107. 'gender': gender,
  108. 'phone': phone,
  109. 'address': address
  110. }
  111. )
  112. if display_name != '':
  113. user = await User.get(id = user_id)
  114. user.username = display_name
  115. await user.save()
  116. return {"msg": "success", "code": 200, "user_inform_id": inform[0].id,"is_exist":not inform[1]}
  117. except Exception as e:
  118. return {"msg": str(e), "code": 500}
  119. @registration.post("/update_information")
  120. async def update_information(
  121. user_id = Depends(check_token),
  122. name : str = Form(default=''),
  123. display_name : str = Form(default=''),
  124. birthday : date = Form(default=datetime.now().date()),
  125. gender : str = Form(default=''),
  126. phone : str = Form(default=''),
  127. address : str = Form(default='')
  128. ):
  129. try :
  130. if not user_id :
  131. return {"msg": "no access", "code": 200}
  132. infor = await User_information.get(user_id = user_id)
  133. user = await User.get(id = user_id)
  134. if name != '':
  135. infor.name = name
  136. if birthday != datetime.now().date():
  137. infor.birthday = birthday
  138. if gender != '':
  139. infor.gender = gender
  140. if phone != '':
  141. infor.phone = phone
  142. if address != '':
  143. infor.address = address
  144. if display_name != '':
  145. user.username = display_name
  146. await infor.save()
  147. await user.save()
  148. return {"msg": "success", "code": 200, "user_inform_id": infor.id}
  149. except Exception as e:
  150. return {"msg": str(e), "code": 500}
  151. @registration.get("/get_user_information")
  152. async def get_user_information(
  153. user_id = Depends(check_token)
  154. ):
  155. try:
  156. if not user_id :
  157. return {"msg": "no access", "code": 200}
  158. try:
  159. infor = await User_information.get(user_id=user_id)
  160. except:
  161. return {"msg": "no user information", "code": 200}
  162. user_obj = await User.get(id = user_id)
  163. user_inform = {
  164. "user_id" : infor.user_id,
  165. "display_name" : user_obj.username,
  166. "name" : infor.name,
  167. "birthday" : infor.birthday,
  168. "gender" : infor.gender,
  169. "phone" : infor.phone,
  170. "address" : infor.address,
  171. "email" : user_obj.email
  172. }
  173. return {"msg":"success","code":200,"user_inform": user_inform}
  174. except Exception as e:
  175. return {"msg": str(e), "code": 500}
  176. @registration.post("/input_registration")
  177. async def input_registration(
  178. user_id = Depends(check_token),
  179. event_id : int = Form(default=0)
  180. ):
  181. try :
  182. if not user_id :
  183. return {"msg": "please log in", "code": 200}
  184. # if check_if_id_exeit(User_information,user_inform_id):
  185. # return {"msg": "no user information", "code": 200}
  186. # if check_if_id_exeit(Class_list,event_id):
  187. # return {"msg": "no class list", "code": 200}
  188. try:
  189. await User_information.get(user_id=user_id)
  190. except:
  191. return {"msg": "no user information", "code": 200}
  192. new_registration = await Registration.create(
  193. event_id = event_id,
  194. user_id = user_id,
  195. reg_confirm = 0,
  196. is_del = 0 ,
  197. create_time = datetime.now()
  198. )
  199. return {"msg": "success", "code": 200,"new_registration_id":new_registration.id}
  200. except Exception as e:
  201. return {"msg": str(e), "code": 500}
  202. @registration.post("/confirm_reg")
  203. async def confirm_reg(
  204. #user_id = Depends(check_token),
  205. registration_id : int
  206. ):
  207. try:
  208. registration_obj = await Registration.get(id=registration_id)
  209. registration_obj.reg_confirm = 1
  210. await registration_obj.save()
  211. return {"msg": "success", "code": 200,"registration_id":registration_obj.id}
  212. except Exception as e:
  213. return {"msg": str(e), "code": 500}
  214. @registration.post("/recover_registration")
  215. async def delete_registration(
  216. user_id = Depends(check_token),
  217. event_id : int = 0
  218. ):
  219. try:
  220. if not user_id :
  221. return {"msg": "please log in", "code": 200}
  222. registration_obj = await Registration.get(event_id=event_id,user_id=user_id)
  223. registration_obj.is_del = 0
  224. await registration_obj.save()
  225. return {"msg": "success", "code": 200}
  226. except Exception as e:
  227. return {"msg": str(e), "code": 500}
  228. @registration.post("/delete_registration")
  229. async def delete_registration(
  230. user_id = Depends(check_token),
  231. event_id : int = 0
  232. ):
  233. try:
  234. if not user_id :
  235. return {"msg": "please log in", "code": 200}
  236. registration_obj = await Registration.get(event_id=event_id,user_id=user_id)
  237. registration_obj.is_del = 1
  238. await registration_obj.save()
  239. return {"msg": "success", "code": 200}
  240. except Exception as e:
  241. return {"msg": str(e), "code": 500}