registration.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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,User_resume
  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. IMAGEDIR = "/var/www/ntcri/assets/resume_pic/"
  23. IMAGEDIR_short = "assets/resume_pic/"
  24. @registration.post("/upload_user_resume_imgs")
  25. async def upload_user_resume_imgs(files:Optional[List[UploadFile]] = File(None)):
  26. files_url = []
  27. if files :
  28. for file in files:
  29. contents = await file.read()
  30. #save the file
  31. with open(f"{IMAGEDIR}{file.filename}", "wb") as f:
  32. f.write(contents)
  33. files_url.append(f"{IMAGEDIR_short}{file.filename}" )
  34. return files_url
  35. async def check_token(access_token: str):
  36. result = await User.filter(token=access_token).first()
  37. if not result:
  38. print("no access")
  39. return None
  40. user_id = result.id
  41. return user_id
  42. async def check_permissions(user_id):
  43. user = await User.get(id=user_id)
  44. if user.is_superuser:
  45. return True
  46. else:
  47. return False
  48. # @registration.get("/protected")
  49. # def protected_route(user_id=Depends(check_token)):
  50. # if not user_id:
  51. # return {"message": "no access"}
  52. # return {'user': user_id}
  53. @registration.get("/get_registration_class")
  54. async def get_registration_class(
  55. event_id : Optional[int] = None,
  56. check_user_id = Depends(check_token)
  57. ):
  58. #inform_list = await Registration.filter(event_id=event_id,is_del=0,reg_confirm=1).all() #use for 8/25 after
  59. check_list1 = await User.get(id = check_user_id).all()
  60. check_list2 = await Class_list.get(id = event_id).all()
  61. if 2!=check_list1.is_superuser:
  62. if check_list2.create_user_id !=check_user_id:
  63. return {"msg": "permissions denied", "code": 200,"registrations":[]}
  64. inform_list = await Registration.filter(event_id=event_id,is_del=0).all()
  65. class_data =[]
  66. for infor in inform_list:
  67. reg_data = {
  68. "event_id" : infor.event_id,
  69. "user_id" : infor.user_id,
  70. "is_attend" : infor.is_attend,
  71. "payment_status":infor.payment_status,
  72. "five_digits":infor.five_digits,
  73. "reg_confirm":infor.reg_confirm,
  74. }
  75. user = await User.get(id=infor.user_id)
  76. inform = await User_information.get(user_id=infor.user_id)
  77. reg_data["real_name"] = inform.name
  78. reg_data["phone"] = inform.phone
  79. reg_data["email"] = user.email
  80. class_data.append(reg_data )
  81. return {"msg": "success", "code": 200,"registrations":class_data}
  82. @registration.post("/update_registration_class")
  83. async def update_registration_class(
  84. event_id : Optional[int] ,
  85. user_id : Optional[int] ,
  86. is_attend : int = Form(default=None),
  87. payment_status : int = Form(default=None),
  88. reg_confirm : int = Form(default=None),
  89. five_digits : str = Form(default=None),
  90. check_user_id = Depends(check_token),
  91. ):
  92. #inform_list = await Registration.filter(event_id=event_id,is_del=0,reg_confirm=1,user_id=user_id).all() #use for 8/25 after
  93. inform_list = await Registration.filter(event_id=event_id,is_del=0,user_id=user_id).all()
  94. check_list1 = await User.get(id = check_user_id).all()
  95. check_list2 = await Class_list.get(id = event_id).all()
  96. if 2!=check_list1.is_superuser:
  97. if check_list2.create_user_id !=check_user_id:
  98. return {"msg": "permissions denied", "code": 500}
  99. class_data =[]
  100. for infor in inform_list:
  101. if is_attend is not None:
  102. infor.is_attend = is_attend
  103. if payment_status is not None:
  104. infor.payment_status = payment_status
  105. if reg_confirm is not None:
  106. infor.reg_confirm = reg_confirm
  107. if five_digits is not None:
  108. if len(five_digits)==5:
  109. infor.five_digits = five_digits
  110. else:
  111. return {"msg": "five_digits must be five_digits", "code": 500}
  112. await infor.save()
  113. return {"msg": "success", "code": 200, "user_id": infor.user_id,"event_id": infor.event_id}
  114. @registration.get("/get_registration")
  115. async def get_registration(
  116. user_id = Depends(check_token),
  117. get_all : Optional[int] = None,
  118. event_id : Optional[int] = None,
  119. registration_id : Optional[int] = None,
  120. is_check : Optional[int] = None
  121. ):
  122. try :
  123. if not user_id :
  124. return {"msg": "please log in", "code": 200}
  125. if get_all:
  126. inform_list = Registration.all()
  127. else:
  128. inform_list = Registration.filter(user_id=user_id).all()
  129. if event_id:
  130. inform_list = inform_list.filter(event_id=event_id)
  131. if is_check != None:
  132. inform_list = inform_list.filter(reg_confirm=is_check)
  133. if registration_id:
  134. reg_list_tmp = await inform_list.filter(id=registration_id,is_del=0).all().order_by("-event_id","create_time")
  135. else:
  136. reg_list_tmp = await inform_list.filter(is_del=0).all().order_by("-event_id","create_time")
  137. reg_list = []
  138. for infor in reg_list_tmp:
  139. try :
  140. reg_data = {
  141. "Registration_id" : infor.id,
  142. "event_id" : infor.event_id,
  143. "user_id" : infor.user_id,
  144. "reg_confirm" : infor.reg_confirm,
  145. "create_time" : infor.create_time
  146. }
  147. except:
  148. reg_data = {
  149. "msg" : "fail to get data"
  150. }
  151. try :
  152. class_obj = await Class_list.get(id = infor.event_id)
  153. class_name_obj = await Class_name.get(id = class_obj.name_id)
  154. school_obj = await Schools.get(id = class_name_obj.school_id)
  155. reg_data["school_name"] = school_obj.name
  156. reg_data["class_name"] = class_name_obj.name
  157. reg_data["class_event"] = class_obj.event
  158. reg_data["start_time"] = str(class_obj.start_time)
  159. reg_data["end_time"] =str(class_obj.end_time)
  160. except Exception as e:
  161. reg_data["class_data"] = str(e)
  162. try:
  163. user = await User.get(id=infor.user_id)
  164. inform = await User_information.get(user_id=infor.user_id)
  165. reg_data["real_name"] = inform.name
  166. reg_data["phone"] = inform.phone
  167. reg_data["email"] = user.email
  168. except Exception as e:
  169. reg_data["user_data"] = str(e)
  170. reg_list.append(reg_data)
  171. return {"msg": "success", "code": 200,"registrations":reg_list}
  172. except Exception as e:
  173. return {"msg": str(e), "code": 500}
  174. @registration.post("/input_information")
  175. async def input_information(
  176. name : str = Form(default=''),
  177. user_name : str = Form(default=''),
  178. birthday : date = Form(default=datetime.now().date()),
  179. gender : str = Form(default=''),
  180. phone : str = Form(default=''),
  181. address : str = Form(default=''),
  182. user_id = Depends(check_token),
  183. position: str = Form(default='[1]'),
  184. ):
  185. try :
  186. if not user_id :
  187. return {"msg": "no access", "code": 200}
  188. position_list = json.loads(position)
  189. d = {"學員":0,"開課工藝家":0,"其他":0}
  190. if 1 in position_list:
  191. d["學員"] = 1
  192. if 2 in position_list:
  193. d["開課工藝家"] = 1
  194. if 3 in position_list:
  195. d["其他"] = 1
  196. print(d)
  197. infor,created = await User_information.get_or_create(
  198. user_id=user_id,
  199. defaults={
  200. 'name': name,
  201. 'birthday' :birthday,
  202. 'gender': gender,
  203. 'phone': phone,
  204. 'address': address,
  205. 'position': d
  206. }
  207. )
  208. if not created:
  209. if name != '':
  210. infor.name = name
  211. if birthday != datetime.now().date():
  212. infor.birthday = birthday
  213. if gender != '':
  214. infor.gender = gender
  215. if phone != '':
  216. infor.phone = phone
  217. if address != '':
  218. infor.address = address
  219. if user_name != '':
  220. global user
  221. user = await User.get(id = user_id)
  222. user.username = user_name
  223. if position != '':
  224. infor.position = d
  225. await infor.save()
  226. await user.save()
  227. msg = "Update success"
  228. else :
  229. msg = "input success"
  230. return {"msg": "success", "code": 200, "user_inform_id": infor.id,"is_exist":not created}
  231. except Exception as e:
  232. return {"msg": str(e), "code": 500}
  233. @registration.post("/update_information")
  234. async def update_information(
  235. user_name : str = Form(default=''),
  236. name : str = Form(default=''),
  237. birthday : date = Form(default=datetime.now().date()),
  238. gender : str = Form(default=''),
  239. phone : str = Form(default=''),
  240. address : str = Form(default=''),
  241. user_id = Depends(check_token),
  242. position : str = Form(default='[1]'),
  243. ):
  244. try :
  245. if not user_id :
  246. return {"msg": "no access", "code": 200}
  247. infor = await User_information.get(user_id = user_id)
  248. user = await User.get(id = user_id)
  249. position_list = json.loads(position)
  250. d = {"學員":0,"開課工藝家":0,"其他":0}
  251. if 1 in position_list:
  252. d["學員"] = 1
  253. if 2 in position_list:
  254. d["開課工藝家"] = 1
  255. if 3 in position_list:
  256. d["其他"] = 1
  257. if name != '':
  258. infor.name = name
  259. if birthday != datetime.now().date():
  260. infor.birthday = birthday
  261. if gender != '':
  262. infor.gender = gender
  263. if phone != '':
  264. infor.phone = phone
  265. if address != '':
  266. infor.address = address
  267. if user_name != '':
  268. user.username = user_name
  269. if position != '':
  270. infor.position = d
  271. await infor.save()
  272. await user.save()
  273. return {"msg": "success", "code": 200, "user_inform_id": infor.id}
  274. except Exception as e:
  275. return {"msg": str(e), "code": 500}
  276. @registration.get("/get_user_information")
  277. async def get_user_information(
  278. user_id = Depends(check_token),
  279. get_all : int = 0,
  280. get_detail_information : int = 1
  281. ):
  282. try:
  283. if not user_id :
  284. return {"msg": "no access", "code": 200}
  285. try:
  286. if get_all == 1:
  287. user_list = await User.all()
  288. else:
  289. user_list = await User.filter(id = user_id).all()
  290. except:
  291. return {"msg": "user table run fail", "code": 500}
  292. user_inform_list = []
  293. for user_obj in user_list:
  294. user_inform = {
  295. "user_id" : user_obj.id,
  296. "user_name" : user_obj.username,
  297. "email" : user_obj.email,
  298. "is_superuser" :user_obj.is_superuser
  299. }
  300. if get_detail_information:
  301. try :
  302. inform = await User_information.get(user_id=user_obj.id)
  303. user_inform["name"] = inform.name
  304. user_inform["birthday"] = inform.birthday
  305. user_inform["gender"] = inform.gender
  306. user_inform["phone"] = inform.phone
  307. user_inform["address"] = inform.address
  308. user_inform["msg"] = "user information exist"
  309. user_inform["exist"] = True
  310. user_inform["position"] = inform.position
  311. except:
  312. user_inform["msg"] = "no user information"
  313. user_inform["exist"] = False
  314. user_inform_list.append(user_inform)
  315. print(user_inform_list)
  316. return {"msg":"success","code":200,"user_inform": user_inform_list}
  317. except Exception as e:
  318. return {"msg": str(e), "code": 500}
  319. @registration.post("/update_superuser")
  320. async def update_superuser(
  321. change_id : int ,
  322. is_superuser : int ,
  323. user_id = Depends(check_token),
  324. ):
  325. if not user_id :
  326. return {"msg": "no exit", "code": 200}
  327. user = await User.get(id=user_id)
  328. if user.is_superuser != 2:
  329. return {"msg": "no access", "code": 200}
  330. user_list = await User.filter(id = change_id).all()
  331. for infor in user_list:
  332. if is_superuser is not None:
  333. infor.is_superuser = is_superuser
  334. await infor.save()
  335. return {"msg": "success", "code": 200}
  336. @registration.get("/change_class_reg_number")
  337. async def change_class_reg_number(
  338. event_id: int = 0,
  339. reduce_number : int = 1
  340. ):
  341. try:
  342. if event_id:
  343. try:
  344. await Class_list.get(id = event_id)
  345. except Exception as e:
  346. return {"msg": "no this event", "code": 200}
  347. try:
  348. class_date = await Class_date.get(class_list_id = event_id)
  349. except Exception as e:
  350. return {"msg": "no this class' number limit", "code": 200}
  351. #if class_date.amount_left ==0 and reduce_number>0:
  352. if class_date.amount_left ==0:
  353. return {"msg": "class is full", "code": 200,"amount_left":-1}
  354. elif class_date.amount_left == class_date.number_limit and reduce_number<0:
  355. return {"msg": "class is empty", "code": 200,"amount_left":class_date.amount_left }
  356. else:
  357. class_date.amount_left = class_date.amount_left-reduce_number
  358. await class_date.save()
  359. return {"msg": "success", "code": 200,"amount_left":class_date.amount_left}
  360. except Exception as e:
  361. return {"msg": str(e), "code": 500}
  362. @registration.post("/input_registration")
  363. async def input_registration(
  364. event_id : int = Form(default=0),
  365. user_id = Depends(check_token)
  366. ):
  367. try :
  368. if not user_id :
  369. return {"msg": "please log in", "code": 500}
  370. try:
  371. await Class_list.get(id = event_id)
  372. except Exception as e:
  373. return {"msg": "no this event", "code": 500}
  374. # if check_if_id_exeit(User_information,user_inform_id):
  375. # return {"msg": "no user information", "code": 200}
  376. # if check_if_id_exeit(Class_list,event_id):
  377. # return {"msg": "no class list", "code": 200}
  378. try:
  379. await User_information.get(user_id=user_id)
  380. except:
  381. return {"msg": "no user information", "code": 500}
  382. try:
  383. existing_registration = await Registration.get(
  384. event_id=event_id,
  385. user_id=user_id
  386. )
  387. new_registration = existing_registration
  388. is_register = 0
  389. except:
  390. existing_registration = None
  391. if existing_registration is None:
  392. is_register = 1
  393. if is_register:
  394. amount_left_obj = await change_class_reg_number(event_id=event_id)
  395. msg = amount_left_obj["msg"]
  396. else:
  397. msg = "already registrate"
  398. if msg == 'success':
  399. new_registration = await Registration.create(
  400. event_id=event_id,
  401. user_id=user_id,
  402. reg_confirm=0,
  403. is_del=0,
  404. create_time=datetime.now(),
  405. is_attend = 0,
  406. payment_status = 0
  407. )
  408. return {"msg": msg, "code": 200,"new_registration_id":new_registration.id,"is_already_exist":not is_register}
  409. else:
  410. return {"msg": msg, "code": 200,"new_registration_id":None,"is_already_exist":not is_register}
  411. #new_registration = await Registration.get_or_create(
  412. # event_id = event_id,
  413. # user_id = user_id,
  414. # defaults = {
  415. # "reg_confirm" : 0,
  416. # "is_del" : 0 ,
  417. # "create_time" : datetime.now(),
  418. #
  419. # }
  420. #)
  421. #print(new_registration)
  422. #if new_registration[1]:
  423. #return {"msg": msg, "code": 200,"new_registration_id":new_registration[0].id,"is_already_exist":not new_registration[1]}
  424. #return {"msg": msg, "code": 200,"new_registration_id":new_registration.id,"is_already_exist":not is_register}
  425. except Exception as e:
  426. return {"msg": str(e), "code": 500}
  427. @registration.post("/confirm_reg")
  428. async def confirm_reg(
  429. #user_id = Depends(check_token),
  430. registration_id : int
  431. ):
  432. try:
  433. registration_obj = await Registration.get(id=registration_id)
  434. registration_obj.reg_confirm = 1
  435. await registration_obj.save()
  436. return {"msg": "success", "code": 200,"registration_id":registration_obj.id}
  437. except Exception as e:
  438. return {"msg": str(e), "code": 500}
  439. @registration.post("/recover_registration")
  440. async def delete_registration(
  441. user_id = Depends(check_token),
  442. event_id : int = 0
  443. ):
  444. try:
  445. if not user_id :
  446. return {"msg": "please log in", "code": 200}
  447. registration_obj = await Registration.get(event_id=event_id,user_id=user_id)
  448. amount_left_obj = await change_class_reg_number(event_id=registration_obj.event_id)
  449. msg = amount_left_obj["msg"]
  450. if msg == "class is full":
  451. return {"msg": msg+" cannot recover registration", "code": 200}
  452. registration_obj.is_del = 0
  453. await registration_obj.save()
  454. return {"msg": msg, "code": 200}
  455. except Exception as e:
  456. return {"msg": str(e), "code": 500}
  457. @registration.post("/delete_registration")
  458. async def delete_registration(
  459. user_id = Depends(check_token),
  460. super_ad_input_user_id : int = 0,
  461. event_id : int = 0
  462. ):
  463. try:
  464. if not user_id :
  465. return {"msg": "please log in", "code": 200}
  466. if super_ad_input_user_id:
  467. registration_obj = await Registration.get(event_id=event_id,user_id=super_ad_input_user_id)
  468. else:
  469. registration_obj = await Registration.get(event_id=event_id,user_id=user_id)
  470. registration_obj.is_del = 1
  471. amount_left_obj = await change_class_reg_number(event_id=registration_obj.event_id,reduce_number=-1)
  472. msg = amount_left_obj["msg"]
  473. await registration_obj.save()
  474. return {"msg": msg , "code": 200}
  475. except Exception as e:
  476. return {"msg": str(e), "code": 500}
  477. @registration.post("/input_user_resume")
  478. async def input_user_resume(
  479. user_id = Depends(check_token),
  480. imgs = Depends(upload_user_resume_imgs),
  481. teacher_name : str = Form(default=''),
  482. work_type : str = Form(default=''),
  483. experience : str = Form(default=''),
  484. expertise : str = Form(default=''),
  485. license : str = Form(default=''),
  486. media : str = Form(default=''),
  487. introduction: str = Form(default='')
  488. ):
  489. try:
  490. if not user_id :
  491. return {"msg": "please log in", "code": 200}
  492. msg = ''
  493. user_resume, created = await User_resume.get_or_create(
  494. user_id = user_id,
  495. defaults = {
  496. "teacher_name": teacher_name,
  497. "work_type": work_type,
  498. "experience": experience,
  499. "expertise": expertise,
  500. "license": license,
  501. "media": media,
  502. "imgs": imgs,
  503. "introduction": introduction
  504. }
  505. )
  506. if not created:
  507. if teacher_name.strip() != '' :
  508. user_resume.teacher_name = teacher_name
  509. if work_type.strip() != '' :
  510. user_resume.work_type= work_type
  511. if experience.strip() != '' :
  512. user_resume.experience= experience
  513. if license.strip() != '' :
  514. user_resume.license= license
  515. if media.strip() != '' :
  516. user_resume.media= media
  517. if imgs != '[]' :
  518. user_resume.imgs= imgs
  519. if introduction.strip() != '' :
  520. user_resume.introduction= introduction
  521. await user_resume.save()
  522. msg = "Update success"
  523. else :
  524. msg = "input success"
  525. return {"msg": msg , "code": 200}
  526. except Exception as e:
  527. return {"msg": str(e), "code": 500}
  528. @registration.get("/get_user_resume")
  529. async def get_user_resume(
  530. user_id = Depends(check_token)
  531. ):
  532. try:
  533. if not user_id :
  534. return {"msg": "please log in", "code": 200}
  535. user_resume = await User_resume.get(user_id = user_id)
  536. data = user_resume.show_data()
  537. return {"msg": "success" , "code": 200,"user_resume":data}
  538. except Exception as e:
  539. return {"msg": str(e), "code": 500}
  540. @registration.post("/delete_user")
  541. async def get_user_resume(
  542. user_id : Optional[int] = None,
  543. check_user_id = Depends(check_token)
  544. ):
  545. try:
  546. #inform_list = await Registration.filter(event_id=event_id,is_del=0,reg_confirm=1).all() #use for 8/25 after
  547. check_list1 = await User.get(id = check_user_id).all()
  548. if 2!=check_list1.is_superuser:
  549. if check_list2.create_user_id !=check_user_id:
  550. return {"msg": "permissions denied", "code": 200,"registrations":[]}
  551. await User.filter(user_id = user_id).delete()
  552. return {"msg": "success" , "code": 200}
  553. except Exception as e:
  554. return {"msg": str(e), "code": 500}