classes.py 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769
  1. from ast import Or
  2. from email.policy import default
  3. from sre_constants import ANY
  4. from fastapi import APIRouter, Form, Depends, HTTPException, File, UploadFile,Request
  5. from typing import List,Optional
  6. from fastapi.responses import FileResponse
  7. from random import randint
  8. from fastapi.security import OAuth2PasswordRequestForm
  9. from app.models.models import User,User_information,Favorite_course,Article_list,Class_date,Attend_record,One_day_class,Outter_class_list,Proposal
  10. from app.models.models import Class_list,Schools,Class_detail,Class_name,Registration,Group_name,Online_course,User_resume,User_point,Point_exchange_record
  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
  18. from app.config import settings
  19. from pathlib import Path
  20. from jose import jwt
  21. from tortoise.queryset import Q
  22. from fastapi.responses import HTMLResponse
  23. import requests
  24. import json
  25. from itertools import chain
  26. from tortoise import fields
  27. import random
  28. from app.log import my_log
  29. import rpyc
  30. import emails
  31. from email.mime.text import MIMEText
  32. from email.mime.image import MIMEImage
  33. from email.mime.multipart import MIMEMultipart
  34. import re
  35. classes = APIRouter()
  36. # logging.basicConfig(
  37. # filename='app-basic.log',
  38. # level=logging.INFO,
  39. # format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
  40. # datefmt='%Y-%m-%d %H:%M:%S'
  41. # )
  42. # logger = logging.getLogger(__name__)
  43. def send_email(
  44. email_to: str,
  45. token: str,
  46. subject_template: str = "",
  47. html_template: str = "",
  48. environment: Dict[str, Any] = {},
  49. ):
  50. # message = emails.Message(
  51. # subject=JinjaTemplate(subject_template),
  52. # html=JinjaTemplate(html_template),
  53. # mail_from=(settings.EMAILS_FROM_NAME, settings.EMAILS_FROM_EMAIL),
  54. # )
  55. try:
  56. subject=subject_template
  57. html=html_template
  58. mailobj={}
  59. mailobj['toaddr']=email_to
  60. mailobj['title']=subject
  61. mailobj['totext']=html
  62. conn = rpyc.connect("192.168.192.80", 12345)
  63. conn.root.mailto(mailobj)
  64. return {"message":f"send email"}
  65. except Exception as e:
  66. return {"msg": str(e), "code": 500}
  67. @classes.post("/test_email")
  68. async def test_email(
  69. email_to: str,
  70. subject_template: str = "",
  71. html_template: str = ""
  72. ):
  73. email = email_to
  74. subject = subject_template
  75. message = html_template
  76. result = send_email(email,"",subject,message)
  77. return result
  78. IMAGEDIR = "/var/www/ntcri/assets/"
  79. IMAGEDIR_short = "assets/"
  80. encode_set = {
  81. "樹藝" : "ARB",
  82. "漆藝" : "LAC",
  83. "藍染" : "AIZ",
  84. "蠟雕" : "WAX",
  85. "竹工藝籃" :"BAM",
  86. "金工/飾品" :"MWT",
  87. "蠟燭/香氛/調香" : "CAN" ,
  88. "編織/羊毛氈/縫紉" : "KNI" ,
  89. "植栽/花藝" : "PLA" ,
  90. "陶藝/玻璃" : "CER" ,
  91. "皮件/皮革" : "LEA" ,
  92. "插畫/繪畫/寫字" : "Ill",
  93. "木工/竹藝" :"WOO",
  94. "安心活動" : "HER"
  95. }
  96. taiwan_city = ["基隆","台北,臺北","新北","宜蘭","花蓮","台東,臺東","桃園","新竹","苗栗","台中,臺中","彰化","南投","雲林","嘉義","台南,臺南","高雄","屏東"]
  97. async def create_upload_files(files:Optional[List[UploadFile]] = File(None)):
  98. files_url = {}
  99. if files :
  100. file_num = 1
  101. print(file_num)
  102. for file in files:
  103. contents = await file.read()
  104. words = file.filename.split('.')
  105. # if words[len(words)-1] != ("png" or "jpg" or "pdf" or "jpeg"):
  106. # return {"msg":"wrong filetype"}
  107. #save the file
  108. with open(f"{IMAGEDIR}{file.filename}", "wb") as f:
  109. f.write(contents)
  110. file_name = "file" + str(file_num)
  111. print(file_name)
  112. files_url[file_name]=f"{IMAGEDIR_short}{file.filename}"
  113. file_num=file_num+1
  114. return files_url
  115. async def check_token(access_token: str):
  116. result = await User.get_or_none(token=access_token)
  117. if not result:
  118. print("no access")
  119. return None
  120. user_id = result.id
  121. return user_id
  122. async def update_location_time(location_id: int):
  123. if location_id:
  124. school = await Schools.get(id=location_id)
  125. school.update_time = datetime.now()
  126. await school.save()
  127. return {"msg": "success", "code": 200}
  128. @classes.post("/change_class_img")
  129. async def change_class_img(
  130. category: str ,
  131. files:Optional[List[UploadFile]] = File(None)
  132. ):
  133. try:
  134. files_url = {}
  135. file_num = 0
  136. for file in files:
  137. contents = await file.read()
  138. #save the file
  139. with open(f"{IMAGEDIR}{file.filename}", "wb") as f:
  140. f.write(contents)
  141. file_name = "file" + str(file_num)
  142. files_url[file_name]=f"{IMAGEDIR_short}{file.filename}"
  143. file_num=file_num+1
  144. class_name_list = await Class_name.filter(Q(category__icontains=category)& Q(is_inner = 0)).all()
  145. for class_name_obj in class_name_list:
  146. x = random.randrange(4)
  147. file_name = "file" + str(x)
  148. class_name_obj.cover_img = files_url[file_name]
  149. await class_name_obj.save()
  150. return {"msg": "success", "code": 200}
  151. except Exception as e:
  152. return {"msg": str(e), "code": 500}
  153. class resume():
  154. imgs = UploadFile
  155. teacher_name : str
  156. work_type : str
  157. experience : str
  158. expertise : str
  159. license : str
  160. media : str
  161. introduction: str
  162. # @classes.post("/check_city")
  163. # async def check_city(
  164. # Lng: str = None,
  165. # Lat: str = None,
  166. # ):
  167. # try:
  168. # country : Any
  169. # city : Any
  170. # if Lng and Lat:
  171. # url = 'https://maps.googleapis.com/maps/api/geocode/json?'
  172. # params = {'key':'AIzaSyDETbkTAmZNOECx2u4rmNudHvyoQTgDbc4', 'language':'zh-TW','latlng':''}
  173. # params['latlng'] = Lat + ',' + Lng
  174. # #print(params['address'])
  175. # res = requests.get(url, params=params)
  176. # mes = json.loads(res.text)
  177. # list = mes['results'][0]['address_components']
  178. # for tmp in list :
  179. # if "administrative_area_level_1" in tmp['types']:
  180. # city = tmp["long_name"]
  181. # if "administrative_area_level_2" in tmp['types']:
  182. # country = tmp["long_name"]
  183. # else :
  184. # return {"msg": "please input Lng and Lat", "code": 500}
  185. # return {"msg": "success", "code": 200, "result": city+','+country}
  186. # except Exception as e:
  187. # return {"msg": str(e), "code": 500}
  188. @classes.post("/insert_school")
  189. async def insert_school(
  190. request: Request,
  191. location_name: str = Form(default=''),
  192. Lng: str = Form(default=None),
  193. Lat: str = Form(default=None),
  194. address : str = Form(default=''),
  195. school_introduction : str = Form(default=''),
  196. email : str = Form(default=''),
  197. phone : str = Form(default=''),
  198. access_token:str = Form(default=None),
  199. teachers_list :str = Form(default=None),
  200. is_pass_proposal : int = Form(default=0),
  201. principal_user_email : str = Form(default=None),
  202. ):
  203. try:
  204. user_id = None
  205. if access_token:
  206. user_id = await check_token(access_token)
  207. if user_id == None:
  208. return {"msg": "沒有權限", "code": 500}
  209. # teachers = []
  210. # for teacher in teachers_list:
  211. # try:
  212. # teacher_info = await User.get(email=teacher,is_avtive=1)
  213. # teachers.append(teacher_info.id)
  214. # except:
  215. # my_log("info",__name__,f"no this teacher")
  216. try:
  217. # 這裡response採用json格式,可以自由選擇為json?或是xml?
  218. if Lat == None or Lng == None:
  219. url = 'https://maps.googleapis.com/maps/api/geocode/json?'
  220. params = {'key':'AIzaSyDETbkTAmZNOECx2u4rmNudHvyoQTgDbc4', 'address':''}
  221. params['address'] = address
  222. #print(params['address'])
  223. res = requests.get(url, params=params)
  224. mes = json.loads(res.text)
  225. Lat = mes['results'][0]['geometry']['location']['lat']
  226. Lng = mes['results'][0]['geometry']['location']['lng']
  227. print(Lat,Lng)
  228. except :
  229. my_log("error",__name__,f"not get lat and lng")
  230. principal_user_id =None
  231. if principal_user_email:
  232. try:
  233. principal_user = await User.get(email=principal_user_email,is_avtive=1)
  234. principal_user_id = principal_user.id
  235. except:
  236. my_log("info",__name__,f"no this teacher")
  237. new_school = await Schools.create(
  238. name=location_name,
  239. longitude=Lng,
  240. latitude=Lat,
  241. address = address,
  242. update_time = datetime.now(),
  243. introduction = school_introduction ,
  244. email= email,
  245. phone = phone,
  246. create_user_id = user_id,
  247. is_delete = 0,
  248. is_pass_proposal = is_pass_proposal,
  249. principal_user_id = principal_user_id,
  250. teachers = teachers_list
  251. )
  252. client_ip = request.client.host
  253. my_log("info",__name__,f"Client IP: {client_ip} - new school:{new_school.id}")
  254. return {"msg": "success", "code": 200, "location_id": new_school.id}
  255. except Exception as e:
  256. return {"msg": str(e), "code": 500}
  257. @classes.post("/insert_class_name")
  258. async def insert_class_name(
  259. name: str = Form(default=''),
  260. location_id: int = Form(default=1),
  261. category: str = Form(default=''),
  262. introduction: str = Form(default=''),
  263. organizer: str = Form(default=''),
  264. cover_img_file:UploadFile = File(default=''),
  265. group_id : int = Form(default=1),
  266. group_sort :str = Form(default=''),
  267. recommend: int = Form(default=0),
  268. special_class_list_name : str = Form(default=None),
  269. is_inner : int = Form(default=0),
  270. is_check : int = Form(default=0),
  271. access_token:str = Form(default=None),
  272. teachers_resume : str = Form(default=None),
  273. syllabus : str = Form(default=None)
  274. ):
  275. try:
  276. cover_img = ''
  277. if cover_img_file != '':
  278. contents = await cover_img_file.read()
  279. #save the file
  280. with open(f"{IMAGEDIR}{cover_img_file.filename}", "wb") as f:
  281. f.write(contents)
  282. cover_img = f"{IMAGEDIR_short}{cover_img_file.filename}"
  283. user_id = None
  284. if access_token:
  285. user_id = await check_token(access_token)
  286. if user_id == None:
  287. return {"msg": "沒有權限", "code": 500}
  288. # 課程編碼
  289. encode = ""
  290. if encode_set.__contains__(category) :
  291. encode = encode_set[category]
  292. else:
  293. encode = "OTH"
  294. now = datetime.now()
  295. encode += now.strftime("%Y%m%d")
  296. new_class_name = await Class_name.create(
  297. name=name,
  298. school_id=location_id,
  299. category=category,
  300. introduction=introduction,
  301. organizer=organizer,
  302. cover_img=cover_img,
  303. group_id=group_id,
  304. group_sort=group_sort,
  305. recommend = recommend,
  306. special_class_list_name = special_class_list_name,
  307. is_inner = is_inner,
  308. is_check = is_check,
  309. create_user_id = user_id,
  310. create_time = datetime.now(),
  311. is_delete = 0,
  312. is_record_point =0,
  313. teachers_resume = teachers_resume,
  314. syllabus = syllabus
  315. )
  316. encode += str(new_class_name.id)
  317. new_class_name.encode = encode
  318. await new_class_name.save()
  319. update_location_time(location_id= location_id)
  320. return {"msg": "success", "code": 200, "new_class_name_id": new_class_name.id}
  321. except Exception as e:
  322. return {"msg": str(e), "code": 500}
  323. # @classes.get("/insert_encode")
  324. # async def insert_encode():
  325. # try:
  326. # class_name = await Class_name.all()
  327. # for classes in class_name:
  328. # encode = ""
  329. # if encode_set.__contains__(classes.category) :
  330. # encode = encode_set[classes.category]
  331. # else:
  332. # encode = "OTH"
  333. # now = classes.create_time
  334. # encode += now.strftime("%Y%m%d")
  335. # encode += str(classes.id)
  336. # classes.encode = encode
  337. # await classes.save()
  338. # except Exception as e:
  339. # return {"msg": str(e), "code": 500}
  340. # 創建場次
  341. @classes.post("/insert_event")
  342. async def insert_event(
  343. request : Request,
  344. name_id: int = Form(default=0),
  345. event: str = Form(default=''),
  346. start_time: datetime = Form(default=None),
  347. end_time: datetime = Form(default=None),
  348. contact: str = Form(default=''),
  349. lecturer: str = Form(default=''),
  350. location: str = Form(default=''),
  351. content: str = Form(default=''),
  352. URL: str = Form(default=''),
  353. people : str = Form(default=''),
  354. fee_method: str = Form(default=''),
  355. registration_way: str = Form(default=''),
  356. registration_start: datetime = Form(default=None),
  357. registration_end: datetime = Form(default=None),
  358. number_limit: int = Form(default=None),
  359. remark : str = Form(default=''),
  360. ATM_address: str = Form(default=''),
  361. access_token:str = Form(default=None),
  362. fee_payment: str = Form(default=''),
  363. number_minimum : int = Form(default=None),
  364. files_url = Depends(create_upload_files)
  365. ):
  366. try:
  367. user_id = None
  368. if access_token:
  369. user_id = await check_token(access_token)
  370. if user_id == None:
  371. return {"msg": "沒有權限", "code": 500}
  372. # 檢查是否有該課程
  373. class_name_list = await Class_name.filter(id=name_id).all()
  374. if class_name_list == []:
  375. return {"msg": "沒有此課程", "code": 500}
  376. if files_url:
  377. if files_url.__contains__("msg"):
  378. return {"msg": files_url["msg"], "code": 500}
  379. # 判斷時間合法
  380. if start_time and end_time :
  381. if start_time >= end_time :
  382. return {"msg": "時間輸入錯誤", "code": 500}
  383. new_class = await Class_list.create(
  384. name_id=name_id,
  385. event =event,
  386. start_time=start_time,
  387. end_time=end_time,
  388. contact=contact,
  389. lecturer=lecturer,
  390. location=location,
  391. content=content,
  392. URL=URL,
  393. people=people,
  394. fee_method=fee_method,
  395. registration_way=registration_way,
  396. remark=remark,
  397. ATM_address=ATM_address,
  398. create_user_id = user_id,
  399. fee_payment = fee_payment,
  400. event_create_time = datetime.now(),
  401. files=files_url
  402. )
  403. client_ip = request.client.host
  404. try:
  405. if (registration_start and registration_end and number_limit) or number_minimum:
  406. await Class_date.create(
  407. class_list_id = new_class.id,
  408. registration_start = registration_start,
  409. registration_end = registration_end,
  410. number_limit = number_limit,
  411. amount_left = number_limit,
  412. number_minimum = number_minimum
  413. )
  414. except:
  415. print("input class_date fail")
  416. # 寄開課信
  417. try :
  418. if user_id:
  419. try:
  420. user = await User.get_or_none(id = user_id)
  421. user_info = await User_information.get_or_none(user_id=user_id)
  422. if user is None or user_info is None:
  423. my_log("info",__name__,f"Client IP: {client_ip} - {user_id} has no user resume")
  424. return {"msg": "請去建立工藝家履歷", "code": 500}
  425. email = user.email
  426. subject = "創建課程通知"
  427. # message = f"親愛的工藝家{user_info.name}您好,<br>\
  428. # 感謝您埋下工藝種子!<br>\
  429. # 您創建的課程由管理員進行審核中,將於3-7個工作天內e-mail通知您審核結果。<br>\
  430. # 若7天後尚未收到課程審核通知,請將後台畫面、聯絡資訊等截圖寄送至客服信箱:<br>\
  431. # craftology@ntcri.gov.tw,以便客服為您查詢。<br>\
  432. # 註:此封信件為系統自動發送,請勿回信,謝謝。"
  433. with open("/var/www/ntcri/assets/edm/success_open_class/index.html", 'r', encoding='utf-8') as html_file:
  434. html_template = html_file.read()
  435. message = html_template.replace("{username}",user_info.name)
  436. send_email(email,"",subject,message)
  437. except Exception as e:
  438. return {"msg": str(e), "code": 500}
  439. except:
  440. print("no this user")
  441. return {"msg": "success", "code": 200, "class_id": new_class.id}
  442. except Exception as e:
  443. return {"msg": str(e), "code": 500}
  444. @classes.post("/auto_create_session")
  445. async def auto_create_session(
  446. class_event_id : int = Form(default=0),
  447. week_day_str : str = Form(default="[]"),
  448. start_week_time : str = Form(default="[]"),
  449. end_week_time : str = Form(default="[]"),
  450. ):
  451. try:
  452. week_day = eval(week_day_str)
  453. start_week_time= eval(start_week_time)
  454. end_week_time= eval(end_week_time)
  455. #print(week_day)
  456. class_obj = await Class_list.get_or_none(id = class_event_id)
  457. if class_obj is None :
  458. return {"msg": "no this class", "code": 500}
  459. time_stemp = class_obj.start_time
  460. session = 1
  461. session_list = await Class_detail.filter(class_list_id=class_event_id).delete()
  462. while time_stemp <= class_obj.end_time:
  463. if week_day[time_stemp.weekday()]:
  464. s_start_time = time_stemp.date().strftime("%Y-%m-%d")+" "+start_week_time[time_stemp.weekday()]
  465. s_end_time = time_stemp.date().strftime("%Y-%m-%d")+" "+end_week_time[time_stemp.weekday()]
  466. #time_del = class_obj.end_time.hour - time_stemp.hour
  467. #time_del_minutes = class_obj.end_time.minute - time_stemp.minute
  468. #time_end = time_stemp + timedelta(hours=time_del,minutes=time_del_minutes)
  469. #print(time_end,class_obj.end_time.hour)
  470. day1 = datetime.strptime(s_start_time, '%Y-%m-%d %H:%M:%S')
  471. day2 = datetime.strptime(s_end_time, '%Y-%m-%d %H:%M:%S')
  472. time_difference = day2 - day1
  473. total_minutes = int(time_difference.total_seconds() // 60)
  474. hour=total_minutes//60 + round((total_minutes%60)/60,1)
  475. new_session = await Class_detail.create(
  476. class_list_id=class_event_id,
  477. #start_time=time_stemp,
  478. #end_time=time_end,
  479. start_time=s_start_time,
  480. end_time=s_end_time,
  481. hour = hour,
  482. sessions=session,
  483. content = ""
  484. )
  485. session+=1
  486. time_stemp = time_stemp + timedelta(days=1)
  487. return {"msg": "success", "code": 200}
  488. except Exception as e:
  489. return {"msg": str(e), "code": 500}
  490. @classes.post("/insert_session")
  491. async def insert_session(
  492. class_event_id : int = Form(default=0),
  493. start_time: datetime = Form(default=datetime.now()),
  494. end_time: datetime = Form(default=datetime.now()),
  495. content : str = Form(default='')
  496. ):
  497. try:
  498. session_list = await Class_detail.filter(class_list_id=class_event_id).all()
  499. session = 0
  500. if session_list != []:
  501. for session_obj in session_list:
  502. if session < session_obj.sessions:
  503. session = session_obj.sessions
  504. time_difference = end_time - start_time
  505. session_list = await Class_detail.filter(class_list_id=class_event_id).delete()
  506. total_minutes = int(time_difference.total_seconds() / 60)
  507. hour=total_minutes//60 + round((total_minutes%60)/60,1)
  508. new_session = await Class_detail.create(
  509. class_list_id=class_event_id,
  510. start_time=start_time,
  511. end_time=end_time,
  512. sessions=session +1,
  513. content = content,
  514. hour = hour
  515. )
  516. return {"msg": "success", "code": 200, "new_session_id": new_session.id}
  517. except Exception as e:
  518. return {"msg": str(e), "code": 500}
  519. @classes.post("/update_school")
  520. async def update_school(
  521. location_id: int = Form(default=0),
  522. location_name: str = Form(default=''),
  523. Lng: str = Form(default=''),
  524. Lat: str = Form(default=''),
  525. address : str = Form(default=''),
  526. school_introduction : str = Form(default=None),
  527. email : str = Form(default=None),
  528. phone : str = Form(default=None),
  529. teachers_list : List[str] = Form(default=[]),
  530. is_pass_proposal : int = Form(default=None),
  531. principal_user_email : str = Form(default=None)
  532. ):
  533. try:
  534. school = await Schools.get(id=location_id)
  535. if location_name.strip() != '':
  536. school.name = location_name
  537. if Lng != '':
  538. school.longitude = Lng
  539. if Lat.strip() != '':
  540. school.latitude = Lat
  541. if address.strip() != '':
  542. school.address = address
  543. if school_introduction:
  544. school.introduction = school_introduction
  545. if email:
  546. school.email = email
  547. if phone:
  548. school.phone = phone
  549. if is_pass_proposal is not None:
  550. if is_pass_proposal == 0 and school.is_pass_proposal == 2:
  551. # 寄開課信
  552. try :
  553. if school.create_user_id:
  554. try:
  555. try :
  556. user = await User.get(id = school.create_user_id)
  557. user_info = await User_information.get(user_id = school.create_user_id)
  558. except:
  559. return {"msg": "請去建立工藝家履歷", "code": 500}
  560. email = user.email
  561. subject = "重新提案通知"
  562. # message = f"親愛的工藝家{user_info.name}您好,<br>\
  563. # 感謝您埋下工藝種子!<br>\
  564. # 您的提案由管理員進行審核中,將於3-7個工作天內e-mail通知您審核結果。<br>\
  565. # 若7天後尚未收到課程審核通知,請將後台畫面、聯絡資訊等截圖寄送至客服信箱:<br>\
  566. # craftology@ntcri.gov.tw,以便客服為您查詢。<br>\
  567. # 註:此封信件為系統自動發送,請勿回信,謝謝。"
  568. with open("/var/www/ntcri/assets/edm/porposal/index.html", 'r', encoding='utf-8') as html_file:
  569. html_template = html_file.read()
  570. message = html_template.replace("{username}",user_info.name)
  571. send_email(email,"",subject,message)
  572. except:
  573. print("have no email")
  574. except:
  575. print("no this user")
  576. if is_pass_proposal != 0 and is_pass_proposal ==1 :
  577. if school.create_user_id:
  578. try:
  579. user = await User.get_or_none(id = school.create_user_id)
  580. user_info = await User_information.get(user_id = school.create_user_id)
  581. if user is None :
  582. return {"msg": "請去建立工藝家履歷", "code": 500}
  583. email = user.email
  584. subject = "提案審核通過通知"
  585. # message = f"親愛的工藝家{user_info.name}您好,<br>\
  586. # 恭喜您埋下工藝種子已成功發芽,讓我們一起期待它的成長茁壯!<br>\
  587. # 您的提案已由管理員審核通過!<br>\
  588. # 您可透過「會員專區」→「我的提案」檢視提案內容,並開始開課。"
  589. with open("/var/www/ntcri/assets/edm/pass_porposal/index.html", 'r', encoding='utf-8') as html_file:
  590. html_template = html_file.read()
  591. message = html_template.replace("{username}",user_info.name)
  592. send_email(email,"",subject,message)
  593. except:
  594. print("have no email")
  595. school.is_pass_proposal = is_pass_proposal
  596. if principal_user_email:
  597. try:
  598. principal_user = await User.get(email=principal_user_email,is_avtive=1)
  599. school.principal_user_id = principal_user.id
  600. except:
  601. my_log("info",__name__,f"no this teacher")
  602. if teachers_list != []:
  603. teachers = []
  604. for teacher in teachers_list:
  605. try:
  606. teacher_info = await User.get(email=teacher,is_avtive=1)
  607. teachers.append(teacher_info.id)
  608. except:
  609. my_log("info",__name__,f"no this teacher")
  610. school.teachers = str(teachers)
  611. await school.save()
  612. update_location_time(location_id= location_id)
  613. return {"msg": "success", "code": 200}
  614. except Exception as e:
  615. return {"msg": str(e), "code": 500}
  616. @classes.post("/update_class_name")
  617. async def update_class_name(
  618. request : Request,
  619. class_name_id: int = Form(default=0),
  620. name: str = Form(default=None),
  621. location_id: int = Form(default=None),
  622. category: str = Form(default=None),
  623. introduction: str = Form(default=None),
  624. organizer: str = Form(default=None),
  625. cover_img_file:UploadFile = File(default=""),
  626. group_id : int = Form(default=None),
  627. group_sort : str = Form(default=None),
  628. recommend: int = Form(default=None),
  629. special_class_list_name : str = Form(default=None),
  630. is_inner : int = Form(default=None),
  631. is_check : int = Form(default=None),
  632. teachers_resume : str = Form(default=None),
  633. syllabus : str = Form(default=None)
  634. ):
  635. try:
  636. class_name = await Class_name.get(id=class_name_id)
  637. if name is not None:
  638. class_name.name = name
  639. if location_id is not None:
  640. class_name.school_id = location_id
  641. update_location_time(location_id= location_id)
  642. if category is not None:
  643. class_name.category = category
  644. if introduction is not None:
  645. class_name.introduction = introduction
  646. if organizer is not None:
  647. class_name.organizer = organizer
  648. if group_id is not None :
  649. class_name.group_id = group_id
  650. if is_inner is not None:
  651. class_name.is_inner = is_inner
  652. if syllabus is not None:
  653. class_name.syllabus = syllabus
  654. if teachers_resume is not None:
  655. class_name.teachers_resume = teachers_resume
  656. if cover_img_file != "":
  657. contents = await cover_img_file.read()
  658. with open(f"{IMAGEDIR}{cover_img_file.filename}", "wb") as f:
  659. f.write(contents)
  660. class_name.cover_img = f"{IMAGEDIR_short}{cover_img_file.filename}"
  661. if group_sort is not None:
  662. class_name.group_sort = group_sort
  663. if recommend is not None:
  664. class_name.recommend = recommend
  665. client_ip = request.client.host
  666. if is_check is not None:
  667. if class_name.is_check == 0 and is_check ==1:
  668. user_id = class_name.create_user_id
  669. if user_id:
  670. try:
  671. user = await User.get(id = user_id)
  672. user_info = await User_information.get(user_id = user_id)
  673. except:
  674. # my_log(type,name,msg)
  675. my_log("info",__name__,f"Client IP: {client_ip} - {user_id} has no user resume")
  676. return {"msg": "請去建立工藝家履歷", "code": 500}
  677. email = user.email
  678. subject = "課程核准通知"
  679. # message = f"親愛的工藝家{user_info.name}您好,<br>\
  680. # 恭喜您埋下工藝種子已成功發芽,讓我們一起期待它的成長茁壯!<br>\
  681. # 您創建的課程已由管理員審核通過並上架成功!<br>\
  682. # 您可透過「會員專區」→「我的開課」檢視跟管理學員報名狀況。<br>\
  683. # 註:此封信件為系統自動發送,請勿回信,謝謝。"
  684. with open("/var/www/ntcri/assets/edm/pass_check/index.html", 'r', encoding='utf-8') as html_file:
  685. html_template = html_file.read()
  686. message = html_template.replace("{username}",user_info.name)
  687. try:
  688. send_email(email,"",subject,message)
  689. except:
  690. my_log("error",__name__,f"Client IP: {client_ip} - send email fail")
  691. class_name.is_check = is_check
  692. if special_class_list_name:
  693. class_name.special_class_list_name = special_class_list_name
  694. await class_name.save()
  695. return {"msg": "success", "code": 200}
  696. except Exception as e:
  697. return {"msg": str(e), "code": 500}
  698. @classes.post("/cancel_proposal")
  699. async def cancel_proposal(
  700. request: Request,
  701. location_id: int = Form(default=0),
  702. access_token:str = Form(default=None),
  703. message:str = Form(default=None)
  704. ):
  705. try:
  706. user_id = None
  707. if access_token:
  708. user_id = await check_token(access_token)
  709. if user_id:
  710. user = await User.get(id=user_id)
  711. else :
  712. return {"msg": "no access", "code": 200}
  713. if user.is_superuser != 2:
  714. return {"msg": "no access", "code": 200}
  715. # 檢查是否有該課程
  716. class_name_list = await Schools.filter(id=location_id).all()
  717. if class_name_list == []:
  718. my_log("info",__name__,f"Client IP: {client_ip} - no this class_name_id:{location_id}")
  719. return {"msg": "沒有此課程", "code": 200}
  720. class_name = await Schools.get(id=location_id)
  721. class_name.is_pass_proposal = 2
  722. await class_name.save()
  723. if class_name.create_user_id:
  724. creater = await User.get(id=class_name.create_user_id)
  725. user_info = await User_information.get_or_none(user_id=user_id)
  726. email = creater.email
  727. subject = '提案駁回通知'
  728. # email_message = f'親愛的工藝家{creater.username}您好,<br>\
  729. # 很遺憾您埋下的工藝種子未能成功發芽。<br>\
  730. # 您的提案經管理員審核,因{message}未能通過審核,<br>\
  731. # 請別灰心!只要修正完善內容再次提交即有機會讓您的工藝種子發芽。<br>\
  732. # 若對於審核結果有疑慮,請將後台畫面、聯絡資訊等截圖寄送至客服信箱:<br>\
  733. # craftology@ntcri.gov.tw,以便客服為您查詢。<br>\
  734. # 註:此封信件為系統自動發送,請勿回信,謝謝。'
  735. with open("/var/www/ntcri/assets/edm/porposal_refuse/index.html", 'r', encoding='utf-8') as html_file:
  736. html_template = html_file.read()
  737. email_message = html_template.replace("{username}",user_info.name)
  738. email_message = email_message.replace("{msg}",message)
  739. send_email(email,"",subject,email_message)
  740. else :
  741. print("no creater")
  742. client_ip = request.client.host
  743. my_log("info",__name__,f"{client_ip} - school_id:{location_id} change is_pass_proposal to 2")
  744. return {"msg": "success", "code": 200}
  745. except Exception as e:
  746. client_ip = request.client.host
  747. my_log("error",__name__,f"{client_ip} - An exception occurred: {e} \n Request : {request}")
  748. return {"msg": str(e), "code": 500}
  749. @classes.post("/cancel_class_check")
  750. async def cancel_class_check(
  751. request: Request,
  752. class_name_id: int = Form(default=0),
  753. access_token:str = Form(default=None),
  754. message:str = Form(default=None)
  755. ):
  756. try:
  757. user_id = None
  758. if access_token:
  759. user_id = await check_token(access_token)
  760. if user_id:
  761. user = await User.get(id=user_id)
  762. else :
  763. return {"msg": "no access", "code": 200}
  764. if user.is_superuser != 2:
  765. return {"msg": "no access", "code": 200}
  766. # 檢查是否有該課程
  767. class_name_list = await Class_name.filter(id=class_name_id).all()
  768. if class_name_list == []:
  769. my_log("info",__name__,f"Client IP: {client_ip} - no this class_name_id:{class_name_id}")
  770. return {"msg": "沒有此課程", "code": 200}
  771. class_name = await Class_name.get(id=class_name_id)
  772. class_name.is_check = 2
  773. await class_name.save()
  774. if class_name.create_user_id:
  775. creater = await User.get(id=class_name.create_user_id)
  776. user_info = await User_information.get_or_none(user_id=user_id)
  777. email = creater.email
  778. subject = '開課駁回通知'
  779. # email_message = f'親愛的工藝家{creater.username}您好,<br>\
  780. # 很遺憾您埋下的工藝種子未能成功發芽。<br>\
  781. # 您創建的課程經管理員審核,因{message}未能通過審核,<br>\
  782. # 請別灰心!只要修正完善內容再次提交即有機會讓您的工藝種子發芽。<br>\
  783. # 若對於審核結果有疑慮,請將後台畫面、聯絡資訊等截圖寄送至客服信箱:<br>\
  784. # craftology@ntcri.gov.tw,以便客服為您查詢。<br>\
  785. # 註:此封信件為系統自動發送,請勿回信,謝謝。'
  786. with open("/var/www/ntcri/assets/edm/class_refuse/index.html", 'r', encoding='utf-8') as html_file:
  787. html_template = html_file.read()
  788. email_message = html_template.replace("{username}",user_info.name)
  789. email_message = email_message.replace("{msg}",message)
  790. send_email(email,"",subject,email_message)
  791. else :
  792. print("no creater")
  793. client_ip = request.client.host
  794. my_log("info",__name__,f"{client_ip} - class_name_id:{class_name_id} change is_check to 2")
  795. return {"msg": "success", "code": 200}
  796. except Exception as e:
  797. client_ip = request.client.host
  798. my_log("error",__name__,f"{client_ip} - An exception occurred: {e} \n Request : {request}")
  799. return {"msg": str(e), "code": 500}
  800. @classes.post("/update_event")
  801. async def update_event(
  802. id: int = Form(default=0),
  803. name_id: int = Form(default=None),
  804. event: str = Form(default=None),
  805. start_time: datetime = Form(default=None),
  806. end_time: datetime = Form(default=None),
  807. contact: str = Form(default=None),
  808. lecturer: str = Form(default=None),
  809. location: str = Form(default=None),
  810. content: str = Form(default=None),
  811. URL: str = Form(default=None),
  812. people : str = Form(default=None),
  813. fee_method: str = Form(default=None),
  814. registration_way: str = Form(default=None),
  815. registration_start: datetime = Form(default=None),
  816. registration_end: datetime = Form(default=None),
  817. number_limit: int = Form(default=None),
  818. remark : str = Form(default=None),
  819. ATM_address :str = Form(default=None),
  820. fee_payment : str = Form(default=None),
  821. number_minimum: int = Form(default=None),
  822. files_url = Depends(create_upload_files)
  823. ):
  824. try:
  825. class_obj = await Class_list.get_or_none(id=id)
  826. if class_obj is None :
  827. return {"msg": "no this event", "code": 500}
  828. if name_id != None:
  829. class_obj.name_id = name_id
  830. if event != None:
  831. class_obj.event = event
  832. if start_time != None:
  833. class_obj.start_time = start_time
  834. if end_time != None:
  835. class_obj.end_time = end_time
  836. if lecturer != None:
  837. class_obj.lecturer = lecturer
  838. if location != None:
  839. class_obj.location = location
  840. if contact != None:
  841. class_obj.contact = contact
  842. if content!= None:
  843. class_obj.content = content
  844. if URL!= None:
  845. class_obj.URL = URL
  846. if people!= None:
  847. class_obj.people = people
  848. if fee_method!= None:
  849. class_obj.fee_method = fee_method
  850. if registration_way!= None:
  851. class_obj.registration_way = registration_way
  852. if remark!= None:
  853. class_obj.remark = remark
  854. if ATM_address!= None:
  855. class_obj.ATM_address = ATM_address
  856. if fee_payment!= None:
  857. class_obj.fee_payment = fee_payment
  858. if files_url:
  859. if files_url.__contains__("msg"):
  860. return {"msg": files_url["msg"], "code": 500}
  861. class_obj.files_url = files_url
  862. class_date_obj,created = await Class_date.get_or_create(
  863. class_list_id=id,
  864. defaults={
  865. "registration_start" : registration_start,
  866. "registration_end" : registration_end,
  867. "number_limit" : number_limit,
  868. "amount_left" : number_limit,
  869. "number_minimum" : number_minimum
  870. }
  871. )
  872. if not created :
  873. if registration_start :
  874. class_date_obj.registration_start = registration_start
  875. if registration_end :
  876. class_date_obj.registration_end = registration_end
  877. if number_limit :
  878. class_date_obj.number_limit = number_limit
  879. if number_minimum != None:
  880. class_date_obj.number_minimum = number_minimum
  881. await class_date_obj.save()
  882. await class_obj.save()
  883. return {"msg": "success", "code": 200}
  884. except Exception as e:
  885. return {"msg": str(e), "code": 500}
  886. @classes.post("/update_session")
  887. async def update_session(
  888. session_id : int = Form(default=0),
  889. class_event_id : int = Form(default=0),
  890. start_time: datetime = Form(default=datetime.now()),
  891. end_time: datetime = Form(default=datetime.now()),
  892. sessions: str = Form(default=0),
  893. content : str = Form(default='')
  894. ):
  895. try:
  896. class_session_obj = await Class_detail.get(id=session_id)
  897. if class_event_id != 0:
  898. class_session_obj.class_list_id = class_event_id
  899. if start_time != '':
  900. class_session_obj.start_time = start_time
  901. if end_time != '':
  902. class_session_obj.end_time = end_time
  903. if sessions.strip() != '':
  904. class_session_obj.sessions = sessions
  905. if content.strip() != '':
  906. class_session_obj.content = content
  907. await class_session_obj.save()
  908. return {"msg": "success", "code": 200}
  909. except Exception as e:
  910. return {"msg": str(e), "code": 500}
  911. @classes.post("/delete_school")
  912. async def delete_school(location_id: int):
  913. if location_id:
  914. try:
  915. school = await Schools.get(id=location_id)
  916. except:
  917. return {"msg": "無法找到據點", "code": 500}
  918. school.is_delete = (school.is_delete+1)%2
  919. await school.save()
  920. return {"msg": "success", "code": 200}
  921. @classes.post("/delete_session")
  922. async def delete_session(id: int):
  923. if id:
  924. await Class_detail.filter(id=id).delete()
  925. return {"msg": "success", "code": 200}
  926. @classes.post("/delete_event")
  927. async def delete(id: int):
  928. if id:
  929. await Class_detail.filter(class_list_id=id).delete()
  930. await Class_list.filter(id=id).delete()
  931. await Class_date.filter(class_list_id=id).delete()
  932. return {"msg": "success", "code": 200}
  933. @classes.post("/delete_class_name")
  934. async def delete(id: int):
  935. if id:
  936. try:
  937. class_name = await Class_name.get(id=id)
  938. except:
  939. return {"msg": "無法找到課程", "code": 500}
  940. class_name.is_delete = (class_name.is_delete+1)%2 # 相反狀態(如果本來被刪除就恢復)
  941. await class_name.save()
  942. # class_event_list = await Class_list.filter(name_id=id).all()
  943. # for class_event_obj in class_event_list:
  944. # await Class_detail.filter(class_list_id=class_event_obj.id).delete()
  945. # await Class_date.filter(class_list_id=class_event_obj.id).delete()
  946. # await Class_list.filter(name_id=id).delete()
  947. # await Class_name.filter(id=id).delete()
  948. return {"msg": "success", "code": 200}
  949. @classes.get("/get_class_state")
  950. async def check_date_state(
  951. class_name_id : Optional[int] = None,
  952. class_event_id : Optional[int] = None,
  953. special_class_list_name : Optional[str] = None,
  954. ):
  955. if class_name_id:
  956. Q_word = Q(name_id=class_name_id)
  957. elif class_event_id:
  958. Q_word = Q(id=class_event_id)
  959. else:
  960. return {"msg": 'please input ID', "code": 500}
  961. if special_class_list_name == "one_day_class":
  962. class_list = await One_day_class.filter(Q_word).all()
  963. elif special_class_list_name == "outter_class_list":
  964. class_list = await Outter_class_list.filter(Q_word).all()
  965. else :
  966. class_list = await Class_list.filter(Q_word).all()
  967. result = {"state": "尚未開放報名"}
  968. try:
  969. if class_list == [] :
  970. result["state"] = "尚未創建場次"
  971. for class_obj in class_list:
  972. try:
  973. class_name = await Class_name.get_or_none(id = class_obj.name_id)
  974. if class_name.is_check == 0 :
  975. result["state"] = "課程等待審核"
  976. break
  977. if class_name.is_check == 2 :
  978. result["state"] = "課程審核未通過"
  979. break
  980. if class_name:
  981. print(class_name.is_record_point)
  982. print(class_name.name)
  983. school = await Schools.get_or_none(id = class_name.school_id)
  984. print(class_name.school_id)
  985. if school :
  986. print(school.is_pass_proposal)
  987. if school.is_pass_proposal == 0 :
  988. result["state"] = "正在提案階段"
  989. break
  990. if school.is_pass_proposal == 2 :
  991. result["state"] = "提案已被駁回"
  992. break
  993. if class_name.is_record_point == 1 :
  994. result["state"] = "課程已關閉"
  995. break
  996. if class_obj.start_time and class_obj.end_time:
  997. if class_obj.start_time.replace(tzinfo=None) <= datetime.now() and class_obj.end_time.replace(tzinfo=None) >= datetime.now():
  998. result["state"] = "開課中"
  999. break
  1000. elif class_obj.end_time.replace(tzinfo=None) < datetime.now():
  1001. result["state"] = "課程已結束"
  1002. else:
  1003. pass
  1004. else :
  1005. result["state"] = "尚未創建課程"
  1006. except Exception as e:
  1007. pass
  1008. if special_class_list_name == "one_day_class":
  1009. if class_obj.reg_deadline.replace(tzinfo=None) >= datetime.now() :
  1010. result["state"] = "報名中"
  1011. break
  1012. elif class_obj.reg_deadline.replace(tzinfo=None) < datetime.now():
  1013. result["state"] = "報名截止(報名額滿)"
  1014. break
  1015. else :
  1016. try:
  1017. class_date_obj = await Class_date.get(class_list_id=class_obj.id)
  1018. if class_date_obj.registration_start.replace(tzinfo=None) <= datetime.now() and class_date_obj.registration_end.replace(tzinfo=None) >= datetime.now() :
  1019. if class_date_obj.amount_left == 0:
  1020. result["state"] = "報名截止(報名額滿)"
  1021. break
  1022. else:
  1023. result["state"] = "報名中"
  1024. break
  1025. elif class_date_obj.registration_end.replace(tzinfo=None) < datetime.now():
  1026. result["state"] = "報名截止(人數已滿)"
  1027. break
  1028. except Exception as e:
  1029. pass
  1030. return {"msg": "success", "code": 200, "result": result}
  1031. except Exception as e:
  1032. return {"msg": str(e), "code": 500}
  1033. @classes.get("/get_event")
  1034. async def search_event(
  1035. class_name_id: Optional[int] = None,
  1036. event_id : Optional[int] = None,
  1037. access_token: Optional[str] = None
  1038. ):
  1039. try:
  1040. try :
  1041. class_name_obj = await Class_name.get(id=class_name_id)
  1042. except:
  1043. return {"msg": "no this class id", "code": 500}
  1044. special_class_list_name = class_name_obj.special_class_list_name
  1045. Q_word = Q()
  1046. if class_name_id:
  1047. Q_word = Q_word | Q(name_id = class_name_id)
  1048. elif event_id:
  1049. Q_word = Q_word | Q(id = event_id)
  1050. else :
  1051. return {"msg": "please input class_name_id or event_id", "code": 500}
  1052. user_id = None
  1053. if access_token:
  1054. user_id = await check_token(access_token)
  1055. if user_id:
  1056. Q_word = Q_word & Q(create_user_id = user_id)
  1057. model_fields =[]
  1058. state = {}
  1059. if special_class_list_name==None:
  1060. class_list = Class_list.filter(Q_word).all().order_by("-start_time")
  1061. elif special_class_list_name=='one_day_class':
  1062. class_list = One_day_class.filter(Q_word).all().order_by("-start_time")
  1063. elif special_class_list_name=='outter_class_list':
  1064. class_list = Outter_class_list.filter(Q_word).all().order_by("-start_time")
  1065. else:
  1066. class_list = Class_list.filter(Q_word).all().order_by("-start_time")
  1067. class_list = await class_list.all()
  1068. classes = []
  1069. for class_obj in class_list:
  1070. class_name_obj = await Class_name.get(id=class_obj.name_id)
  1071. class_name = class_name_obj.name
  1072. try :
  1073. class_data = class_obj.show_data()
  1074. class_data["class_name"] = class_name
  1075. state = await check_date_state(class_event_id=class_obj.id,special_class_list_name=special_class_list_name)
  1076. class_data["state"] = state["result"]["state"]
  1077. try:
  1078. class_date_obj = await Class_date.get(class_list_id=class_obj.id)
  1079. class_data["registration_start"] = class_date_obj.registration_start
  1080. class_data["registration_end"] = class_date_obj.registration_end
  1081. class_data["number_limit"] = class_date_obj.number_limit
  1082. class_data["amount_left"] = class_date_obj.amount_left
  1083. class_data["number_minimum"] = class_date_obj.number_minimum
  1084. model_fields.append("registration_start","registration_end","number_limit","amount_left")
  1085. except:
  1086. print("沒有報名時間&人數限制的資料")
  1087. except:
  1088. class_data = {
  1089. "msg" : "fail to get data"
  1090. }
  1091. classes.append(class_data)
  1092. return {"msg": "success", "code": 200, "classes": classes}
  1093. except Exception as e:
  1094. return {"msg": str(e), "code": 500}
  1095. @classes.get("/get_school")
  1096. async def get_school(
  1097. location_id : Optional[int] = None,
  1098. keyword : Optional[str] = None,
  1099. location_keyword : Optional[str] = None,
  1100. page_num : Optional[int] = None,
  1101. page_amount : Optional[int] = None,
  1102. category : Optional[str] = None,
  1103. access_token: Optional[str] = None,
  1104. is_delete: Optional[int] = None,
  1105. is_pass_proposal : Optional[int] = None,
  1106. principal_user_email : Optional[str] = None
  1107. ):
  1108. try:
  1109. await copy()
  1110. my_log("info",__name__,"get_school")
  1111. Q_word = Q()
  1112. if location_id :
  1113. Q_word = Q_word & Q(id = location_id)
  1114. if keyword :
  1115. Q_word = Q_word & Q(name__icontains=keyword)
  1116. if location_keyword :
  1117. Q_word = Q_word & Q(address__icontains=location_keyword)
  1118. if is_pass_proposal is not None :
  1119. Q_word = Q_word &Q(is_pass_proposal=is_pass_proposal)
  1120. if principal_user_email :
  1121. try:
  1122. principal_user = await User.get(email=principal_user_email,is_avtive=1)
  1123. except:
  1124. my_log("info",__name__,f"no this teacher")
  1125. Q_word = Q_word & Q(principal_user_id=principal_user.id)
  1126. if is_delete is not None:
  1127. Q_word = Q_word &Q(is_delete=is_delete)
  1128. else :
  1129. Q_word = Q_word &Q(is_delete=0)
  1130. user_id = None
  1131. if access_token:
  1132. user_id = await check_token(access_token)
  1133. if user_id:
  1134. Q_word = Q_word &Q(create_user_id = user_id)
  1135. Q_word2 = Q()
  1136. if category:
  1137. for tmp_word in category.split(",") :
  1138. Q_word2 = Q_word2 | Q(category__icontains=tmp_word)
  1139. Q_word = Q_word & Q_word2
  1140. school_list = Schools.filter(Q_word)
  1141. # for school_obj in school_list_tmp:
  1142. # class_list = await Class_name.filter(Q(school_id = school_obj.id) & Q_word)
  1143. # if class_list == []:
  1144. # school_list = school_list.exclude(id = school_obj.id)
  1145. count = await school_list.all().count()
  1146. if page_num and page_amount:
  1147. school_list = school_list.offset((page_num-1)*page_amount).limit(page_amount)
  1148. school_list = await school_list.all().order_by("-update_time")
  1149. schools = []
  1150. for school_obj in school_list:
  1151. try :
  1152. school_data = school_obj.show_data()
  1153. if school_data["Lng"] == (None or '') or school_data["Lat"] == (None or ''):
  1154. try:
  1155. # 這裡response採用json格式,可以自由選擇為json?或是xml?
  1156. url = 'https://maps.googleapis.com/maps/api/geocode/json?'
  1157. params = {'key':'AIzaSyDETbkTAmZNOECx2u4rmNudHvyoQTgDbc4', 'address':''}
  1158. params['address'] = school_data["address"]
  1159. #print(params['address'])
  1160. res = requests.get(url, params=params)
  1161. mes = json.loads(res.text)
  1162. school_data["Lat"] = mes['results'][0]['geometry']['location']['lat']
  1163. school_data["Lng"] = mes['results'][0]['geometry']['location']['lng']
  1164. print(school_data["Lat"],school_data["Lng"])
  1165. school_obj.longitude = school_data["Lng"]
  1166. school_obj.latitude = school_data["Lng"]
  1167. await school_obj.save()
  1168. except :
  1169. my_log("error",__name__,f"not get lat and lng")
  1170. schools.append(school_data)
  1171. except:
  1172. schools.append({"msg : data wrong"})
  1173. return {"msg": "success", "code": 200, "total_num" : count,"schools": schools}
  1174. except Exception as e:
  1175. return {"msg": str(e), "code": 500}
  1176. @classes.get("/get_group_name")
  1177. async def get_school_group(
  1178. id : Optional[int] = 0
  1179. ):
  1180. try:
  1181. if id==0:
  1182. school_group_list = await Group_name.all()
  1183. print(school_group_list)
  1184. else:
  1185. school_group_list = [await Group_name.get(id=id)]
  1186. print(school_group_list)
  1187. school_groups = []
  1188. for school_obj in school_group_list:
  1189. school_data = {
  1190. "group_id": school_obj.id,
  1191. "group_name": school_obj.group_name,
  1192. "describe": school_obj.describe
  1193. }
  1194. school_groups.append(school_data)
  1195. return {"msg": "success", "code": 200, "school_groups": school_groups}
  1196. except Exception as e:
  1197. return {"msg": str(e), "code": 500}
  1198. @classes.post("/update_group_name")
  1199. async def update_school_group(
  1200. id : int = Form(default=0),
  1201. group_name : str = Form(default=''),
  1202. describe : str = Form(default=''),
  1203. ):
  1204. try:
  1205. group_name_obj = await Group_name.get(id=id)
  1206. if group_name.strip() != '':
  1207. group_name_obj.group_name = group_name
  1208. if describe.strip() != '':
  1209. group_name_obj.describe = describe
  1210. await group_name_obj.save()
  1211. return {"msg": "success", "code": 200}
  1212. except Exception as e:
  1213. return {"msg": str(e), "code": 500}
  1214. @classes.get("/get_class_name")
  1215. async def get_class_name(
  1216. location_id : str = None ,
  1217. class_name_id : Optional[int] = None,
  1218. group_id : Optional[int] = None,
  1219. group_sort :Optional[str] = None,
  1220. exclude_word: Optional[str] = None,
  1221. category :Optional[str] = None,
  1222. page_num : Optional[int] = None,
  1223. page_amount : Optional[int] = None,
  1224. recommend : Optional[int] = None,
  1225. is_inner : Optional[int] = None,
  1226. is_check : Optional[int] = None,
  1227. access_token: Optional[str] = None,
  1228. is_delete: Optional[int] = None,
  1229. keyword: Optional[str] = None,
  1230. has_user : Optional[int] = None,
  1231. is_record_point : Optional[int] = None,
  1232. location_keyword : Optional[str] = None,
  1233. encode : Optional[str] = None,
  1234. syllabus : Optional[str] = None
  1235. ):
  1236. try:
  1237. await copy()
  1238. class_name_list = Class_name.all()
  1239. Q_word = Q()
  1240. if location_keyword:
  1241. school_list = await Schools.filter(Q(address__icontains=location_keyword)).all()
  1242. schools = []
  1243. for school in school_list:
  1244. schools.append(school.id)
  1245. Q_word = Q_word & Q(school_id__in = schools)
  1246. if group_id:
  1247. Q_word = Q_word & Q(group_id = group_id)
  1248. if group_sort:
  1249. Q_word = Q_word & Q(group_sort = group_sort)
  1250. if encode:
  1251. Q_word = Q_word & Q(encode = encode)
  1252. if syllabus:
  1253. Q_word = Q_word & Q(syllabus__icontains = syllabus)
  1254. if category:
  1255. Q_word2 = Q()
  1256. for tmp_word in category.split(",") :
  1257. Q_word2 = Q_word2 | Q(category__icontains=tmp_word)
  1258. Q_word = Q_word &Q_word2
  1259. if location_id :
  1260. location_id_list = eval(location_id)
  1261. if not isinstance(location_id_list, list):
  1262. location_id_list = [location_id_list]
  1263. Q_word = Q_word & Q(school_id__in = location_id_list)
  1264. if class_name_id :
  1265. Q_word = Q_word & Q(id = class_name_id)
  1266. if recommend :
  1267. Q_word = Q_word & Q(recommend = recommend)
  1268. if is_inner!=None:
  1269. Q_word = Q_word & Q(is_inner = is_inner)
  1270. if is_check!=None:
  1271. if is_check == 3 :
  1272. Q_word = Q_word & Q(is_record_point =1) #已關閉
  1273. else:
  1274. Q_word = Q_word & Q(is_check = is_check,is_record_point__in = [0,None])
  1275. if is_delete is not None:
  1276. Q_word = Q_word & Q(is_delete = is_delete)
  1277. elif is_delete == 2:
  1278. pass
  1279. else :
  1280. Q_word = Q_word & Q(is_delete = 0)
  1281. if keyword:
  1282. Q_word = Q_word & ( Q(name__icontains=keyword)|
  1283. Q(category__icontains=keyword)|
  1284. Q(introduction__icontains=keyword)|
  1285. Q(organizer__icontains=keyword)|
  1286. Q(group_sort__icontains=keyword) |
  1287. Q(syllabus__icontains=keyword))
  1288. if is_record_point :
  1289. Q_word = Q_word & Q(is_record_point =is_record_point)
  1290. user_id = None
  1291. if access_token:
  1292. user_id = await check_token(access_token)
  1293. if user_id:
  1294. Q_word = Q_word & Q(create_user_id = user_id)
  1295. else :
  1296. return {"msg": "no access", "code": 500}
  1297. if has_user==1:
  1298. Q_word = Q_word & Q(create_user_id__isnull=False)
  1299. class_name_list = class_name_list.filter(Q_word).all()
  1300. if exclude_word:
  1301. for tmp_word in exclude_word.split(",") :
  1302. # print(tmp_word)
  1303. class_name_list = class_name_list.exclude(Q(category__icontains=tmp_word)|Q(group_sort__icontains=tmp_word)).all()
  1304. count = await class_name_list.all().count()
  1305. if page_num and page_amount:
  1306. class_name_list = class_name_list.offset((page_num-1)*page_amount).limit(page_amount)
  1307. class_name_list = await class_name_list.all().order_by("-id")
  1308. classes_name = []
  1309. state = {}
  1310. special_class_list_name = None
  1311. for class_name_obj in class_name_list:
  1312. try:
  1313. school_obj = await Schools.get(id=class_name_obj.school_id)
  1314. school_obj_id = school_obj.id
  1315. except:
  1316. school_obj_id = 0
  1317. try:
  1318. special_class_list_name = class_name_obj.special_class_list_name
  1319. if special_class_list_name:
  1320. state = await check_date_state(class_name_id=class_name_obj.id,special_class_list_name=special_class_list_name)
  1321. else:
  1322. state = await check_date_state(class_name_id=class_name_obj.id)
  1323. class_data = class_name_obj.show_data()
  1324. if school_obj_id:
  1325. for key, item in school_obj.show_data().items():
  1326. class_data[key] = item
  1327. class_data["state"]=state["result"]["state"]
  1328. if class_data["state"] == "課程已關閉" :
  1329. class_data["is_check"] = 3
  1330. except Exception as e:
  1331. class_data = {
  1332. "msg" : str(e)
  1333. }
  1334. classes_name.append(class_data)
  1335. return {"msg": "success", "code": 200,"total_num" : count,"classes": classes_name}
  1336. except Exception as e:
  1337. return {"msg": str(e), "code": 500}
  1338. @classes.get("/get_session")
  1339. async def get_session(
  1340. event_id : Optional[int] = None
  1341. ):
  1342. try:
  1343. if not event_id:
  1344. return {"msg": "please input event_id", "code": 500}
  1345. class_session_list = await Class_detail.filter(class_list_id=event_id).all().order_by("start_time")
  1346. classe_sessions = []
  1347. week_day = [0] * 7
  1348. start_week_time= [None] * 7
  1349. end_week_time= [None] * 7
  1350. for class_session_obj in class_session_list:
  1351. class_session_data = {
  1352. "session_id": class_session_obj.id,
  1353. "class_event_id": class_session_obj.class_list_id,
  1354. "start_time": class_session_obj.start_time,
  1355. "end_time": class_session_obj.end_time,
  1356. "sessions": class_session_obj.sessions,
  1357. "content": class_session_obj.content
  1358. }
  1359. week_day[class_session_obj.start_time.weekday()] = 1
  1360. start_week_time[class_session_obj.start_time.weekday()]=class_session_obj.start_time.time().strftime("%H:%M")
  1361. end_week_time[class_session_obj.end_time.weekday()]=class_session_obj.end_time.time().strftime("%H:%M")
  1362. classe_sessions.append(class_session_data)
  1363. if len(class_session_list)== 1 :
  1364. data_form = "one day class"
  1365. else :
  1366. data_form = {
  1367. "week_day" : week_day,
  1368. "start_week_time" : start_week_time,
  1369. "end_week_time" :end_week_time
  1370. }
  1371. return {"msg": "success", "code": 200, "data_form":data_form,"classe_sessions": classe_sessions}
  1372. except Exception as e:
  1373. return {"msg": str(e), "code": 500}
  1374. @classes.get("/search_class_like")
  1375. async def search_class_like(
  1376. keyword: str,
  1377. location_id : Optional[int] = None ,
  1378. group_id : Optional[int] = None,
  1379. group_sort :Optional[str] = None,
  1380. exclude_word: Optional[str] = None,
  1381. category :Optional[str] = None,
  1382. page_num : Optional[int] = None,
  1383. page_amount : Optional[int] = None,
  1384. recommend : Optional[int] = None,
  1385. is_inner : Optional[int] = None,
  1386. is_check : Optional[int] = None
  1387. ):
  1388. try:
  1389. Q_word = Q()
  1390. if group_id:
  1391. Q_word = Q_word & Q(group_id = group_id)
  1392. if group_sort:
  1393. Q_word = Q_word & Q(group_sort = group_sort)
  1394. if category:
  1395. for tmp_word in category.split(",") :
  1396. Q_word = Q_word | Q(category__icontains=tmp_word)
  1397. if location_id :
  1398. Q_word = Q_word & Q(school_id = location_id)
  1399. if recommend :
  1400. Q_word = Q_word & Q(recommend = recommend)
  1401. if is_inner!=None:
  1402. Q_word = Q_word & Q(is_inner = is_inner)
  1403. if is_check!=None:
  1404. Q_word = Q_word & Q(is_check = is_check)
  1405. class_name_id = []
  1406. for class_list in await Class_list.filter(Q(lecturer__icontains=keyword)):
  1407. class_name_id.append(class_list.name_id)
  1408. for class_list in await One_day_class.filter(Q(teacher__icontains=keyword)):
  1409. class_name_id.append(class_list.name_id)
  1410. if keyword :
  1411. Q_word = Q_word & ( Q(name__icontains=keyword)|
  1412. Q(category__icontains=keyword)|
  1413. Q(introduction__icontains=keyword)|
  1414. Q(organizer__icontains=keyword)|
  1415. Q(group_sort__icontains=keyword) |
  1416. Q(id__in=class_name_id)|
  1417. Q(syllabus__icontains=keyword))
  1418. class_name_list = Class_name.filter(Q_word).all()
  1419. count = await class_name_list.all().count()
  1420. if page_num and page_amount:
  1421. class_name_list = class_name_list.offset((page_num-1)*page_amount).limit(page_amount)
  1422. if exclude_word:
  1423. for tmp_word in exclude_word.split(",") :
  1424. # print(tmp_word)
  1425. class_name_list = class_name_list.exclude(Q(category__icontains=tmp_word)|Q(group_sort__icontains=tmp_word)).all()
  1426. class_name_list = await class_name_list.all().order_by("-id")
  1427. classes_name = []
  1428. state = {}
  1429. special_class_list_name = None
  1430. for class_name_obj in class_name_list:
  1431. try:
  1432. school_obj = await Schools.get(id=class_name_obj.school_id)
  1433. school_obj_id = school_obj.id
  1434. except:
  1435. school_obj_id = 0
  1436. try:
  1437. special_class_list_name = class_name_obj.special_class_list_name
  1438. if special_class_list_name:
  1439. state = await check_date_state(class_name_id=class_name_obj.id,special_class_list_name=special_class_list_name)
  1440. else:
  1441. state = await check_date_state(class_name_id=class_name_obj.id)
  1442. class_data = class_name_obj.show_data()
  1443. if school_obj_id:
  1444. for key, item in school_obj.show_data().items():
  1445. class_data[key] = item
  1446. class_data["state"]=state["result"]["state"]
  1447. except Exception as e:
  1448. class_data = {
  1449. "msg" : str(e)
  1450. }
  1451. classes_name.append(class_data)
  1452. return {"msg": "success", "code": 200, "total_num" : count,"classes": classes_name}
  1453. except Exception as e:
  1454. return {"msg": str(e), "code": 500}
  1455. @classes.post("/add_favorite_class")
  1456. async def add_favorite_class(
  1457. class_name_id: int,
  1458. user_id = Depends(check_token),
  1459. time_stemp: datetime = datetime.now()
  1460. ):
  1461. try:
  1462. if not user_id:
  1463. return {"msg": "no access", "code": 500}
  1464. new_favorite_class = await Favorite_course.get_or_create(
  1465. class_name_id=class_name_id,
  1466. user_id=user_id,
  1467. defaults={'time_stemp': time_stemp}
  1468. )
  1469. return {"msg": "success", "code": 200,"is exist": not new_favorite_class[1],"id":new_favorite_class[0].id}
  1470. except Exception as e:
  1471. return {"msg": str(e), "code": 500}
  1472. @classes.get("/get_favorite_class")
  1473. async def get_favorite_class(
  1474. user_id = Depends(check_token),
  1475. no_details : Optional[int] = None
  1476. ):
  1477. try:
  1478. class_list = await Favorite_course.filter(user_id = user_id).all()
  1479. favorite_courses = []
  1480. for class_obj in class_list:
  1481. if no_details:
  1482. class_data = {
  1483. "id": class_obj.id,
  1484. "user_id": class_obj.user_id,
  1485. "class_name_id" : class_obj.class_name_id,
  1486. "time_stemp":class_obj.time_stemp
  1487. }
  1488. favorite_courses.append(class_data)
  1489. else:
  1490. class_data = {
  1491. "id": class_obj.id,
  1492. "user_id": class_obj.user_id,
  1493. "class_name_id" : class_obj.class_name_id,
  1494. "time_stemp":class_obj.time_stemp
  1495. }
  1496. result = await get_class_name(class_name_id = class_obj.class_name_id)
  1497. try:
  1498. class_detail = result["classes"][0]
  1499. for key,item in class_detail.items():
  1500. class_data[key] = item
  1501. favorite_courses.append(class_data)
  1502. except:
  1503. class_data["msg"] = "this class doesn't exit"
  1504. favorite_courses.append(class_data)
  1505. return {"msg": "success", "code": 200, "favorite_courses": favorite_courses}
  1506. except Exception as e:
  1507. return {"msg": str(e), "code": 500}
  1508. @classes.post("/delete_favorite_class")
  1509. async def delete_favorite_class(
  1510. class_name_id: int,
  1511. user_id = Depends(check_token)
  1512. ):
  1513. try:
  1514. await Favorite_course.filter(class_name_id=class_name_id,user_id=user_id).delete()
  1515. return {"msg": "success", "code": 200}
  1516. except Exception as e:
  1517. return {"msg": str(e), "code": 500}
  1518. @classes.post("/insert_online_course")
  1519. async def insert_online_course(
  1520. title : str = Form(default=''),
  1521. category : str = Form(default=''),
  1522. create_time :str = Form(default=datetime.now()),
  1523. content : str = Form(default=''),
  1524. video_url :str = Form(default='')
  1525. ):
  1526. try:
  1527. new_online_course = await Online_course.create(
  1528. title=title,
  1529. create_time=create_time,
  1530. category=category,
  1531. content=content,
  1532. video_url=video_url,
  1533. group_id = 8
  1534. )
  1535. return {"msg": "success", "code": 200, "online_course_obj": new_online_course.id}
  1536. except Exception as e:
  1537. return {"msg": str(e), "code": 500}
  1538. @classes.post("/update_online_course")
  1539. async def update_online_course(
  1540. id : int = Form(default=0),
  1541. title : str = Form(default=''),
  1542. category : str = Form(default=''),
  1543. create_time :str = Form(default=datetime.now()),
  1544. content : str = Form(default=''),
  1545. video_url :str = Form(default='')
  1546. ):
  1547. try:
  1548. online_course_obj = await Online_course.get(id=id)
  1549. if title.strip() != '':
  1550. online_course_obj.title = title
  1551. if category.strip() != '':
  1552. online_course_obj.category = category
  1553. if create_time.strip() != '':
  1554. online_course_obj.create_time = create_time
  1555. if content.strip() != '':
  1556. online_course_obj.content = content
  1557. if video_url.strip() != '':
  1558. online_course_obj.video_url = video_url
  1559. await online_course_obj.save()
  1560. return {"msg": "success", "code": 200}
  1561. except Exception as e:
  1562. return {"msg": str(e), "code": 500}
  1563. @classes.get("/get_online_courese")
  1564. async def get_online_courese(
  1565. online_courese_id : Optional[int] = None,
  1566. category:Optional[str] = None,
  1567. group_id : Optional[int] = None,
  1568. page_num : Optional[int] = None,
  1569. page_amount : Optional[int] = None,
  1570. org :Optional[str] = None,
  1571. no_org:Optional[str] = None,
  1572. key_word:Optional[str] = None,
  1573. ):
  1574. try:
  1575. online_courese_list = Online_course.all()
  1576. Q_word = Q()
  1577. if group_id:
  1578. Q_word = Q_word & Q(group_id = group_id)
  1579. if online_courese_id :
  1580. Q_word = Q_word & Q(id = online_courese_id)
  1581. if org :
  1582. Q_word = Q_word & Q(org = org)
  1583. if no_org:
  1584. no_org_condition = Q(org__isnull=True) | ~Q(org=no_org)
  1585. Q_word = Q_word & no_org_condition
  1586. if category:
  1587. category_conditions = Q()
  1588. for tmp_word in category.split(",") :
  1589. category_conditions |= Q(category__icontains=tmp_word)
  1590. Q_word = Q_word & category_conditions
  1591. #Q_word = Q_word | Q(category__icontains=tmp_word)
  1592. if key_word:
  1593. key_word_condition = Q(title__icontains=key_word) | Q(category__icontains=key_word) | Q(content__icontains=key_word)
  1594. Q_word = Q_word & key_word_condition
  1595. count = await online_courese_list.all().filter(Q_word).count()
  1596. if page_num and page_amount:
  1597. online_courese_list = online_courese_list.offset((page_num-1)*page_amount).limit(page_amount)
  1598. online_courese_list = await online_courese_list.all().filter(Q_word).order_by("id")
  1599. online_coureses = []
  1600. for online_coures_obj in online_courese_list:
  1601. online_coures_data = online_coures_obj.show_data()
  1602. online_coureses.append(online_coures_data)
  1603. return {"msg": "success", "code": 200, "total_num" : count,"online_coures": online_coureses}
  1604. except Exception as e:
  1605. return {"msg": str(e), "code": 500}
  1606. @classes.post("/delete_online_course")
  1607. async def delete_online_course(
  1608. online_course_id : int
  1609. ):
  1610. try:
  1611. await Online_course.filter(id=online_course_id).delete()
  1612. return {"msg": "success", "code": 200}
  1613. except Exception as e:
  1614. return {"msg": str(e), "code": 500}
  1615. @classes.get("/get_group_classes_and_articles")
  1616. async def get_group_classes_and_articles(
  1617. group_id : int,
  1618. page_num : Optional[int] = None,
  1619. page_amount : Optional[int] = None
  1620. ):
  1621. try:
  1622. class_name_list = Class_name.filter(group_id = group_id,is_delete=0).all().order_by("-id")
  1623. article_list = Article_list.filter(group_id = group_id,is_del=0).all().order_by("-id")
  1624. class_count = await class_name_list.all().count()
  1625. article_count = await article_list.all().count()
  1626. if page_num and page_amount:
  1627. class_name_list = class_name_list.offset((page_num-1)*page_amount).limit(page_amount)
  1628. article_list = article_list.offset((page_num-1)*page_amount).limit(page_amount)
  1629. class_name_list = await class_name_list.all()
  1630. article_list = await article_list.all()
  1631. article_objs = []
  1632. for article_obj in article_list:
  1633. try :
  1634. article_tmp = {
  1635. "article_id": article_obj.id,
  1636. "title": article_obj.title,
  1637. "school_id" :article_obj.school_id,
  1638. "group_sort" :article_obj.group_sort,
  1639. "group_id" :article_obj.group_id,
  1640. "category": article_obj.category,
  1641. "create_time" : article_obj.create_time,
  1642. "click_time" : article_obj.click_time,
  1643. "depiction" : article_obj.depiction,
  1644. "content" : article_obj.content,
  1645. "files" : article_obj.files,
  1646. "vedio_url" : article_obj.vedio_url,
  1647. "tags" : article_obj.tags,
  1648. "cover_img": article_obj.cover_img
  1649. }
  1650. except:
  1651. article_tmp = {
  1652. "msg" : "fail to get data"
  1653. }
  1654. article_objs.append(article_tmp)
  1655. classes_name = []
  1656. for class_name_obj in class_name_list:
  1657. try:
  1658. school_obj = await Schools.get(id=class_name_obj.school_id)
  1659. school_obj_id = school_obj.id
  1660. except:
  1661. school_obj_id = 0
  1662. try:
  1663. special_class_list_name = class_name_obj.special_class_list_name
  1664. if special_class_list_name:
  1665. state = await check_date_state(class_name_id=class_name_obj.id,special_class_list_name=special_class_list_name)
  1666. else:
  1667. state = await check_date_state(class_name_id=class_name_obj.id)
  1668. class_data = class_name_obj.show_data()
  1669. if school_obj_id:
  1670. for key, item in school_obj.show_data().items():
  1671. class_data[key] = item
  1672. class_data["state"]=state["result"]["state"]
  1673. except Exception as e:
  1674. class_data = {
  1675. "msg" : str(e)
  1676. }
  1677. classes_name.append(class_data)
  1678. return {"msg": "success", "code": 200,"class_num" : class_count,"classes": classes_name,"article_num":article_count,"articles": article_objs}
  1679. except Exception as e:
  1680. return {"msg": str(e), "code": 500}
  1681. @classes.post("/add_attend_record_by_event")
  1682. async def add_attend_record(
  1683. class_id : int = Form(default=0),
  1684. ):
  1685. try:
  1686. if not class_id:
  1687. return {"msg": "Please input right ","code":500}
  1688. try:
  1689. class_detail_obj = await Class_detail.filter(class_list_id = class_id).all()
  1690. if not isinstance(class_detail_obj, list):
  1691. class_detail_obj = [class_detail_obj]
  1692. registration_obj = await Registration.filter(event_id = class_id,is_del = 0,).all()
  1693. if not isinstance(registration_obj, list):
  1694. registration_obj = [registration_obj]
  1695. if not class_detail_obj:
  1696. return {"msg": "no this class_detail_id session","code":500}
  1697. except:
  1698. return {"msg": "get class_detail_id error","code":500}
  1699. for obj in class_detail_obj:
  1700. class_event = await Class_list.get_or_none(id = class_id)
  1701. if class_event is None:
  1702. return {"msg": "no this class","code":500}
  1703. for obj1 in registration_obj:
  1704. new_record = await Attend_record.get_or_create(
  1705. class_detail_id = obj.id,
  1706. user_id = obj1.user_id,
  1707. defaults={'class_name_id':class_event.name_id,'class_event_id':class_id,'is_attend': 0}
  1708. )
  1709. return {"msg": "success", "code": 200}
  1710. except Exception as e:
  1711. return {"msg": str(e), "code": 500}
  1712. @classes.post("/add_attend_record")
  1713. async def add_attend_record(
  1714. class_detail_id : int = Form(default=0),
  1715. user_id : int = Form(default=0),
  1716. is_attend : int = Form(default=0)
  1717. ):
  1718. try:
  1719. if not class_detail_id or not user_id:
  1720. return {"msg": "Please input right ","code":500}
  1721. try:
  1722. class_detail_obj = await Class_detail.get_or_none(id = class_detail_id)
  1723. class_list = await Class_list.get_or_none(id = class_detail_obj.class_list_id)
  1724. if not class_detail_obj or not class_list:
  1725. return {"msg": "no this class_detail_id","code":500}
  1726. except:
  1727. return {"msg": "get class_detail_id error","code":500}
  1728. msg = ""
  1729. new_record,created = await Attend_record.get_or_create(
  1730. class_detail_id = class_detail_id,
  1731. user_id = user_id,
  1732. defaults = {
  1733. "class_name_id" : class_list.name_id,
  1734. "class_list_id" : class_detail_obj.class_list_id,
  1735. "is_attend" : is_attend
  1736. }
  1737. )
  1738. if not created:
  1739. try:
  1740. new_record.is_attend = is_attend
  1741. await new_record.save()
  1742. msg = "update success"
  1743. except:
  1744. msg = "update fail"
  1745. else:
  1746. msg = "created success"
  1747. return {"msg": msg, "code": 200,"new_record_id":new_record.id}
  1748. except Exception as e:
  1749. return {"msg": str(e), "code": 500}
  1750. @classes.post("/close_class")
  1751. async def close_class(user_id:int = Depends(check_token),class_name_id : int = None,cancel:int=None):
  1752. try:
  1753. if user_id :
  1754. user = await User.get(id=user_id)
  1755. class_name = await Class_name.get_or_none(id = class_name_id)
  1756. if not class_name :
  1757. return {"msg": "no this class", "code": 500}
  1758. elif class_name.is_record_point == 1:
  1759. if cancel==1:
  1760. user_attend_record = await Attend_record.filter(class_name_id=class_name_id,is_attend = 1)
  1761. for tmp in user_attend_record:
  1762. point = await User_point.get_or_none(user_id = tmp.user_id)
  1763. class_detail = await Class_detail.get_or_none(id = tmp.class_detail_id)
  1764. point.hours -= float(class_detail.hour)
  1765. await point.save()
  1766. class_name.is_record_point = 0
  1767. await class_name.save()
  1768. return {"msg": "class closed has cancel", "code": 500}
  1769. else:
  1770. return {"msg": "class has closed", "code": 500}
  1771. else :
  1772. if class_name.create_user_id:
  1773. if class_name.create_user_id != user_id:
  1774. print(class_name.create_user_id,user_id)
  1775. return {"msg": "no access", "code": 200}
  1776. user_attend_record = await Attend_record.filter(class_name_id=class_name_id,is_attend = 1)
  1777. for tmp in user_attend_record:
  1778. point,created = await User_point.get_or_create(
  1779. user_id = tmp.user_id,
  1780. defaults = {
  1781. "hours" : 0.0,
  1782. "points" : 0.0
  1783. }
  1784. )
  1785. class_detail = await Class_detail.get_or_none(id = tmp.class_detail_id)
  1786. point.hours += float(class_detail.hour)
  1787. await point.save()
  1788. result = await count_point(tmp.user_id)
  1789. class_name.is_record_point = 1
  1790. await class_name.save()
  1791. return {"msg":"success close class and record point", "code": 200}
  1792. except Exception as e:
  1793. return {"msg": str(e), "code": 500}
  1794. async def count_point(user_id) :
  1795. point = await User_point.get_or_none(user_id = user_id)
  1796. records = await Point_exchange_record.filter(user_id = user_id)
  1797. if point:
  1798. point.points = int(point.hours/3)
  1799. for record in records:
  1800. point.points -= record.point_exchange
  1801. await point.save()
  1802. return {"msg":"success", "code": 200}
  1803. else:
  1804. return {"msg":"沒有點數資料", "code": 500}
  1805. @classes.post("/delete_attend_record")
  1806. async def delete_attend_record(
  1807. id : int = 0
  1808. ):
  1809. try :
  1810. record = await Attend_record.get(id=id)
  1811. await Attend_record.filter(id=id).delete()
  1812. return {"msg": "success", "code": 200}
  1813. except Exception as e:
  1814. return {"msg": str(e), "code": 500}
  1815. @classes.get("/update_attend_record")
  1816. async def update_attend_record(
  1817. id : int = 0,
  1818. class_detail_id : Optional[int] = None,
  1819. user_id : Optional[int] = None,
  1820. is_attend : Optional[int] = None
  1821. ):
  1822. if not id :
  1823. return {"msg":"please input id"}
  1824. try :
  1825. tmp = await Attend_record.get(id=id)
  1826. if class_detail_id!=None:
  1827. tmp.class_detail_id = class_detail_id
  1828. if user_id!=None:
  1829. tmp.user_id = user_id
  1830. if is_attend!=None:
  1831. tmp.is_attend = is_attend
  1832. await tmp.save()
  1833. return {"msg": "success", "code": 200}
  1834. except Exception as e:
  1835. return {"msg": str(e), "code": 500}
  1836. @classes.get("/get_attend_record")
  1837. async def get_attend_record(
  1838. class_event_id : Optional[int] = None,
  1839. class_detail_id : Optional[int] = None,
  1840. user_id : Optional[int] = None,
  1841. is_attend : Optional[int] = None,
  1842. page_num : Optional[int] = None,
  1843. page_amount : Optional[int] = None
  1844. ):
  1845. try:
  1846. attend_record_list = Attend_record.all()
  1847. if class_event_id:
  1848. id_list = []
  1849. class_detail_list = await Class_detail.filter(class_list_id = class_event_id)
  1850. for tmp in class_detail_list :
  1851. id_list.append(tmp.id)
  1852. attend_record_list = Attend_record.filter(class_detail_id__in=id_list)
  1853. if class_detail_id:
  1854. attend_record_list = attend_record_list.filter(class_detail_id = class_detail_id)
  1855. if user_id :
  1856. attend_record_list = attend_record_list.filter(user_id = user_id)
  1857. if is_attend :
  1858. attend_record_list = attend_record_list.filter(is_attend = is_attend)
  1859. count = await attend_record_list.all().count()
  1860. if page_num and page_amount:
  1861. attend_record_list = attend_record_list.offset((page_num-1)*page_amount).limit(page_amount)
  1862. attend_record_list = await attend_record_list.all()
  1863. attend_records = []
  1864. for obj in attend_record_list:
  1865. class_detail_list = await Class_detail.get_or_none(id = obj.class_detail_id)
  1866. if class_detail_list == None:
  1867. return {"msg": f"class detail number error id={obj.class_detail_id}", "code": 500}
  1868. #registration_list = await Registration.filter(event_id = class_detail_list[0].class_list_id,user_id =obj.user_id)
  1869. user_list = await User.filter(id = obj.user_id)
  1870. user_information_list = await User_information.filter(user_id = obj.user_id)
  1871. class_list = await Class_list.get_or_none(id = obj.class_event_id)
  1872. if class_list != None :
  1873. class_name = await Class_name.get_or_none(id = class_list.name_id)
  1874. #if len(registration_list)==0:
  1875. # continue
  1876. data = {
  1877. "id" :obj.id,
  1878. "class_name_id" : obj.class_name_id,
  1879. "class_event_id":class_detail_list.class_list_id,
  1880. "class_detail_id" :obj.class_detail_id,
  1881. "user_id" :obj.user_id,
  1882. "is_attend" :obj.is_attend,
  1883. #"reg_confirm" : registration_list[0].reg_confirm,
  1884. #"payment_status": registration_list[0].payment_status,
  1885. #"five_digits": registration_list[0].five_digits,
  1886. "real_name": user_information_list[0].name,
  1887. "phone": user_information_list[0].phone,
  1888. "email": user_list[0].email
  1889. }
  1890. if class_list and class_name :
  1891. data["class_name"] = class_name.name
  1892. data["start_time"] = class_detail_list.start_time
  1893. data["end_time"]= class_detail_list.end_time
  1894. attend_records.append(data)
  1895. return {"msg": "success", "code": 200,"total_num" : count,"attend_record_list":attend_records}
  1896. except Exception as e:
  1897. return {"msg": str(e), "code": 500}
  1898. import pymysql
  1899. @classes.get("/copy")
  1900. async def copy(
  1901. from_id : Optional[int] = None
  1902. ):
  1903. try:
  1904. connection = pymysql.connect(
  1905. host='db.ptt.cx',
  1906. user='choozmo',
  1907. password='pAssw0rd',
  1908. database='test_copy'
  1909. )
  1910. class_list_ip = []
  1911. with connection.cursor() as cursor:
  1912. sql = "SELECT * FROM class_name"
  1913. cursor.execute(sql)
  1914. connection.commit()
  1915. rows = cursor.fetchall()
  1916. encode = "HOP"
  1917. now = datetime.now()
  1918. encode += now.strftime("%Y%m%d")
  1919. class_id_list = []
  1920. for row in rows:
  1921. # print(row)
  1922. class_name,created = await Class_name.update_or_create(
  1923. name = row[1],
  1924. defaults = {
  1925. "school_id":1,
  1926. "category" : row[3],
  1927. "introduction" : row[4],
  1928. "organizer" : row[5],
  1929. "cover_img" : row[6],
  1930. "group_id" :9,
  1931. "group_sort" : "希望工程",
  1932. "special_class_list_name" :None,
  1933. "recommend" : 0,
  1934. "is_inner" :1,
  1935. "is_check" :1,
  1936. "create_user_id" :None,
  1937. "create_time" : datetime.now(),
  1938. "is_delete" : row[10],
  1939. "is_record_point" :0,
  1940. "encode" :encode
  1941. }
  1942. )
  1943. #print(class_name.id)
  1944. sql = f"SELECT * FROM schools where id = {row[2]}"
  1945. cursor.execute(sql)
  1946. connection.commit()
  1947. school_row = cursor.fetchone()
  1948. # print(school_row)
  1949. try:
  1950. school,school_created = await Schools.update_or_create(
  1951. name = school_row[1],
  1952. defaults = {
  1953. "longitude" : school_row[2],
  1954. "latitude" : school_row[3],
  1955. "address" :school_row[4],
  1956. "update_time" :school_row[5],
  1957. "school_introduction":None,
  1958. "email":None,
  1959. "phone":None,
  1960. "school_create_user_id" :None,
  1961. "teachers" : None,
  1962. "is_delete" : 0,
  1963. "is_pass_proposal" : 1
  1964. }
  1965. )
  1966. class_name.school_id = school.id
  1967. await class_name.save()
  1968. #print("step 1 complete")
  1969. sql = f"SELECT * FROM class_list where name_id = {row[0]}"
  1970. cursor.execute(sql)
  1971. connection.commit()
  1972. list_row = cursor.fetchone()
  1973. # print(list_row)
  1974. event,event_created = await Class_list.update_or_create(
  1975. name_id = class_name.id,
  1976. defaults = {
  1977. "event": list_row[2],
  1978. "start_time": list_row[3],
  1979. "end_time": list_row[4],
  1980. "location": list_row[10],
  1981. "lecturer": list_row[9],
  1982. "contact": list_row[8],
  1983. "content": list_row[11],
  1984. "URL": list_row[12],
  1985. "people": None,
  1986. "fee_method": list_row[19],
  1987. "registration_way": list_row[15],
  1988. "remark": list_row[16],
  1989. "ATM_address":list_row[17],
  1990. "fee_payment" :list_row[18],
  1991. "create_user_id" :list_row[19],
  1992. "event_create_time" : datetime.now(),
  1993. "files" : list_row[20]
  1994. }
  1995. )
  1996. #print("step 2 complete")
  1997. date,date_create = await Class_date.update_or_create(
  1998. class_list_id = event.id,
  1999. defaults = {
  2000. "registration_start": list_row[5],
  2001. "registration_end": list_row[6],
  2002. "number_limit": list_row[13],
  2003. "amount_left": 0
  2004. }
  2005. )
  2006. #print("step 3 complete")
  2007. class_id_list.append(class_name.id)
  2008. except :
  2009. await Class_name.filter(id = class_name.id).delete()
  2010. my_log("error",__name__,f"{row[0]}copy error")
  2011. class_delete_list = await Class_name.filter(group_id = 9,group_sort = "希望工程").exclude(id__in = class_id_list).delete()
  2012. for class_ in class_delete_list :
  2013. print("class_delete:",class_.id)
  2014. cursor.close()
  2015. connection.close()
  2016. return {"msg": "success", "code": 500}
  2017. except Exception as e:
  2018. return {"msg": str(e), "code": 500}
  2019. @classes.post("/insert_proposal")
  2020. async def insert_class_name(
  2021. class_name: str = Form(default=None),
  2022. school_id : int = Form(default=None),
  2023. category : str = Form(default=None),
  2024. introduction: str = Form(default=None),
  2025. organizer: str = Form(default=None),
  2026. cover_img_file : UploadFile = File(default=None),
  2027. fee_method: str = Form(default=None),
  2028. number_limit: int = Form(default=None),
  2029. number_minimum : int = Form(default=None),
  2030. people : str = Form(default=None),
  2031. files = Depends(create_upload_files)
  2032. ):
  2033. try:
  2034. cover_img = ''
  2035. if cover_img_file != None:
  2036. contents = await cover_img_file.read()
  2037. #save the file
  2038. with open(f"{IMAGEDIR}{cover_img_file.filename}", "wb") as f:
  2039. f.write(contents)
  2040. cover_img = f"{IMAGEDIR_short}{cover_img_file.filename}"
  2041. if files:
  2042. if files.__contains__("msg"):
  2043. return {"msg": files["msg"], "code": 500}
  2044. school = await Schools.get_or_none(id = school_id)
  2045. if school is None :
  2046. return {"msg": f"no this school id {school_id}", "code": 500}
  2047. new_proposal,created = await Proposal.get_or_create(
  2048. school_id =school_id,
  2049. defaults = {
  2050. "class_name":class_name,
  2051. "category":category,
  2052. "introduction":introduction,
  2053. "organizer":organizer,
  2054. "cover_img":cover_img,
  2055. "fee_method":fee_method,
  2056. "number_limit":number_limit,
  2057. "number_minimum":number_minimum,
  2058. "people":people,
  2059. "files":files,
  2060. "create_time":datetime.now()
  2061. }
  2062. )
  2063. if not created:
  2064. if class_name:
  2065. new_proposal.class_name = class_name
  2066. if category:
  2067. new_proposal.category = category
  2068. if introduction:
  2069. new_proposal.introduction = introduction
  2070. if cover_img_file:
  2071. new_proposal.cover_img = cover_img
  2072. if fee_method:
  2073. new_proposal.fee_method = fee_method
  2074. if number_limit:
  2075. new_proposal.number_limit = number_limit
  2076. if number_minimum:
  2077. new_proposal.number_minimum = number_minimum
  2078. if people:
  2079. new_proposal.people = people
  2080. if files != "{}":
  2081. new_proposal.files = files
  2082. await new_proposal.save()
  2083. else:
  2084. user_id = school.create_user_id
  2085. if user_id:
  2086. try:
  2087. try :
  2088. user = await User.get(id = user_id)
  2089. user_info = await User_information.get(user_id = user_id)
  2090. except:
  2091. return {"msg": "請去建立工藝家履歷", "code": 500}
  2092. email = user.email
  2093. subject = "課程提案通知"
  2094. # message = f"親愛的工藝家{user_info.name}您好,<br>\
  2095. # 感謝您埋下工藝種子!<br>\
  2096. # 您的提案將由管理員進行審核中,將於3-7個工作天內e-mail通知您審核結果。<br>\
  2097. # 若7天後尚未收到課程審核通知,請將後台畫面、聯絡資訊等截圖寄送至客服信箱:<br>\
  2098. # craftology@ntcri.gov.tw,以便客服為您查詢。<br>\
  2099. # 註:此封信件為系統自動發送,請勿回信,謝謝。"
  2100. with open("/var/www/ntcri/assets/edm/porposal/index.html", 'r', encoding='utf-8') as html_file:
  2101. html_template = html_file.read()
  2102. message = html_template.replace("{username}",user_info.name)
  2103. send_email(email,"",subject,message)
  2104. except:
  2105. print("have no email")
  2106. return {"msg": "success", "code": 200, "new_proposal_id": new_proposal.id}
  2107. except Exception as e:
  2108. return {"msg": str(e), "code": 500}
  2109. @classes.get("/get_proposal")
  2110. async def insert_class_name(
  2111. id : Optional[int] = None,
  2112. school_id : Optional[int] = None
  2113. ):
  2114. try:
  2115. proposal_list = Proposal.all()
  2116. Q_word = Q()
  2117. if id :
  2118. Q_word = Q(id=id)
  2119. if school_id :
  2120. Q_word = Q_word & Q(school_id=school_id)
  2121. proposal_list = await proposal_list.filter(Q_word)
  2122. result = []
  2123. for proposal in proposal_list:
  2124. school = await Schools.get_or_none(id=proposal.school_id)
  2125. if school is None :
  2126. return {"msg": f"no this school,id = {proposal.school_id}", "code": 500}
  2127. info = proposal.show_data()
  2128. info["school"]=school.show_data()
  2129. result.append(info)
  2130. return {"msg": "success", "code": 200, "proposal_list": result}
  2131. except Exception as e:
  2132. return {"msg": str(e), "code": 500}
  2133. @classes.get("/get_favorite_course_count")
  2134. async def insert_class_name(
  2135. class_name_id_list : str = None
  2136. ):
  2137. try:
  2138. result = []
  2139. if class_name_id_list :
  2140. class_name_id = eval(class_name_id_list)
  2141. distinct_class_name_ids = list(map(int,class_name_id))
  2142. for entry in distinct_class_name_ids:
  2143. class_num = await Favorite_course.filter(class_name_id = entry).count()
  2144. result.append({"class_name_id":entry,"count":class_num})
  2145. else:
  2146. # 使用 Tortoise ORM 的 count 方法進行統計
  2147. distinct_class_name_ids = await Favorite_course.all().values_list('class_name_id')
  2148. # print(distinct_class_name_ids)
  2149. for entry in distinct_class_name_ids:
  2150. class_num = await Favorite_course.filter(class_name_id = entry[0]).count()
  2151. result.append({"class_name_id":entry[0],"count":class_num})
  2152. return {"msg": "success", "code": 200, "result": result}
  2153. except Exception as e:
  2154. return {"msg": str(e), "code": 500}
  2155. @classes.get("/get_school_city_count")
  2156. async def insert_class_name(
  2157. school_city_list : str = None
  2158. ):
  2159. try:
  2160. result = []
  2161. distinct_class_name_ids = []
  2162. if school_city_list :
  2163. distinct_class_name_ids = eval(school_city_list)
  2164. else:
  2165. # 使用 Tortoise ORM 的 count 方法進行統計
  2166. distinct_class_name_ids = taiwan_city
  2167. # print(distinct_class_name_ids)
  2168. for entry in distinct_class_name_ids:
  2169. count = 0
  2170. if "," in entry:
  2171. for tmp in entry.split(","):
  2172. count += await Schools.filter(address__icontains=tmp).count()
  2173. else:
  2174. count = await Schools.filter(address__icontains=entry).count()
  2175. result.append({"city":entry,"count":count})
  2176. return {"msg": "success", "code": 200, "result": result}
  2177. except Exception as e:
  2178. return {"msg": str(e), "code": 500}