-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
98 lines (90 loc) · 2.41 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import os
from typing import Optional
from dotenv import load_dotenv
from fastapi import FastAPI, Form
from starlette import templating
from starlette.requests import Request
from starlette.templating import Jinja2Templates
from datamodel import StudentInfo
from database import DatabaseConnect
load_dotenv(verbose=True)
db_host = os.getenv('DB_HOST')
db_port = os.getenv('DB_PORT')
db_name = os.getenv('DB_NAME')
db_user = os.getenv('DB_USER')
db_passwd = os.getenv('DB_PASSWORD')
DatabaseConnect(
host=db_host,
port=int(db_port),
db=db_name,
user=db_user,
passwd=db_passwd)
app = FastAPI()
template = Jinja2Templates(directory="templates")
@app.get("/")
async def render_home(
request: Request
):
try:
departments = DatabaseConnect().get_dept()
faculties = DatabaseConnect().get_faculty()
except Exception as e:
departments = []
faculties = []
error = True
msg = f"系统出现错误,请联系老师。\n{e}"
else:
error = False
msg = ""
finally:
return template.TemplateResponse(
"home.html",
{
"request" : request,
"departments": departments,
"faculties" : faculties,
"error" : error,
"msg" : msg
}
)
@app.post("/register")
async def parse_form(
request : Request,
name : str = Form(...),
id : str = Form(...),
gender : str = Form(...),
phone : str = Form(...),
faculty : str = Form(...),
class_ : str = Form(...),
ideal_dept : str = Form(...),
sub_ideal_dept: str = Form(...),
talent : str = Form(...)
):
register_info = StudentInfo(
name=name,
id=id,
gender=gender,
phone=phone,
faculty=faculty,
class_=class_,
ideal_dept=ideal_dept,
sub_ideal_dept=sub_ideal_dept,
talent=talent
)
try:
DatabaseConnect().add_student_info(register_info)
except Exception as e:
error = True
msg = f"error occured\n{e}"
else:
error = False
msg = "注册成功!"
finally:
return template.TemplateResponse(
"results.html",
{
"request": request,
"error": error,
"msg": msg
}
)