|
@@ -15,8 +15,8 @@ from typing import List, Optional
|
|
|
import os
|
|
|
|
|
|
# time
|
|
|
-import datetime
|
|
|
-from datetime import timedelta
|
|
|
+# import datetime
|
|
|
+from datetime import timedelta, datetime
|
|
|
|
|
|
# db
|
|
|
import dataset
|
|
@@ -62,18 +62,11 @@ templates = Jinja2Templates(directory='templates')
|
|
|
async def index(request: Request):
|
|
|
print(request)
|
|
|
return templates.TemplateResponse(name='index.html', context={'request': request})
|
|
|
-
|
|
|
-
|
|
|
-# @app.get('/make_video', response_class=HTMLResponse)
|
|
|
-# async def index(request: Request):
|
|
|
-# print(request)
|
|
|
-# return templates.TemplateResponse(name='make_video.html', context={'request': request})
|
|
|
-
|
|
|
+
|
|
|
@app.get('/login', response_class=HTMLResponse)
|
|
|
async def login(request: Request):
|
|
|
return templates.TemplateResponse(name='login.html', context={'request': request})
|
|
|
|
|
|
-
|
|
|
@app.post("/login")
|
|
|
async def login_for_access_token(request: Request, form_data: OAuth2PasswordRequestForm = Depends(), Authorize: AuthJWT = Depends()):
|
|
|
db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/aaron_testdb?charset=utf8mb4')
|
|
@@ -99,7 +92,25 @@ async def login_for_access_token(request: Request, form_data: OAuth2PasswordRequ
|
|
|
return {"access_token": access_token, "token_type": "bearer"}
|
|
|
|
|
|
|
|
|
+@app.get('/register', response_class=HTMLResponse)
|
|
|
+async def login(request: Request):
|
|
|
+ return templates.TemplateResponse(name='register.html', context={'request': request})
|
|
|
|
|
|
+@app.post('/register')
|
|
|
+async def register(request: Request, form_data: OAuth2PasswordRequestForm = Depends()):
|
|
|
+ user = models.User(**await request.form())
|
|
|
+ print(form_data.username, form_data.password, user)
|
|
|
+
|
|
|
+ # 密碼加密
|
|
|
+ user.password = get_password_hash(user.password)
|
|
|
+
|
|
|
+ # 存入DB
|
|
|
+ db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/aaron_testdb?charset=utf8mb4')
|
|
|
+ user_table = db['users']
|
|
|
+ user_table.insert(dict(user))
|
|
|
+
|
|
|
+ # 跳轉頁面至登入
|
|
|
+ return templates.TemplateResponse(name='login.html', context={'request': request})
|
|
|
|
|
|
@app.get('/home', response_class=HTMLResponse)
|
|
|
async def login(request: Request):
|
|
@@ -138,7 +149,7 @@ async def login(request: Request):
|
|
|
# Login funtion part
|
|
|
def check_user_exists(username):
|
|
|
db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/aaron_testdb?charset=utf8mb4')
|
|
|
- if int(next(iter(db.query('SELECT COUNT(*) FROM aaron_testdb.user WHERE username = "'+username+'"')))['COUNT(*)']) > 0:
|
|
|
+ if int(next(iter(db.query('SELECT COUNT(*) FROM aaron_testdb.users WHERE userName = "'+username+'"')))['COUNT(*)']) > 0:
|
|
|
return True
|
|
|
else:
|
|
|
return False
|
|
@@ -149,15 +160,15 @@ def get_user(username: str):
|
|
|
if not check_user_exists(username): # if user don't exist
|
|
|
return False
|
|
|
user_dict = next(
|
|
|
- iter(db.query('SELECT * FROM aaron_testdb.user where username ="'+username+'"')))
|
|
|
+ iter(db.query('SELECT * FROM aaron_testdb.users where userName ="'+username+'"')))
|
|
|
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 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):
|
|
|
""" 加密密碼 """
|
|
@@ -172,7 +183,7 @@ def authenticate_user(username: str, password: str):
|
|
|
db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/aaron_testdb?charset=utf8mb4')
|
|
|
if not check_user_exists(username): # if user don't exist
|
|
|
return False
|
|
|
- user_dict = next(iter(db.query('SELECT * FROM aaron_testdb.user where username ="'+username+'"')))
|
|
|
+ user_dict = next(iter(db.query('SELECT * FROM aaron_testdb.users where userName ="'+username+'"')))
|
|
|
user = models.User(**user_dict)
|
|
|
if not verify_password(password, user.password):
|
|
|
return False
|