registration.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. import json
  2. from fastapi_login import LoginManager
  3. from fastapi import APIRouter, Form, Depends, HTTPException, File, UploadFile
  4. from typing import List,Optional,Union
  5. from fastapi.responses import FileResponse
  6. from random import randint
  7. from fastapi.security import OAuth2PasswordRequestForm
  8. from app.models.models import Registration,User,User_information,Class_list,Class_name,Schools,Class_date
  9. from app.api import deps
  10. from sqlalchemy.orm import Session
  11. from typing import Any, Dict
  12. import secrets
  13. from fastapi_login.exceptions import InvalidCredentialsException
  14. from fastapi_login import LoginManager
  15. from datetime import timedelta,datetime,date
  16. from jose import jwt
  17. from emails.template import JinjaTemplate
  18. from tortoise.queryset import Q
  19. from fastapi.responses import HTMLResponse
  20. from app.api.users import manager
  21. registration = APIRouter()
  22. async def check_token(access_token: str):
  23. result = await User.filter(token=access_token).first()
  24. if not result:
  25. print("no access")
  26. return None
  27. user_id = result.id
  28. return user_id
  29. async def check_permissions(user_id):
  30. user = await User.get(id=user_id)
  31. if user.is_superuser:
  32. return True
  33. else:
  34. return False
  35. # @registration.get("/protected")
  36. # def protected_route(user_id=Depends(check_token)):
  37. # if not user_id:
  38. # return {"message": "no access"}
  39. # return {'user': user_id}
  40. @registration.get("/get_registration")
  41. async def get_registration(
  42. user_id = Depends(check_token),
  43. get_all : Optional[int] = None,
  44. event_id : Optional[int] = None,
  45. registration_id : Optional[int] = None,
  46. is_check : Optional[int] = None
  47. ):
  48. try :
  49. if not user_id :
  50. return {"msg": "please log in", "code": 200}
  51. if get_all:
  52. inform_list = Registration.all()
  53. else:
  54. inform_list = Registration.filter(user_id=user_id).all()
  55. if event_id:
  56. inform_list = inform_list.filter(event_id=event_id)
  57. if is_check != None:
  58. inform_list = inform_list.filter(reg_confirm=is_check)
  59. if registration_id:
  60. reg_list_tmp = await inform_list.filter(id=registration_id,is_del=0).all().order_by("-event_id","create_time")
  61. else:
  62. reg_list_tmp = await inform_list.filter(is_del=0).all().order_by("-event_id","create_time")
  63. reg_list = []
  64. for infor in reg_list_tmp:
  65. try :
  66. reg_data = {
  67. "Registration_id" : infor.id,
  68. "event_id" : infor.event_id,
  69. "user_id" : infor.user_id,
  70. "reg_confirm" : infor.reg_confirm,
  71. "create_time" : infor.create_time
  72. }
  73. except:
  74. reg_data = {
  75. "msg" : "fail to get data"
  76. }
  77. try :
  78. class_obj = await Class_list.get(id = infor.event_id)
  79. class_name_obj = await Class_name.get(id = class_obj.name_id)
  80. school_obj = await Schools.get(id = class_name_obj.school_id)
  81. reg_data["school_name"] = school_obj.name
  82. reg_data["class_name"] = class_name_obj.name
  83. reg_data["class_event"] = class_obj.event
  84. reg_data["start_time"] = str(class_obj.start_time)
  85. reg_data["end_time"] =str(class_obj.end_time)
  86. except Exception as e:
  87. reg_data["class_data"] = str(e)
  88. try:
  89. user = await User.get(id=infor.user_id)
  90. inform = await User_information.get(user_id=infor.user_id)
  91. reg_data["real_name"] = inform.name
  92. reg_data["phone"] = inform.phone
  93. reg_data["email"] = user.email
  94. except Exception as e:
  95. reg_data["user_data"] = str(e)
  96. reg_list.append(reg_data)
  97. return {"msg": "success", "code": 200,"registrations":reg_list}
  98. except Exception as e:
  99. return {"msg": str(e), "code": 500}
  100. @registration.post("/input_information")
  101. async def input_information(
  102. name : str = Form(default=''),
  103. user_name : str = Form(default=''),
  104. birthday : date = Form(default=datetime.now().date()),
  105. gender : str = Form(default=''),
  106. phone : str = Form(default=''),
  107. address : str = Form(default=''),
  108. user_id = Depends(check_token),
  109. position: str = Form(...),
  110. ):
  111. try :
  112. if not user_id :
  113. return {"msg": "no access", "code": 200}
  114. position_list = json.loads(position)
  115. d = {"學員":0,"開課工藝家":0,"其他":0}
  116. if 1 in position_list:
  117. d["學員"] = 1
  118. if 2 in position_list:
  119. d["開課工藝家"] = 1
  120. if 3 in position_list:
  121. d["其他"] = 1
  122. print(d)
  123. inform = await User_information.get_or_create(
  124. user_id=user_id,
  125. defaults={
  126. 'name': name,
  127. 'birthday' :birthday,
  128. 'gender': gender,
  129. 'phone': phone,
  130. 'address': address,
  131. 'position': d,
  132. }
  133. )
  134. if user_name != '':
  135. user = await User.get(id = user_id)
  136. user.username = user_name
  137. await user.save()
  138. return {"msg": "success", "code": 200, "user_inform_id": inform[0].id,"is_exist":not inform[1]}
  139. except Exception as e:
  140. return {"msg": str(e), "code": 500}
  141. @registration.post("/update_information")
  142. async def update_information(
  143. user_name : str = Form(default=''),
  144. name : str = Form(default=''),
  145. birthday : date = Form(default=datetime.now().date()),
  146. gender : str = Form(default=''),
  147. phone : str = Form(default=''),
  148. address : str = Form(default=''),
  149. user_id = Depends(check_token),
  150. position : str = Form(default=''),
  151. ):
  152. try :
  153. if not user_id :
  154. return {"msg": "no access", "code": 200}
  155. infor = await User_information.get(user_id = user_id)
  156. user = await User.get(id = user_id)
  157. position_list = json.loads(position)
  158. d = {"學員":0,"開課工藝家":0,"其他":0}
  159. if 1 in position_list:
  160. d["學員"] = 1
  161. if 2 in position_list:
  162. d["開課工藝家"] = 1
  163. if 3 in position_list:
  164. d["其他"] = 1
  165. if name != '':
  166. infor.name = name
  167. if birthday != datetime.now().date():
  168. infor.birthday = birthday
  169. if gender != '':
  170. infor.gender = gender
  171. if phone != '':
  172. infor.phone = phone
  173. if address != '':
  174. infor.address = address
  175. if user_name != '':
  176. user.username = user_name
  177. if position != '':
  178. infor.position = d
  179. await infor.save()
  180. await user.save()
  181. return {"msg": "success", "code": 200, "user_inform_id": infor.id}
  182. except Exception as e:
  183. return {"msg": str(e), "code": 500}
  184. @registration.get("/get_user_information")
  185. async def get_user_information(
  186. user_id = Depends(check_token),
  187. get_all : int = 0,
  188. get_detail_information : int = 1
  189. ):
  190. try:
  191. if not user_id :
  192. return {"msg": "no access", "code": 200}
  193. try:
  194. if get_all:
  195. user_list = await User.all()
  196. else:
  197. user_list = await User.filter(id = user_id)
  198. except:
  199. return {"msg": "user table run fail", "code": 500}
  200. user_inform_list = []
  201. for user_obj in user_list:
  202. user_inform = {
  203. "user_id" : user_obj.id,
  204. "user_name" : user_obj.username,
  205. "email" : user_obj.email
  206. }
  207. if get_detail_information:
  208. try :
  209. inform = await User_information.get(user_id=user_obj.id)
  210. user_inform["name"] = inform.name
  211. user_inform["birthday"] = inform.birthday
  212. user_inform["gender"] = inform.gender
  213. user_inform["phone"] = inform.phone
  214. user_inform["address"] = inform.address
  215. user_inform["msg"] = "user information exist"
  216. user_inform["exist"] = True
  217. user_inform["position"] = inform.position
  218. except:
  219. user_inform["msg"] = "no user information"
  220. user_inform["exist"] = False
  221. user_inform_list.append(user_inform)
  222. print(user_inform_list)
  223. return {"msg":"success","code":200,"user_inform": user_inform_list}
  224. except Exception as e:
  225. return {"msg": str(e), "code": 500}
  226. @registration.get("/change_class_reg_number")
  227. async def change_class_reg_number(
  228. event_id: int = 0,
  229. reduce_number : int = 1
  230. ):
  231. try:
  232. if event_id:
  233. try:
  234. await Class_list.get(id = event_id)
  235. except Exception as e:
  236. return {"msg": "no this event", "code": 200}
  237. try:
  238. class_date = await Class_date.get(class_list_id = event_id)
  239. except Exception as e:
  240. return {"msg": "no this class' number limit", "code": 200}
  241. if class_date.amount_left ==0 and reduce_number>0:
  242. return {"msg": "class is full", "code": 200,"amount_left":-1}
  243. elif class_date.amount_left == class_date.number_limit and reduce_number<0:
  244. return {"msg": "class is empty", "code": 200,"amount_left":class_date.amount_left }
  245. else:
  246. class_date.amount_left = class_date.amount_left-reduce_number
  247. await class_date.save()
  248. return {"msg": "success", "code": 200,"amount_left":class_date.amount_left}
  249. except Exception as e:
  250. return {"msg": str(e), "code": 500}
  251. @registration.post("/input_registration")
  252. async def input_registration(
  253. event_id : int = Form(default=0),
  254. user_id = Depends(check_token)
  255. ):
  256. try :
  257. if not user_id :
  258. return {"msg": "please log in", "code": 500}
  259. try:
  260. await Class_list.get(id = event_id)
  261. except Exception as e:
  262. return {"msg": "no this event", "code": 500}
  263. # if check_if_id_exeit(User_information,user_inform_id):
  264. # return {"msg": "no user information", "code": 200}
  265. # if check_if_id_exeit(Class_list,event_id):
  266. # return {"msg": "no class list", "code": 200}
  267. try:
  268. await User_information.get(user_id=user_id)
  269. except:
  270. return {"msg": "no user information", "code": 500}
  271. new_registration = await Registration.get_or_create(
  272. event_id = event_id,
  273. user_id = user_id,
  274. defaults = {
  275. "reg_confirm" : 0,
  276. "is_del" : 0 ,
  277. "create_time" : datetime.now()
  278. }
  279. )
  280. if new_registration[1]:
  281. amount_left_obj = await change_class_reg_number(event_id=event_id)
  282. msg = amount_left_obj["msg"]
  283. else:
  284. msg = "already registrate"
  285. return {"msg": msg, "code": 200,"new_registration_id":new_registration[0].id,"is_already_exist":not new_registration[1]}
  286. except Exception as e:
  287. return {"msg": str(e), "code": 500}
  288. @registration.post("/confirm_reg")
  289. async def confirm_reg(
  290. #user_id = Depends(check_token),
  291. registration_id : int
  292. ):
  293. try:
  294. registration_obj = await Registration.get(id=registration_id)
  295. registration_obj.reg_confirm = 1
  296. await registration_obj.save()
  297. return {"msg": "success", "code": 200,"registration_id":registration_obj.id}
  298. except Exception as e:
  299. return {"msg": str(e), "code": 500}
  300. @registration.post("/recover_registration")
  301. async def delete_registration(
  302. user_id = Depends(check_token),
  303. event_id : int = 0
  304. ):
  305. try:
  306. if not user_id :
  307. return {"msg": "please log in", "code": 200}
  308. registration_obj = await Registration.get(event_id=event_id,user_id=user_id)
  309. amount_left_obj = await change_class_reg_number(event_id=registration_obj.event_id)
  310. msg = amount_left_obj["msg"]
  311. if msg == "class is full":
  312. return {"msg": msg+" cannot recover registration", "code": 200}
  313. registration_obj.is_del = 0
  314. await registration_obj.save()
  315. return {"msg": msg, "code": 200}
  316. except Exception as e:
  317. return {"msg": str(e), "code": 500}
  318. @registration.post("/delete_registration")
  319. async def delete_registration(
  320. user_id = Depends(check_token),
  321. super_ad_input_user_id : int = 0,
  322. event_id : int = 0
  323. ):
  324. try:
  325. if not user_id :
  326. return {"msg": "please log in", "code": 200}
  327. if super_ad_input_user_id:
  328. registration_obj = await Registration.get(event_id=event_id,user_id=super_ad_input_user_id)
  329. else:
  330. registration_obj = await Registration.get(event_id=event_id,user_id=user_id)
  331. registration_obj.is_del = 1
  332. amount_left_obj = await change_class_reg_number(event_id=registration_obj.event_id,reduce_number=-1)
  333. msg = amount_left_obj["msg"]
  334. await registration_obj.save()
  335. return {"msg": msg , "code": 200}
  336. except Exception as e:
  337. return {"msg": str(e), "code": 500}