Browse Source

報名流程、update event

Mia Cheng 1 year ago
parent
commit
ae77b28942
3 changed files with 194 additions and 16 deletions
  1. 9 5
      app/api/classes.py
  2. 172 6
      app/api/registration.py
  3. 13 5
      app/models/models.py

+ 9 - 5
app/api/classes.py

@@ -239,21 +239,22 @@ async def update_class_name(
         return {"msg": str(e), "code": 500}
     
 @classes.post("/update_event")
-async def update_class(
+async def update_event(
     id: int = Form(default=0),
-    name_id: str = Form(default=0),
+    name_id: int = Form(default=0),
     event: str = Form(default=''),
     start_time: datetime = Form(default=datetime.now()),
     end_time: datetime = Form(default=datetime.now()),
     contact: str = Form(default=''),
     lecturer: str = Form(default=''),
+    location: str = Form(default=''),
     content: str = Form(default=''),
-    URL: str = Form(default=''), 
+    URL:  str = Form(default=''), 
     people : str = Form(default=''),
     fee_method: str = Form(default=''),
     registration_way: str = Form(default=''),
-    registration_day: str = Form(default=''),
-    remark : str = Form(default='')
+    registration_day:  str = Form(default=''),
+    remark :  str = Form(default='')
 ):
     try:
         class_obj = await Class_list.get(id=id)
@@ -273,6 +274,9 @@ async def update_class(
         if lecturer.strip() != '':
             class_obj.lecturer = lecturer
 
+        if location.strip() != '':
+            class_obj.location = location
+
         if contact.strip() != '':
             class_obj.contact = contact
 

+ 172 - 6
app/api/registration.py

@@ -4,7 +4,7 @@ from typing import List,Optional,Union
 from fastapi.responses import FileResponse
 from random import randint
 from fastapi.security import OAuth2PasswordRequestForm
-from app.models.models import Guidance_group,User
+from app.models.models import Registration,User,User_information,Class_list
 from app.api import deps
 from sqlalchemy.orm import Session
 from typing import Any, Dict
@@ -33,9 +33,175 @@ async def check_token(access_token: str):
 
     return user_id
 
-@registration.get("/protected")
-def protected_route(user_id=Depends(check_token)):
-    if not user_id:
-        return {"message": "no access"}
+async def check_if_id_exeit(
+    table_name : str,
+    id : int      
+):
+    exec('check_list = await {}.filter(id=id).all()'.format(table_name))
+    if check_list == []:
+        return True
+    else:
+        False
 
-    return {'user': user_id}
+async def check_permissions(user_id):
+    user = await User.get(id=user_id)
+    if user.is_superuser:
+        return True
+    else:
+        return False
+
+# @registration.get("/protected")
+# def protected_route(user_id=Depends(check_token)):
+#     if not user_id:
+#         return {"message": "no access"}
+
+#     return {'user': user_id}
+
+@registration.get("/get_registration")
+async def get_registration(
+    user_id = Depends(check_token),
+    get_all : Optional[int] = None,
+    event_id : Optional[int] = None,
+    registration_id : Optional[int] = None,
+    is_check : Optional[int] = None
+):
+    try :
+        if not user_id :
+            return  {"msg": "please log in", "code": 200}
+        
+        if get_all:
+            inform_list = await User_information.all()
+        else:
+            inform_list = await User_information.filter(user_id=user_id).all()
+
+        reg_list = []
+
+        for infor in inform_list:
+            if registration_id:
+                reg_list_tmp = await Registration.filter(id=registration_id,user_inform_id=infor.id,is_del=0).all()
+            else:
+                reg_list_tmp = await Registration.filter(user_inform_id=infor.id,is_del=0).all()
+
+            for reg_obj in reg_list_tmp:
+                reg_data = {
+                    "Registration_id" : reg_obj.id,
+                    "event_id" : reg_obj.event_id,
+                    "user_inform_id" : reg_obj.user_inform_id,
+                    "reg_confirm" : reg_obj.reg_confirm,
+                    "create_time" : reg_obj.create_time
+                }
+                if event_id and reg_data["event_id"] == event_id :
+                    if is_check != None :
+                        if reg_obj.reg_confirm == is_check :
+                            reg_list.append(reg_data)
+                    else :
+                        reg_list.append(reg_data)
+                elif not event_id:
+                    if is_check != None :
+                        if reg_obj.reg_confirm == is_check :
+                            reg_list.append(reg_data)
+                    else :
+                        reg_list.append(reg_data)
+
+        
+        return {"msg": "success", "code": 200,"registrations":reg_list}
+    except Exception as e:
+        return {"msg": str(e), "code": 500}
+
+@registration.post("/input_information")
+async def input_information(
+    user_id = Depends(check_token),
+    name : str = Form(default=''),
+    gender : str = Form(default=''),
+    phone : str = Form(default=''),
+    email : str = Form(default=''),
+    is_default : int = Form(default=0)
+):
+    try :
+        if not user_id :
+            return  {"msg": "no access", "code": 200}
+        
+        if is_default:
+            inform = await User_information.get_or_create(
+                user_id=user_id,
+                is_default = is_default,
+                defaults={
+                    'name': name,
+                    'gender': gender,
+                    'phone': phone,
+                    'email': email
+                }
+            )
+            new_inform = inform[0]
+        else :
+            new_inform = await User_information.create(
+                user_id = user_id,
+                name = name ,
+                gender = gender,
+                phone = phone,
+                email = email,
+                is_default = is_default
+            )
+
+        return {"msg": "success", "code": 200, "user_inform_id": new_inform.id}
+    except Exception as e:
+        return {"msg": str(e), "code": 500}
+
+@registration.post("/input_registration")
+async def input_registration(
+    user_id = Depends(check_token),
+    user_inform_id : int= Form(default=0),
+    event_id : int = Form(default=0)
+): 
+    try :
+        if not user_id :
+            return  {"msg": "please log in", "code": 200}
+
+        # if check_if_id_exeit(User_information,user_inform_id):
+        #     return {"msg": "no user information", "code": 200}
+        
+        # if check_if_id_exeit(Class_list,event_id):
+        #     return {"msg": "no class list", "code": 200}
+        
+        new_registration = await Registration.create(
+            event_id = event_id,
+            user_inform_id = user_inform_id,
+            reg_confirm = 0,
+            is_del = 0 ,
+            create_time = datetime.now()
+        )
+
+        return {"msg": "success", "code": 200,"new_registration_id":new_registration.id}
+    except Exception as e:
+        return {"msg": str(e), "code": 500}
+    
+@registration.post("/confirm_reg")
+async def confirm_reg(
+    #user_id = Depends(check_token),
+    registration_id : int
+):
+    try:
+        registration_obj = await Registration.get(id=registration_id)
+
+        registration_obj.reg_confirm = 1
+
+        await registration_obj.save()
+
+        return {"msg": "success", "code": 200,"registration_id":registration_obj.id}
+    except Exception as e:
+        return {"msg": str(e), "code": 500}
+
+@registration.post("/delete_registration")
+async def delete_registration(
+    registration_id : int 
+):
+    try:
+        registration_obj = await Registration.get(id=registration_id)
+
+        registration_obj.is_del = 1
+
+        await registration_obj.save()
+
+        return {"msg": "success", "code": 200}
+    except Exception as e:
+        return {"msg": str(e), "code": 500}

+ 13 - 5
app/models/models.py

@@ -91,10 +91,10 @@ class Class_detail(Model):
 
 class Registration(Model):
     id = fields.IntField(pk=True)
-    class_id = fields.IntField(description="課程ID")
-    user_id = fields.IntField(description="使用者ID")
-    reg_confirm = fields.CharField(max_length=255, description="報名確認(1:報名成功 0:單純報名)")
-    is_del = fields.CharField(max_length=255, description="是否取消(1:yes ,0:no)")
+    event_id = fields.IntField(description="課程ID")
+    user_inform_id = fields.IntField(description="使用者ID")
+    reg_confirm = fields.IntField(description="報名確認(1:報名成功 0:單純報名)")
+    is_del = fields.IntField(description="是否取消(1:yes ,0:no)")
     create_time = fields.DatetimeField(description="創建時間")
 
 class News(Model):
@@ -157,4 +157,12 @@ class Guidance_group(Model):
     result = fields.CharField(max_length=125, description="處理結果")
     person = fields.CharField(max_length=45, description="輔導人員")
     remark = fields.TextField(description="備註")
-    
+    
+class User_information(Model):
+    id = fields.IntField(pk=True)
+    user_id = fields.IntField(description="使用者ID")
+    name = fields.CharField(max_length=45, description="姓名")
+    gender = fields.CharField(max_length=45, description="性別")
+    phone = fields.CharField(max_length=45, description="電話")
+    email = fields.CharField(max_length=45, description="信箱")
+    is_default = fields.IntField(description="是否為預設")