registration.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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
  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 = await User_information.all()
  61. else:
  62. inform_list = await User_information.filter(user_id=user_id).all()
  63. reg_list = []
  64. for infor in inform_list:
  65. if registration_id:
  66. reg_list_tmp = await Registration.filter(id=registration_id,user_inform_id=infor.id,is_del=0).all()
  67. else:
  68. reg_list_tmp = await Registration.filter(user_inform_id=infor.id,is_del=0).all()
  69. for reg_obj in reg_list_tmp:
  70. reg_data = {
  71. "Registration_id" : reg_obj.id,
  72. "event_id" : reg_obj.event_id,
  73. "user_inform_id" : reg_obj.user_inform_id,
  74. "reg_confirm" : reg_obj.reg_confirm,
  75. "create_time" : reg_obj.create_time
  76. }
  77. if event_id and reg_data["event_id"] == event_id :
  78. if is_check != None :
  79. if reg_obj.reg_confirm == is_check :
  80. reg_list.append(reg_data)
  81. else :
  82. reg_list.append(reg_data)
  83. elif not event_id:
  84. if is_check != None :
  85. if reg_obj.reg_confirm == is_check :
  86. reg_list.append(reg_data)
  87. else :
  88. reg_list.append(reg_data)
  89. return {"msg": "success", "code": 200,"registrations":reg_list}
  90. except Exception as e:
  91. return {"msg": str(e), "code": 500}
  92. @registration.post("/input_information")
  93. async def input_information(
  94. user_id = Depends(check_token),
  95. name : str = Form(default=''),
  96. gender : str = Form(default=''),
  97. phone : str = Form(default=''),
  98. email : str = Form(default=''),
  99. is_default : int = Form(default=0)
  100. ):
  101. try :
  102. if not user_id :
  103. return {"msg": "no access", "code": 200}
  104. if is_default:
  105. inform = await User_information.get_or_create(
  106. user_id=user_id,
  107. is_default = is_default,
  108. defaults={
  109. 'name': name,
  110. 'gender': gender,
  111. 'phone': phone,
  112. 'email': email
  113. }
  114. )
  115. new_inform = inform[0]
  116. else :
  117. new_inform = await User_information.create(
  118. user_id = user_id,
  119. name = name ,
  120. gender = gender,
  121. phone = phone,
  122. email = email,
  123. is_default = is_default
  124. )
  125. return {"msg": "success", "code": 200, "user_inform_id": new_inform.id}
  126. except Exception as e:
  127. return {"msg": str(e), "code": 500}
  128. @registration.post("/input_registration")
  129. async def input_registration(
  130. user_id = Depends(check_token),
  131. user_inform_id : int= Form(default=0),
  132. event_id : int = Form(default=0)
  133. ):
  134. try :
  135. if not user_id :
  136. return {"msg": "please log in", "code": 200}
  137. # if check_if_id_exeit(User_information,user_inform_id):
  138. # return {"msg": "no user information", "code": 200}
  139. # if check_if_id_exeit(Class_list,event_id):
  140. # return {"msg": "no class list", "code": 200}
  141. new_registration = await Registration.create(
  142. event_id = event_id,
  143. user_inform_id = user_inform_id,
  144. reg_confirm = 0,
  145. is_del = 0 ,
  146. create_time = datetime.now()
  147. )
  148. return {"msg": "success", "code": 200,"new_registration_id":new_registration.id}
  149. except Exception as e:
  150. return {"msg": str(e), "code": 500}
  151. @registration.post("/confirm_reg")
  152. async def confirm_reg(
  153. #user_id = Depends(check_token),
  154. registration_id : int
  155. ):
  156. try:
  157. registration_obj = await Registration.get(id=registration_id)
  158. registration_obj.reg_confirm = 1
  159. await registration_obj.save()
  160. return {"msg": "success", "code": 200,"registration_id":registration_obj.id}
  161. except Exception as e:
  162. return {"msg": str(e), "code": 500}
  163. @registration.post("/delete_registration")
  164. async def delete_registration(
  165. registration_id : int
  166. ):
  167. try:
  168. registration_obj = await Registration.get(id=registration_id)
  169. registration_obj.is_del = 1
  170. await registration_obj.save()
  171. return {"msg": "success", "code": 200}
  172. except Exception as e:
  173. return {"msg": str(e), "code": 500}