registration.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. from email import message
  2. import json
  3. # from urllib.request import Request
  4. from fastapi_login import LoginManager
  5. from fastapi import APIRouter, Form, Depends, HTTPException, File, UploadFile,Request
  6. from typing import List,Optional,Union
  7. from fastapi.responses import FileResponse
  8. from random import randint
  9. from fastapi.security import OAuth2PasswordRequestForm
  10. from app.models.models import Registration,User,User_information,Class_list,Class_name,Schools,Class_date,User_resume,Class_detail
  11. from app.api import deps
  12. from sqlalchemy.orm import Session
  13. from typing import Any, Dict
  14. import secrets
  15. from fastapi_login.exceptions import InvalidCredentialsException
  16. from fastapi_login import LoginManager
  17. from datetime import timedelta,datetime,date
  18. from jose import jwt
  19. from emails.template import JinjaTemplate
  20. from tortoise.queryset import Q
  21. from fastapi.responses import HTMLResponse
  22. import rpyc
  23. from app.api.users import manager
  24. from app.log import my_log
  25. from app.api.classes import count_point
  26. registration = APIRouter()
  27. IMAGEDIR = "/var/www/ntcri/assets/resume_pic/"
  28. IMAGEDIR_short = "assets/resume_pic/"
  29. def send_email(
  30. email_to: str,
  31. token: str,
  32. subject_template: str = "",
  33. html_template: str = "",
  34. environment: Dict[str, Any] = {},
  35. ):
  36. # message = emails.Message(
  37. # subject=JinjaTemplate(subject_template),
  38. # html=JinjaTemplate(html_template),
  39. # mail_from=(settings.EMAILS_FROM_NAME, settings.EMAILS_FROM_EMAIL),
  40. # )
  41. subject=subject_template
  42. html=html_template
  43. mailobj={}
  44. mailobj['toaddr']=email_to
  45. mailobj['title']=subject
  46. mailobj['totext']=html
  47. conn = rpyc.connect("192.168.192.80", 12345)
  48. conn.root.mailto(mailobj)
  49. return {"message":f"send email"}
  50. @registration.post("/upload_user_resume_imgs")
  51. async def upload_user_resume_imgs(files:Optional[List[UploadFile]] = File(None),old_file:Optional[str]=None):
  52. files_url = []
  53. if old_file:
  54. old_list = eval(old_file)
  55. for file_name in old_list:
  56. files_url.append(file_name)
  57. if files :
  58. for file in files:
  59. contents = await file.read()
  60. #save the file
  61. with open(f"{IMAGEDIR}{file.filename}", "wb") as f:
  62. f.write(contents)
  63. files_url.append(f"{IMAGEDIR_short}{file.filename}" )
  64. return files_url
  65. async def check_token(access_token: str):
  66. result = await User.filter(token=access_token).first()
  67. if not result:
  68. print("no access")
  69. return None
  70. user_id = result.id
  71. return user_id
  72. async def check_permissions(user_id):
  73. user = await User.get(id=user_id)
  74. if user.is_superuser:
  75. return True
  76. else:
  77. return False
  78. # @registration.get("/protected")
  79. # def protected_route(user_id=Depends(check_token)):
  80. # if not user_id:
  81. # return {"message": "no access"}
  82. # return {'user': user_id}
  83. @registration.get("/get_registration_class")
  84. async def get_registration_class(
  85. event_id : Optional[int] = None,
  86. check_user_id = Depends(check_token)
  87. ):
  88. #inform_list = await Registration.filter(event_id=event_id,is_del=0,reg_confirm=1).all() #use for 8/25 after
  89. check_list1 = await User.get(id = check_user_id).all()
  90. check_list2 = await Class_list.get(id = event_id).all()
  91. if 2!=check_list1.is_superuser:
  92. if check_list2.create_user_id !=check_user_id:
  93. return {"msg": "permissions denied", "code": 200,"registrations":[]}
  94. inform_list = await Registration.filter(event_id=event_id,is_del=0).all()
  95. class_data =[]
  96. for infor in inform_list:
  97. reg_data = {
  98. "event_id" : infor.event_id,
  99. "user_id" : infor.user_id,
  100. "is_attend" : infor.is_attend,
  101. "payment_status":infor.payment_status,
  102. "five_digits":infor.five_digits,
  103. "reg_confirm":infor.reg_confirm,
  104. }
  105. user = await User.get(id=infor.user_id)
  106. inform = await User_information.get(user_id=infor.user_id)
  107. reg_data["real_name"] = inform.name
  108. reg_data["phone"] = inform.phone
  109. reg_data["email"] = user.email
  110. class_data.append(reg_data )
  111. return {"msg": "success", "code": 200,"registrations":class_data}
  112. @registration.post("/update_registration_class")
  113. async def update_registration_class(
  114. event_id : Optional[int] ,
  115. user_id : Optional[int] ,
  116. is_attend : int = Form(default=None),
  117. payment_status : int = Form(default=None),
  118. reg_confirm : int = Form(default=None),
  119. five_digits : str = Form(default=None),
  120. check_user_id = Depends(check_token),
  121. ):
  122. #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
  123. inform_list = await Registration.filter(event_id=event_id,is_del=0,user_id=user_id).all()
  124. check_list1 = await User.get(id = check_user_id).all()
  125. check_list2 = await Class_list.get(id = event_id).all()
  126. if 2!=check_list1.is_superuser:
  127. if check_list2.create_user_id !=check_user_id:
  128. return {"msg": "permissions denied", "code": 500}
  129. class_data =[]
  130. for infor in inform_list:
  131. if is_attend is not None:
  132. infor.is_attend = is_attend
  133. if payment_status is not None:
  134. infor.payment_status = payment_status
  135. if reg_confirm is not None:
  136. infor.reg_confirm = reg_confirm
  137. if five_digits is not None :
  138. if len(five_digits)==5:
  139. infor.five_digits = five_digits
  140. if five_digits=='00000':
  141. infor.five_digits = None
  142. else:
  143. return {"msg": "five_digits must be five_digits", "code": 500}
  144. await infor.save()
  145. return {"msg": "success", "code": 200, "user_id": infor.user_id,"event_id": infor.event_id}
  146. @registration.get("/get_registration")
  147. async def get_registration(
  148. user_id = Depends(check_token),
  149. get_all : Optional[int] = None,
  150. event_id : Optional[int] = None,
  151. registration_id : Optional[int] = None,
  152. is_check : Optional[int] = None
  153. ):
  154. try :
  155. if not user_id :
  156. return {"msg": "please log in", "code": 200}
  157. if get_all:
  158. inform_list = Registration.all()
  159. else:
  160. inform_list = Registration.filter(user_id=user_id).all()
  161. if event_id:
  162. inform_list = inform_list.filter(event_id=event_id)
  163. if is_check != None:
  164. inform_list = inform_list.filter(reg_confirm=is_check)
  165. if registration_id:
  166. reg_list_tmp = await inform_list.filter(id=registration_id,is_del=0).all().order_by("-event_id","create_time")
  167. else:
  168. reg_list_tmp = await inform_list.filter(is_del=0).all().order_by("-event_id","create_time")
  169. reg_list = []
  170. for infor in reg_list_tmp:
  171. try :
  172. reg_data = {
  173. "Registration_id" : infor.id,
  174. "event_id" : infor.event_id,
  175. "user_id" : infor.user_id,
  176. "reg_confirm" : infor.reg_confirm,
  177. "create_time" : infor.create_time
  178. }
  179. except:
  180. reg_data = {
  181. "msg" : "fail to get data"
  182. }
  183. try:
  184. class_detail = await Class_detail.filter(class_list_id = infor.event_id)
  185. hours = 0.0
  186. for detail in class_detail:
  187. hours += float(detail.hour)
  188. reg_data["hours"] = hours
  189. except Exception as e:
  190. print(str(e))
  191. try :
  192. class_obj = await Class_list.get(id = infor.event_id)
  193. class_name_obj = await Class_name.get(id = class_obj.name_id)
  194. school_obj = await Schools.get(id = class_name_obj.school_id)
  195. reg_data["school_name"] = school_obj.name
  196. reg_data["class_name"] = class_name_obj.name
  197. reg_data["class_event"] = class_obj.event
  198. reg_data["start_time"] = str(class_obj.start_time)
  199. reg_data["end_time"] =str(class_obj.end_time)
  200. except Exception as e:
  201. await Registration.filter(id =infor.id).delete()
  202. reg_data = {
  203. "event_id" : infor.event_id,
  204. "msg" : "no this class (auto delete)"
  205. }
  206. reg_list.append(reg_data)
  207. continue
  208. try:
  209. user = await User.get(id=infor.user_id)
  210. inform = await User_information.get(user_id=infor.user_id)
  211. reg_data["real_name"] = inform.name
  212. reg_data["phone"] = inform.phone
  213. reg_data["email"] = user.email
  214. except Exception as e:
  215. reg_data["user_data"] = str(e)
  216. reg_list.append(reg_data)
  217. return {"msg": "success", "code": 200,"registrations":reg_list}
  218. except Exception as e:
  219. return {"msg": str(e), "code": 500}
  220. @registration.post("/input_information")
  221. async def input_information(
  222. name : str = Form(default=''),
  223. user_name : str = Form(default=''),
  224. birthday : date = Form(default=datetime.now().date()),
  225. gender : str = Form(default=''),
  226. phone : str = Form(default=''),
  227. address : str = Form(default=''),
  228. user_id = Depends(check_token),
  229. position: str = Form(default='[1]'),
  230. identity:str = Form(default='[]')
  231. ):
  232. try :
  233. if not user_id :
  234. return {"msg": "no access", "code": 200}
  235. position_list = json.loads(position)
  236. d = {"學員":0,"開課工藝家":0,"其他":0}
  237. if 1 in position_list:
  238. d["學員"] = 1
  239. if 2 in position_list:
  240. d["開課工藝家"] = 1
  241. if 3 in position_list:
  242. d["其他"] = 1
  243. print(d)
  244. infor,created = await User_information.get_or_create(
  245. user_id=user_id,
  246. defaults={
  247. 'name': name,
  248. 'birthday' :birthday,
  249. 'gender': gender,
  250. 'phone': phone,
  251. 'address': address,
  252. 'position': d,
  253. "identity": identity
  254. }
  255. )
  256. if not created:
  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 identity != None :
  268. infor.identity = identity
  269. if user_name != '':
  270. global user
  271. user = await User.get(id = user_id)
  272. user.username = user_name
  273. if position != '':
  274. infor.position = d
  275. await infor.save()
  276. await user.save()
  277. msg = "Update success"
  278. else :
  279. msg = "input success"
  280. return {"msg": "success", "code": 200, "user_inform_id": infor.id,"is_exist":not created}
  281. except Exception as e:
  282. return {"msg": str(e), "code": 500}
  283. @registration.post("/update_information")
  284. async def update_information(
  285. user_name : str = Form(default=''),
  286. name : str = Form(default=''),
  287. birthday : date = Form(default=None),
  288. gender : str = Form(default=''),
  289. phone : str = Form(default=''),
  290. address : str = Form(default=''),
  291. user_id = Depends(check_token),
  292. position : str = Form(default='[1]'),
  293. is_superuser : int = Form(default=None),
  294. identity : str = Form(default=None),
  295. change_user_id : int = Form(default=None)
  296. ):
  297. try :
  298. if not user_id :
  299. return {"msg": "no access", "code": 200}
  300. if change_user_id is None:
  301. change_user_id = user_id
  302. infor = await User_information.get(user_id = change_user_id)
  303. user = await User.get(id = change_user_id)
  304. operator = await User.get(id = user_id)
  305. position_list = json.loads(position)
  306. d = {"學員":0,"開課工藝家":0,"其他":0}
  307. if 1 in position_list:
  308. d["學員"] = 1
  309. if 2 in position_list:
  310. d["開課工藝家"] = 1
  311. if 3 in position_list:
  312. d["其他"] = 1
  313. if is_superuser != None :
  314. if is_superuser == 2:
  315. if operator.is_superuser != 2:
  316. return {"msg": "no access", "code": 500}
  317. user.is_superuser = is_superuser
  318. await user.save()
  319. if name != '':
  320. infor.name = name
  321. if birthday != None:
  322. infor.birthday = birthday
  323. if gender != '':
  324. infor.gender = gender
  325. if phone != '':
  326. infor.phone = phone
  327. if address != '':
  328. infor.address = address
  329. if user_name != '':
  330. user.username = user_name
  331. if position != '':
  332. infor.position = d
  333. if identity != None :
  334. infor.identity = identity
  335. await infor.save()
  336. await user.save()
  337. return {"msg": "success", "code": 200, "user_inform_id": infor.id}
  338. except Exception as e:
  339. return {"msg": str(e), "code": 500}
  340. @registration.get("/get_user_information")
  341. async def get_user_information(
  342. user_id = Depends(check_token),
  343. get_all : int = 0,
  344. get_detail_information : int = 1
  345. ):
  346. try:
  347. if not user_id :
  348. return {"msg": "no access", "code": 200}
  349. try:
  350. if get_all == 1:
  351. user_list = await User.all()
  352. else:
  353. user_list = await User.filter(id = user_id).all()
  354. except:
  355. return {"msg": "user table run fail", "code": 500}
  356. user_inform_list = []
  357. for user_obj in user_list:
  358. user_inform = {
  359. "user_id" : user_obj.id,
  360. "user_name" : user_obj.username,
  361. "email" : user_obj.email,
  362. "points" : user_obj.points,
  363. "is_superuser" :user_obj.is_superuser
  364. }
  365. if get_detail_information:
  366. try :
  367. inform = await User_information.get(user_id=user_obj.id)
  368. user_inform["name"] = inform.name
  369. user_inform["birthday"] = inform.birthday
  370. user_inform["gender"] = inform.gender
  371. user_inform["phone"] = inform.phone
  372. user_inform["address"] = inform.address
  373. user_inform["msg"] = "user information exist"
  374. user_inform["exist"] = True
  375. user_inform["position"] = inform.position
  376. user_inform["identify"] = inform.identity
  377. except:
  378. user_inform["msg"] = "no user information"
  379. user_inform["exist"] = False
  380. user_inform_list.append(user_inform)
  381. print(user_inform_list)
  382. return {"msg":"success","code":200,"user_inform": user_inform_list}
  383. except Exception as e:
  384. return {"msg": str(e), "code": 500}
  385. # @registration.post("/update_superuser")
  386. # async def update_superuser(
  387. # change_id : int ,
  388. # is_superuser : int ,
  389. # user_id = Depends(check_token),
  390. # ):
  391. # if not user_id :
  392. # return {"msg": "no exit", "code": 200}
  393. # user = await User.get(id=user_id)
  394. # if user.is_superuser != 2:
  395. # return {"msg": "no access", "code": 200}
  396. # user_list = await User.filter(id = change_id).all()
  397. # for infor in user_list:
  398. # if is_superuser is not None:
  399. # infor.is_superuser = is_superuser
  400. # await infor.save()
  401. # return {"msg": "success", "code": 200}
  402. @registration.get("/change_class_reg_number")
  403. async def change_class_reg_number(
  404. event_id: int = 0,
  405. reduce_number : int = 1,
  406. check_in_time : bool = True
  407. ):
  408. try:
  409. if event_id:
  410. try:
  411. await Class_list.get(id = event_id)
  412. except Exception as e:
  413. return {"msg": "no this event", "code": 200}
  414. try:
  415. class_date = await Class_date.get(class_list_id = event_id)
  416. except Exception as e:
  417. return {"msg": "no this class' number limit", "code": 200}
  418. #確認是否在報名時間內
  419. if check_in_time:
  420. today = datetime.now()
  421. if today <= class_date.registration_end.replace(tzinfo=None) and today >= class_date.registration_start.replace(tzinfo=None):
  422. print("尚可報名")
  423. else :
  424. return {"msg": "未在報名時間內", "code": 200}
  425. #if class_date.amount_left ==0 and reduce_number>0:
  426. if class_date.amount_left ==0:
  427. return {"msg": "課程報名已滿", "code": 200,"amount_left":-1}
  428. elif class_date.amount_left == class_date.number_limit and reduce_number<0:
  429. return {"msg": "class is empty", "code": 200,"amount_left":class_date.amount_left }
  430. else:
  431. class_date.amount_left = class_date.amount_left-reduce_number
  432. await class_date.save()
  433. return {"msg": "success", "code": 200,"amount_left":class_date.amount_left}
  434. except Exception as e:
  435. return {"msg": str(e), "code": 500}
  436. @registration.post("/input_registration")
  437. async def input_registration(
  438. request : Request,
  439. event_id : int = Form(default=0),
  440. user_id = Depends(check_token)
  441. ):
  442. try :
  443. if not user_id :
  444. return {"msg": "please log in", "code": 500}
  445. try:
  446. event = await Class_list.get(id = event_id)
  447. class_name = await Class_name.get(id = event.name_id)
  448. except Exception as e:
  449. return {"msg": "no this event", "code": 500}
  450. # if check_if_id_exeit(User_information,user_inform_id):
  451. # return {"msg": "no user information", "code": 200}
  452. # if check_if_id_exeit(Class_list,event_id):
  453. # return {"msg": "no class list", "code": 200}
  454. try:
  455. user = await User_information.get(user_id=user_id)
  456. except:
  457. return {"msg": "no user information", "code": 500}
  458. try:
  459. existing_registration = await Registration.get(
  460. event_id=event_id,
  461. user_id=user_id
  462. )
  463. new_registration = existing_registration
  464. is_register = 0
  465. except:
  466. existing_registration = None
  467. if existing_registration is None:
  468. is_register = 1
  469. if is_register:
  470. amount_left_obj = await change_class_reg_number(event_id=event_id)
  471. msg = amount_left_obj["msg"]
  472. else:
  473. msg = "已經有報名過了"
  474. if msg == 'success':
  475. new_registration = await Registration.create(
  476. event_id=event_id,
  477. user_id=user_id,
  478. reg_confirm=0,
  479. is_del=0,
  480. create_time=datetime.now(),
  481. is_attend = 0,
  482. payment_status = 0
  483. )
  484. if event.create_user_id: # 取得課程創建者資料
  485. try:
  486. creater = await User.get(id=event.create_user_id)
  487. email = creater.email
  488. message = f"報名人姓名:{user.name}<br>報名課程名稱:{class_name.name}<br>場次名稱:{event.event}"
  489. subject = '報名通知'
  490. print(message)
  491. send_email(email,"",subject,message)
  492. except:
  493. print("creater no exixt,no sending email")
  494. try:
  495. user_email = await User.get(id=user_id)
  496. email = user_email.email
  497. message = f"親愛的學員{user.name}您好,<br>\
  498. 恭喜您在工藝學校找到心儀的課程,課程名稱為:{class_name.name}!<br>\
  499. 工藝老師正在確認您的報名資料,將盡快給您後續通知。<br>\
  500. 若於開課前10日尚未收到通知,請將後台畫面、聯絡資訊等截圖寄送至客服信箱:<br>\
  501. craftology@ntcri.gov.tw,以便客服為您查詢。<br>\
  502. 註:此封信件為系統自動發送,請勿回信,謝謝。"
  503. subject = '報名通知'
  504. print(message)
  505. send_email(email,"",subject,message)
  506. except:
  507. client_ip = request.client.host
  508. my_log("error",__name__,f"Client IP: {client_ip} - send email fail")
  509. return {"msg": msg, "code": 200,"new_registration_id":new_registration.id,"is_already_exist":not is_register}
  510. else:
  511. return {"msg": msg, "code": 200,"new_registration_id":None,"is_already_exist":not is_register}
  512. #new_registration = await Registration.get_or_create(
  513. # event_id = event_id,
  514. # user_id = user_id,
  515. # defaults = {
  516. # "reg_confirm" : 0,
  517. # "is_del" : 0 ,
  518. # "create_time" : datetime.now(),
  519. #
  520. # }
  521. #)
  522. #print(new_registration)
  523. #if new_registration[1]:
  524. #return {"msg": msg, "code": 200,"new_registration_id":new_registration[0].id,"is_already_exist":not new_registration[1]}
  525. #return {"msg": msg, "code": 200,"new_registration_id":new_registration.id,"is_already_exist":not is_register}
  526. except Exception as e:
  527. return {"msg": str(e), "code": 500}
  528. @registration.post("/confirm_reg")
  529. async def confirm_reg(
  530. #user_id = Depends(check_token),
  531. # request = Request,
  532. registration_id : int = 0
  533. ):
  534. try:
  535. try:
  536. registration_obj = await Registration.get(id=registration_id)
  537. except:
  538. return {"msg": "can't find registration", "code": 500}
  539. registration_obj.reg_confirm = 1
  540. await registration_obj.save()
  541. student = await User.get(id = registration_obj.user_id)
  542. student_info = await User_information.get(user_id = registration_obj.user_id)
  543. class_event = await Class_list.get(id = registration_obj.event_id)
  544. class_name = await Class_name.get(id = class_event.name_id)
  545. subject = "確認報名"
  546. # message = f"報名人:{student_info.name}<br>課程名稱:{class_name.name}"
  547. message = f'親愛的學員 {student_info.name}您好,<br>\
  548. 感謝您報名課程:{class_name.name},<br>\
  549. 課程費用完成付款後請聯絡您的工藝老師!<br>\
  550. 以下是匯款資訊及聯絡方式:<br>\
  551. (帳戶資訊)<br>\
  552. (聯絡電話或e-mail)<br>\
  553. *若需臨時改期請於開課前10日主動聯繫工藝老師,以便老師準備材料包數量<br>\
  554. 註:此封信件為系統自動發送,請勿回信,謝謝。'
  555. try:
  556. send_email(student.email,"",subject,message)
  557. except:
  558. client_ip = "0.0.0.0"#request.client.host
  559. my_log("info",__name__,f"Client IP: {client_ip} - error to send email")
  560. return {"msg": "success", "code": 200,"registration_id":registration_obj.id}
  561. except Exception as e:
  562. return {"msg": str(e), "code": 500}
  563. @registration.post("/recover_registration")
  564. async def delete_registration(
  565. user_id = Depends(check_token),
  566. event_id : int = 0
  567. ):
  568. try:
  569. if not user_id :
  570. return {"msg": "please log in", "code": 200}
  571. registration_obj = await Registration.get(event_id=event_id,user_id=user_id)
  572. amount_left_obj = await change_class_reg_number(event_id=registration_obj.event_id)
  573. msg = amount_left_obj["msg"]
  574. if msg == "class is full":
  575. return {"msg": msg+" cannot recover registration", "code": 200}
  576. registration_obj.is_del = 0
  577. await registration_obj.save()
  578. return {"msg": msg, "code": 200}
  579. except Exception as e:
  580. return {"msg": str(e), "code": 500}
  581. @registration.post("/delete_registration")
  582. async def delete_registration(
  583. user_id = Depends(check_token),
  584. super_ad_input_user_id : int = 0,
  585. event_id : int = 0
  586. ):
  587. try:
  588. if not user_id :
  589. return {"msg": "please log in", "code": 200}
  590. if super_ad_input_user_id:
  591. registration_obj = await Registration.get(event_id=event_id,user_id=super_ad_input_user_id)
  592. else:
  593. registration_obj = await Registration.get(event_id=event_id,user_id=user_id)
  594. registration_obj.is_del = 1
  595. amount_left_obj = await change_class_reg_number(event_id=registration_obj.event_id,reduce_number=-1)
  596. msg = amount_left_obj["msg"]
  597. await registration_obj.save()
  598. return {"msg": msg , "code": 200}
  599. except Exception as e:
  600. return {"msg": str(e), "code": 500}
  601. @registration.post("/input_user_resume")
  602. async def input_user_resume(
  603. user_id = Depends(check_token),
  604. imgs = Depends(upload_user_resume_imgs),
  605. teacher_name : str = Form(default=''),
  606. work_type : str = Form(default=''),
  607. experience : str = Form(default=''),
  608. expertise : str = Form(default=''),
  609. license : str = Form(default=''),
  610. media : str = Form(default=''),
  611. introduction: str = Form(default='')
  612. ):
  613. try:
  614. if not user_id :
  615. return {"msg": "please log in", "code": 200}
  616. msg = ''
  617. user_resume, created = await User_resume.get_or_create(
  618. user_id = user_id,
  619. defaults = {
  620. "teacher_name": teacher_name,
  621. "work_type": work_type,
  622. "experience": experience,
  623. "expertise": expertise,
  624. "license": license,
  625. "media": media,
  626. "imgs": imgs,
  627. "introduction": introduction
  628. }
  629. )
  630. if not created:
  631. if teacher_name.strip() != '' :
  632. user_resume.teacher_name = teacher_name
  633. if work_type.strip() != '' :
  634. user_resume.work_type= work_type
  635. if experience.strip() != '' :
  636. user_resume.experience= experience
  637. if expertise.strip() != '' :
  638. user_resume.expertise= expertise
  639. if license.strip() != '' :
  640. user_resume.license= license
  641. if media.strip() != '' :
  642. user_resume.media= media
  643. if imgs != '[]' :
  644. user_resume.imgs= imgs
  645. if introduction.strip() != '' :
  646. user_resume.introduction= introduction
  647. await user_resume.save()
  648. msg = "Update success"
  649. else :
  650. msg = "input success"
  651. return {"msg": msg , "code": 200}
  652. except Exception as e:
  653. return {"msg": str(e), "code": 500}
  654. @registration.get("/get_user_resume")
  655. async def get_user_resume(
  656. user_id = Depends(check_token)
  657. ):
  658. try:
  659. if not user_id :
  660. return {"msg": "please log in", "code": 200}
  661. user_resume = await User_resume.get(user_id = user_id)
  662. data = user_resume.show_data()
  663. return {"msg": "success" , "code": 200,"user_resume":data}
  664. except Exception as e:
  665. return {"msg": str(e), "code": 500}
  666. @registration.post("/check_is_user")
  667. async def check_is_user(
  668. email : str = None
  669. ):
  670. try:
  671. try:
  672. await User.get(email=email,is_active=1)
  673. except:
  674. return {"msg": "error" , "code": 200,"result":"此使用者未註冊,請先去註冊"}
  675. return {"msg": "success" , "code": 200,"result":"此使用者已註冊,可以使用"}
  676. except Exception as e:
  677. return {"msg": str(e), "code": 500}
  678. @registration.post("/delete_user")
  679. async def get_user_resume(
  680. user_id : Optional[int] = None,
  681. check_user_id = Depends(check_token)
  682. ):
  683. try:
  684. #inform_list = await Registration.filter(event_id=event_id,is_del=0,reg_confirm=1).all() #use for 8/25 after
  685. check_list1 = await User.get(id = check_user_id).all()
  686. if 2!=check_list1.is_superuser:
  687. if check_list1.create_user_id !=check_user_id:
  688. return {"msg": "permissions denied", "code": 200,"registrations":[]}
  689. await User.filter(user_id = user_id).delete()
  690. return {"msg": "success" , "code": 200}
  691. except Exception as e:
  692. return {"msg": str(e), "code": 500}