|
@@ -71,9 +71,11 @@ async def index(request: Request):
|
|
|
print(request)
|
|
|
return templates.TemplateResponse(name='index.html', context={'request': request})
|
|
|
|
|
|
+
|
|
|
@app.get('/login', response_class=HTMLResponse)
|
|
|
async def login(request: Request):
|
|
|
- return templates.TemplateResponse(name='login.html', context={'request': request})
|
|
|
+ return templates.TemplateResponse(name='login_test.html', context={'request': request})
|
|
|
+
|
|
|
|
|
|
@app.post("/login")
|
|
|
async def login_for_access_token(request: Request, form_data: OAuth2PasswordRequestForm = Depends(), Authorize: AuthJWT = Depends()):
|
|
@@ -100,10 +102,11 @@ async def login_for_access_token(request: Request, form_data: OAuth2PasswordRequ
|
|
|
# return templates.TemplateResponse("home.html", {"request": request, "msg": 'Login'})
|
|
|
return {"access_token": access_token, "token_type": "bearer"} # 回傳token給前端
|
|
|
|
|
|
-
|
|
|
+
|
|
|
@app.get('/register', response_class=HTMLResponse)
|
|
|
async def login(request: Request):
|
|
|
- return templates.TemplateResponse(name='register.html', context={'request': request})
|
|
|
+ return templates.TemplateResponse(name='rigister_test.html', context={'request': request})
|
|
|
+
|
|
|
|
|
|
@app.post('/register')
|
|
|
async def register(request: Request, form_data: OAuth2PasswordRequestForm = Depends()):
|
|
@@ -121,10 +124,12 @@ async def register(request: Request, form_data: OAuth2PasswordRequestForm = Depe
|
|
|
# 跳轉頁面至登入
|
|
|
return templates.TemplateResponse(name='login.html', context={'request': request})
|
|
|
|
|
|
+
|
|
|
@app.get('/home', response_class=HTMLResponse)
|
|
|
async def home(request: Request):
|
|
|
return templates.TemplateResponse(name='home.html', context={'request': request})
|
|
|
|
|
|
+
|
|
|
@app.get('/tower', response_class=HTMLResponse)
|
|
|
async def tower(request: Request, Authorize: AuthJWT = Depends()):
|
|
|
try:
|
|
@@ -136,7 +141,6 @@ async def tower(request: Request, Authorize: AuthJWT = Depends()):
|
|
|
return templates.TemplateResponse(name='tower.html', context={'request': request})
|
|
|
|
|
|
|
|
|
-
|
|
|
@app.get('/optim', response_class=HTMLResponse)
|
|
|
async def optim(request: Request, Authorize: AuthJWT = Depends()):
|
|
|
try:
|
|
@@ -147,6 +151,7 @@ async def optim(request: Request, Authorize: AuthJWT = Depends()):
|
|
|
# current_user = Authorize.get_jwt_subject()
|
|
|
return templates.TemplateResponse(name='optim.html', context={'request': request})
|
|
|
|
|
|
+
|
|
|
@app.get('/vibration', response_class=HTMLResponse)
|
|
|
async def vibration(request: Request, Authorize: AuthJWT = Depends()):
|
|
|
try:
|
|
@@ -157,6 +162,7 @@ async def vibration(request: Request, Authorize: AuthJWT = Depends()):
|
|
|
# current_user = Authorize.get_jwt_subject()
|
|
|
return templates.TemplateResponse(name='vibration.html', context={'request': request})
|
|
|
|
|
|
+
|
|
|
@app.get('/history', response_class=HTMLResponse)
|
|
|
async def history(request: Request, Authorize: AuthJWT = Depends()):
|
|
|
try:
|
|
@@ -167,6 +173,7 @@ async def history(request: Request, Authorize: AuthJWT = Depends()):
|
|
|
# current_user = Authorize.get_jwt_subject()
|
|
|
return templates.TemplateResponse(name='history.html', context={'request': request})
|
|
|
|
|
|
+
|
|
|
@app.get('/device', response_class=HTMLResponse)
|
|
|
async def device(request: Request, Authorize: AuthJWT = Depends()):
|
|
|
try:
|
|
@@ -177,6 +184,7 @@ async def device(request: Request, Authorize: AuthJWT = Depends()):
|
|
|
# current_user = Authorize.get_jwt_subject()
|
|
|
return templates.TemplateResponse(name='device.html', context={'request': request})
|
|
|
|
|
|
+
|
|
|
@app.get('/system', response_class=HTMLResponse)
|
|
|
async def system(request: Request, Authorize: AuthJWT = Depends()):
|
|
|
try:
|
|
@@ -187,6 +195,7 @@ async def system(request: Request, Authorize: AuthJWT = Depends()):
|
|
|
# current_user = Authorize.get_jwt_subject()
|
|
|
return templates.TemplateResponse(name='system.html', context={'request': request})
|
|
|
|
|
|
+
|
|
|
# 溫度API
|
|
|
@app.get('/temperature')
|
|
|
async def get_temperatures():
|
|
@@ -285,7 +294,11 @@ async def get_history(time_end: str):
|
|
|
return fake_data[date]
|
|
|
|
|
|
|
|
|
-
|
|
|
+# Get data from db
|
|
|
+def get_data_from_db(query):
|
|
|
+ db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/aaron_test_db?charset=utf8mb4')
|
|
|
+ data = db.query(query=query)
|
|
|
+ return data
|
|
|
|
|
|
|
|
|
# Login funtion part
|
|
@@ -296,6 +309,7 @@ def check_user_exists(username):
|
|
|
else:
|
|
|
return False
|
|
|
|
|
|
+
|
|
|
def get_user(username: str):
|
|
|
""" 取得使用者資訊(Model) """
|
|
|
db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/aaron_testdb?charset=utf8mb4')
|
|
@@ -306,20 +320,24 @@ def get_user(username: str):
|
|
|
user = models.User(**user_dict)
|
|
|
return user
|
|
|
|
|
|
+
|
|
|
def user_register(user):
|
|
|
db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/aaron_testdb?charset=utf8mb4')
|
|
|
table = db['users']
|
|
|
user.password = get_password_hash(user.password)
|
|
|
table.insert(dict(user))
|
|
|
|
|
|
+
|
|
|
def get_password_hash(password):
|
|
|
""" 加密密碼 """
|
|
|
return pwd_context.hash(password)
|
|
|
|
|
|
+
|
|
|
def verify_password(plain_password, hashed_password):
|
|
|
""" 驗證密碼(hashed) """
|
|
|
return pwd_context.verify(plain_password, hashed_password)
|
|
|
|
|
|
+
|
|
|
def authenticate_user(username: str, password: str):
|
|
|
""" 連線DB,讀取使用者是否存在。 """
|
|
|
db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/aaron_testdb?charset=utf8mb4')
|
|
@@ -331,6 +349,7 @@ def authenticate_user(username: str, password: str):
|
|
|
return False
|
|
|
return user
|
|
|
|
|
|
+
|
|
|
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
|
|
|
""" 創建token,並設定過期時間。 """
|
|
|
to_encode = data.copy()
|