Ver Fonte

出席紀錄、學校搜尋功能、news搜尋功能

Mia Cheng há 1 ano atrás
pai
commit
398830b84b
4 ficheiros alterados com 226 adições e 61 exclusões
  1. 5 1
      app/api/article.py
  2. 193 54
      app/api/classes.py
  3. 21 5
      app/api/news.py
  4. 7 1
      app/models/models.py

+ 5 - 1
app/api/article.py

@@ -235,7 +235,11 @@ async def update_article(
 @article.post("/delete_article")
 async def delete_article(id: int = 0):
     if id != 0:
-        await Article_list.filter(id=id).delete()
+        article_obj = await Article_list.get(id=id)
+
+        article_obj.is_del = 1
+
+        await article_obj.save()
         return {"msg": "success", "code": 200}
     else :
         return {"msg": "please input ID", "code": 200}

+ 193 - 54
app/api/classes.py

@@ -3,7 +3,7 @@ from typing import List,Optional
 from fastapi.responses import FileResponse
 from random import randint
 from fastapi.security import OAuth2PasswordRequestForm
-from app.models.models import User,Favorite_course,Article_list,Class_date
+from app.models.models import User,Favorite_course,Article_list,Class_date,Attend_record
 from app.models.models import Class_list,Schools,Class_detail,Class_name,Registration,Group_name,Online_course
 from app.api import deps
 from sqlalchemy.orm import Session
@@ -20,6 +20,7 @@ from tortoise.queryset import Q
 from fastapi.responses import HTMLResponse
 import requests
 import json
+from itertools import chain
 
 classes = APIRouter()
 
@@ -583,15 +584,30 @@ async def search_event(
     
 @classes.get("/get_school")
 async def get_school(
-    location_id : Optional[int] = None
+    location_id : Optional[int] = None,
+    keyword : Optional[str] = None, 
+    location_keyword : Optional[str] = None, 
+    page_num : Optional[int] = None,
+    page_amount : Optional[int] = None
 ):
     try:
+        school_list = Schools.all()
+
         if location_id :
-            school_list = await Schools.filter(id = location_id).all().order_by("-update_time")
-        else :
-            school_list = await Schools.all().order_by("-update_time")
+            school_list = school_list.filter(id = location_id).all()
+        
+        if keyword :
+            school_list = school_list.filter(Q(name__icontains=keyword)).all()
+
+        if location_keyword :
+            school_list = school_list.filter(Q(address__icontains=location_keyword)).all()
+        
+        
+        school_list = await school_list.all().order_by("-update_time")
         
         schools = []
+        count = 0
+
         for school_obj in school_list:
             school_data = {
                 "location_id": school_obj.id,
@@ -601,9 +617,17 @@ async def get_school(
                 "address": school_obj.address,
                 "update_time":school_obj.update_time
             }
-            schools.append(school_data)
+            if page_num and page_amount:
+                if count < page_num*page_amount and count >= (page_num-1)*page_amount :
+                    schools.append(school_data)
+                    count += 1
+                else : count += 1
+            else :
+                schools.append(school_data)
+                count += 1
+            
 
-        return {"msg": "success", "code": 200, "schools": schools}
+        return {"msg": "success", "code": 200, "total_num" : count,"schools": schools}
     except Exception as e:
         return {"msg": str(e), "code": 500}
     
@@ -693,6 +717,9 @@ async def get_session(
     event_id : Optional[int] = None
 ):
     try:
+        if not event_id:
+            return {"msg": "please input event_id", "code": 500}
+        
         class_session_list = await Class_detail.filter(class_list_id=event_id).all().order_by("start_time")
         
         classe_sessions = []
@@ -722,62 +749,62 @@ async def search_class_like(keyword: str):
             school_obj = await Schools.get(id=class_name.school_id)
             
             class_data = {
-                "msg": "success",
-            "code": 200,
-            "class_id": class_obj.id,
-            "name": class_name.name,
-            "school":school_obj.name,
-            "category": class_name.category,
-            "introduction": class_name.introduction,
-            "organizer": class_name.organizer,
-            "cover_img": class_name.cover_img,
-            "event": class_obj.event,
-            "start_time": class_obj.start_time,
-            "end_time": class_obj.end_time,
-            "location": class_obj.location,
-            "lecturer": class_obj.lecturer,
-            "contact": class_obj.contact,
-            "content": class_obj.content,
-            "URL": class_obj.URL,
-            "people": class_obj.people,
-            "fee_method": class_obj.fee_method,
-            "registration_way": class_obj.registration_way,
-            "remark": class_obj.remark
-
+                "class_id": class_obj.id,
+                "name": class_name.name,
+                "school":school_obj.name,
+                "category": class_name.category,
+                "introduction": class_name.introduction,
+                "organizer": class_name.organizer,
+                "cover_img": class_name.cover_img,
+                "event": class_obj.event,
+                "start_time": class_obj.start_time,
+                "end_time": class_obj.end_time,
+                "location": class_obj.location,
+                "lecturer": class_obj.lecturer,
+                "contact": class_obj.contact,
+                "content": class_obj.content,
+                "URL": class_obj.URL,
+                "people": class_obj.people,
+                "fee_method": class_obj.fee_method,
+                "registration_way": class_obj.registration_way,
+                "remark": class_obj.remark
             }
             classes.append(class_data)
 
-        class_name_list = await Class_name.filter(Q(name__icontains=keyword)|Q(group_sort__icontains=keyword)).all()
+        class_name_list = await Class_name.filter(
+            Q(name__icontains=keyword)|
+            Q(group_sort__icontains=keyword)|
+            Q(category__icontains=keyword)|
+            Q(organizer__icontains=keyword)).all()
 
         for class_name in class_name_list:
             class_list = await Class_list.filter(name_id=class_name.id).all()
             for class_obj in class_list:
                 school_obj = await Schools.get(id=class_name.school_id)
                 class_data = {
-                "msg": "success",
-            "code": 200,
-            "class_id": class_obj.id,
-            "name": class_name.name,
-            "school":school_obj.name,
-            "category": class_name.category,
-            "introduction": class_name.introduction,
-            "organizer": class_name.organizer,
-            "cover_img": class_name.cover_img,
-            "event": class_obj.event,
-            "start_time": class_obj.start_time,
-            "end_time": class_obj.end_time,
-            "location": class_obj.location,
-            "lecturer": class_obj.lecturer,
-            "contact": class_obj.contact,
-            "content": class_obj.content,
-            "URL": class_obj.URL,
-            "people": class_obj.people,
-            "fee_method": class_obj.fee_method,
-            "registration_way": class_obj.registration_way,
-            "remark": class_obj.remark
+                    "class_id": class_obj.id,
+                    "name": class_name.name,
+                    "school":school_obj.name,
+                    "category": class_name.category,
+                    "introduction": class_name.introduction,
+                    "organizer": class_name.organizer,
+                    "cover_img": class_name.cover_img,
+                    "event": class_obj.event,
+                    "start_time": class_obj.start_time,
+                    "end_time": class_obj.end_time,
+                    "location": class_obj.location,
+                    "lecturer": class_obj.lecturer,
+                    "contact": class_obj.contact,
+                    "content": class_obj.content,
+                    "URL": class_obj.URL,
+                    "people": class_obj.people,
+                    "fee_method": class_obj.fee_method,
+                    "registration_way": class_obj.registration_way,
+                    "remark": class_obj.remark
 
-            }
-                classes.append(class_data)
+                }
+                if class_data not in classes:
+                    classes.append(class_data)
 
         return {"msg": "success", "code": 200, "classes": classes}
     except Exception as e:
@@ -1015,4 +1042,116 @@ async def get_group_classes_and_articles(
         return {"msg": "success", "code": 200,"class_num" : class_count,"classes": classes_name,"article_num":article_count,"articles": article_objs}
     
     except Exception as e:
-        return {"msg": str(e), "code": 500}    
+        return {"msg": str(e), "code": 500}    
+    
+@classes.post("/add_attend_record")
+async def add_attend_record(
+    class_detail_id : int = Form(default=0),
+    user_id : int =  Form(default=0),
+    is_attend : int = Form(default=0)
+):
+    try:
+        if not class_detail_id or not user_id:
+            return {"msg": "Please input right ","code":500}
+        
+        try:
+            class_detail_obj = await Class_detail.filter(id = class_detail_id).all()
+            if not class_detail_obj:
+                return {"msg": "no this class_detail_id","code":500}
+        except:
+            return {"msg": "get class_detail_id error","code":500}
+        
+        new_record = await Attend_record.get_or_create(
+            class_detail_id = class_detail_id,
+            user_id = user_id,
+            defaults = {
+                "is_attend" : is_attend
+            }
+        )
+
+        return {"msg": "success", "code": 200,"new_record_id":new_record[0].id}
+
+    except Exception as e:
+        return {"msg": str(e), "code": 500} 
+    
+@classes.post("/delete_attend_record")
+async def delete_attend_record(
+    id : int = 0
+):
+    try :
+        await Attend_record.filter(id=id).delete()
+        return {"msg": "success", "code": 200}
+    except Exception as e:
+        return {"msg": str(e), "code": 500} 
+    
+@classes.get("/update_attend_record")
+async def update_attend_record(
+    id : int = 0,
+    class_detail_id : Optional[int] = None,
+    user_id : Optional[int] = None,
+    is_attend : Optional[int] = None
+):
+    if not id :
+        return {"msg":"please input id"}
+    
+    try :
+        tmp = await Attend_record.get(id=id)
+
+        if class_detail_id!=None:
+            tmp.class_detail_id = class_detail_id
+
+        if user_id!=None:
+            tmp.user_id = user_id
+
+        if is_attend!=None:
+            tmp.is_attend = is_attend
+
+        await tmp.save()
+
+        return {"msg": "success", "code": 200}
+    except Exception as e:
+        return {"msg": str(e), "code": 500}   
+    
+    
+@classes.get("/get_attend_record")
+async def get_attend_record(
+    class_event_id : Optional[int] = None,
+    class_detail_id : Optional[int] = None,
+    user_id : Optional[int] = None,
+    is_attend : Optional[int] = None
+):
+    try:
+        attend_record_list = Attend_record.all()
+
+        if class_event_id:
+            id_list = []
+            class_detail_list = await Class_detail.filter(class_list_id = class_event_id)
+            for tmp in class_detail_list :
+                id_list.append(tmp.id)
+            attend_record_list = Attend_record.filter(class_detail_id__in=id_list)
+                
+
+        if class_detail_id:
+            attend_record_list = attend_record_list.filter(class_detail_id = class_detail_id)
+
+        if user_id :
+            attend_record_list = attend_record_list.filter(user_id = user_id)
+
+        if is_attend :
+            attend_record_list = attend_record_list.filter(is_attend = is_attend)
+
+        attend_record_list = await attend_record_list.all()
+        attend_records = []
+
+        for obj in attend_record_list:
+            data = {
+                "id" :obj.id,
+                "class_detail_id" :obj.class_detail_id,
+                "user_id" :obj.user_id,
+                "is_attend" :obj.is_attend
+            }
+            attend_records.append(data)
+
+        return {"msg": "success", "code": 200,"attend_record_list":attend_records}
+    except Exception as e:
+        return {"msg": str(e), "code": 500} 

+ 21 - 5
app/api/news.py

@@ -23,13 +23,21 @@ IMAGEDIR_short = "assets/news_files/"
 
 @news.get("/get_news")
 async def search_news(
-    news_id : int = None
+    news_id : int = None,
+    category :Optional[str] = None,
+    page_num : Optional[int] = None,
+    page_amount : Optional[int] = None
 ):
     try:
+        news_list = News.all()
         if news_id :
-            news_list = await News.filter(id=news_id).all()
-        else:    
-            news_list = await News.all()
+            news_list = news_list.filter(id=news_id).all()
+        
+        if category:
+            news_list = news_list.filter(category=category).all()
+
+        news_list = await news_list.all()
+        count = 0
 
         news_objs = []
         for news_obj in news_list:
@@ -45,9 +53,17 @@ async def search_news(
             "tags" : news_obj.tags,
             "cover_img": news_obj.cover_img
         }
+            if page_num and page_amount:
+                if count < page_num*page_amount and count >= (page_num-1)*page_amount :
+                    news_objs.append(news_tmp)
+                    count += 1
+                else : count += 1
+            else :
+                news_objs.append(news_tmp)
+                count += 1
             news_objs.append(news_tmp)
 
-        return {"msg": "success", "code": 200, "news": news_objs}
+        return {"msg": "success", "code": 200, "total_num" : count,"news": news_objs}
     except Exception as e:
         return {"msg": str(e), "code": 500}
     

+ 7 - 1
app/models/models.py

@@ -175,4 +175,10 @@ class Class_date(Model):
     registration_start = fields.DatetimeField(description="報名時間開始")
     registration_end = fields.DatetimeField(description="報名時間結束")
     number_limit = fields.IntField(description="名額")
-    amount_left = fields.IntField(description="剩餘名額")
+    amount_left = fields.IntField(description="剩餘名額")
+
+class Attend_record(Model):
+    id = fields.IntField(pk=True)
+    class_detail_id = fields.IntField(description="課堂ID")
+    user_id = fields.IntField(description="使用者ID")
+    is_attend = fields.IntField(description="是否已出席(1:是、0:否)")