Bläddra i källkod

Merge branch 'master' of http://git.choozmo.com:3000/choozmo/AI_Anchor_2 into master

jeter20131220 3 år sedan
förälder
incheckning
10fc356ede
3 ändrade filer med 106 tillägg och 92 borttagningar
  1. 1 1
      OpenshotService/openshot_video_generator.py
  2. 22 19
      api/main.py
  3. 83 72
      api/util/user.py

+ 1 - 1
OpenshotService/openshot_video_generator.py

@@ -767,7 +767,7 @@ def anchor_video_v2(name_hash,name,text_content, image_urls,multiLang,avatar):
         img_list[idx] = openshot.FFmpegReader(dir_photo+name_hash+'/'+p)
         img_list[idx].Open()
         photo_clip_list[idx] = video_photo_clip(vid=img_list[idx],layer=3
-                ,scale_x=0.81,scale_y=0.68,location_y=-0.03,position=main_timer,end=clip_duration,audio=False)
+                ,scale_x=0.8,scale_y=0.704,location_y=-0.03,position=main_timer,end=clip_duration,audio=False)
         t.AddClip(photo_clip_list[idx])
         img_list[idx].Close()
 

+ 22 - 19
api/main.py

@@ -36,7 +36,6 @@ from fastapi_jwt_auth.exceptions import AuthJWTException
 from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
 import models
 import pymysql
-from first import first
 import mailer
 from moviepy.editor import VideoFileClip
 import traceback
@@ -44,6 +43,7 @@ import logging
 import gSlide
 import aiofiles
 import json
+import util.user
 pymysql.install_as_MySQLdb()
 
 app = FastAPI()
@@ -182,7 +182,10 @@ async def login_for_access_token(request: Request, form_data: OAuth2PasswordRequ
     Authorize.set_access_cookies(access_token)
     Authorize.set_refresh_cookies(refresh_token)
     #return templates.TemplateResponse("index.html", {"request": request, "msg": 'Login'})
-    return {"access_token": access_token, "token_type": "bearer"}
+    if util.user.email_veri_pass(form_data.username):
+        return {"access_token": access_token, "token_type": "bearer",'veri':'ok'}
+    else:
+        return {"access_token": access_token, "token_type": "bearer",'veri':'fail'}
 
 
 @app.post("/token")
@@ -208,13 +211,14 @@ async def register_old(request: Request):
     user = models.User(**await request.form())
     user_obj = first(db.query('SELECT * FROM users where username ="'+str(user.username)+'"'))
     if user_obj == None:
-        user_register(user)
-        user_obj = first(db.query('SELECT * FROM users where username ="'+str(request.username)+'"'))
-        veri_table = db['register_veri_code']
-        code = str(time.time()).replace('.','')
-        veri_table.insert({'code':code,'user_id':user_obj['id']})
-        mailer.register_verify('請至點擊網址驗證 : https://www.choozmo.com:8887/verify_email?code='+code, user_obj['email'])
-        return '註冊成功! 請回到上頁登入帳號'
+        id = user_register(user)
+        if type(id) is int:
+            code = str(time.time()).replace('.','')
+            db['register_veri_code'].insert({'code':code,'user_id':id})
+            mailer.register_verify('請至點擊網址驗證 : https://www.choozmo.com:8887/verify_email?code='+code, user.email)
+            return '註冊成功! 請回到上頁登入帳號'
+        else :
+            return {'msg':'error'}
         #return templates.TemplateResponse("make_video.html", {"request": request, "success": True},status_code=status.HTTP_302_FOUND)
         #return templates.TemplateResponse("login.html", {'request': request,"success": True}, status_code=status.HTTP_302_FOUND)
     else:
@@ -226,15 +230,14 @@ async def register(request: models.register_req):
     user_obj = first(db.query('SELECT * FROM users where username ="'+str(request.username)+'"'))
     
     if user_obj == None:
-        if user_register(request):
-            user_obj = first(db.query('SELECT * FROM users where username ="'+str(request.username)+'"'))
-            veri_table = db['register_veri_code']
+        id = user_register(request)
+        if type(id) is int:
             code = str(time.time()).replace('.','')
-            veri_table.insert({'code':code,'user_id':user_obj['id']})
-            mailer.register_verify('請至點擊網址驗證 : https://www.choozmo.com:8887/verify_email?code='+code, user_obj['email'])
-            return {'msg':'ok'}
-        else:
-            return {'msg': '內部錯誤'}
+            db['register_veri_code'].insert({'code':code,'user_id':id})
+            mailer.register_verify('請至點擊網址驗證 : https://www.choozmo.com:8887/verify_email?code='+code, request.email)
+            return '註冊成功! 請回到上頁登入帳號'
+        else :
+            return {'msg':'error'}
     else:
         return {'msg':request.username+':使用者名稱重複,請更改'}
 
@@ -506,8 +509,8 @@ def user_register(user):
     db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
     table = db['users']
     user.password = get_password_hash(user.password)
-    table.insert(dict(user))
-    return True
+    id = table.insert(dict(user))
+    return id
 
 def get_password_hash(password):
     return pwd_context.hash(password)

+ 83 - 72
api/util/user.py

@@ -1,78 +1,89 @@
-class user_util():
-    def get_user_id(self, token):
-        db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
-        credentials_exception = HTTPException(
-            status_code=status.HTTP_401_UNAUTHORIZED,
-            detail="Could not validate credentials",
-            headers={"WWW-Authenticate": "Bearer"},
-        )
-        try:
-            payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
-            username: str = payload.get("sub")
-            if username is None:
-                raise credentials_exception
-            token_data = models.TokenData(username=username)
-        except JWTError:
+import dataset
+from first import first
+def get_user_id(token):
+    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
+    credentials_exception = HTTPException(
+        status_code=status.HTTP_401_UNAUTHORIZED,
+        detail="Could not validate credentials",
+        headers={"WWW-Authenticate": "Bearer"},
+    )
+    try:
+        payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
+        username: str = payload.get("sub")
+        if username is None:
             raise credentials_exception
-        user = get_user(username=token_data.username)
-        if user is None:
-            raise credentials_exception
-        user_id = first(db.query('SELECT * FROM users where username="' + user.username+'"'))['id']
-        return user_id
+        token_data = models.TokenData(username=username)
+    except JWTError:
+        raise credentials_exception
+    user = get_user(username=token_data.username)
+    if user is None:
+        raise credentials_exception
+    user_id = first(db.query('SELECT * FROM users where username="' + user.username+'"'))['id']
+    return user_id
+
+def check_user_exists( username):
+    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
+    if int(next(iter(db.query('SELECT COUNT(*) FROM AI_anchor.users WHERE username = "'+username+'"')))['COUNT(*)']) > 0:
+        return True
+    else:
+        return False
 
-    def check_user_exists(self, username):
-        db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
-        if int(next(iter(db.query('SELECT COUNT(*) FROM AI_anchor.users WHERE username = "'+username+'"')))['COUNT(*)']) > 0:
-            return True
-        else:
-            return False
+def get_user( username: str):
+    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
+    if not check_user_exists(username):  # if user don't exist
+        return False
+    user_dict = next(
+        iter(db.query('SELECT * FROM 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/AI_anchor?charset=utf8mb4')
+    table = db['users']
+    user.password = get_password_hash(user.password)
+    table.insert(dict(user))
 
-    def get_user(self, username: str):
-        db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
-        if not check_user_exists(username):  # if user don't exist
-            return False
-        user_dict = next(
-            iter(db.query('SELECT * FROM AI_anchor.users where username ="'+username+'"')))
-        user = models.User(**user_dict)
-        return user
-        
-    def user_register(self, user):
-        db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?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):
+    return pwd_context.verify(plain_password, hashed_password)
+def authenticate_user( username: str, password: str):
+    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
+    if not check_user_exists(username):  # if user don't exist
+        return False
+    user_dict = next(iter(db.query('SELECT * FROM AI_anchor.users where username ="'+username+'"')))
+    user = models.User(**user_dict)
+    if not verify_password(password, user.password):
+        return False
+    return user
 
-    def get_password_hash(self, password):
-        return pwd_context.hash(password)
-    def verify_password(self, plain_password, hashed_password):
-        return pwd_context.verify(plain_password, hashed_password)
-    def authenticate_user(self, username: str, password: str):
-        db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
-        if not check_user_exists(username):  # if user don't exist
-            return False
-        user_dict = next(iter(db.query('SELECT * FROM AI_anchor.users where username ="'+username+'"')))
-        user = models.User(**user_dict)
-        if not verify_password(password, user.password):
-            return False
-        return user
+def get_roles( username):
+    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
+    state = 'SELECT * FROM user_role '\
+    'INNER JOIN users on user_role.user_id= users.id'\
+    'INNER JOIN role on user_role.role_id = role.id '\
+    'WHERE username=username'
+    role_list = []
+    for row in db.query(statement):
+        role_list.append({'id':row['role_id'],'name':row['name']})
+    return role_list
+    
+#def add_role( username,role_id):
+    #db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
+    #user_role_table = db['user_role']
+    #user_role_table.insert({'user_id':,'role_id':role_id})
 
-    def get_roles(self, username):
-        db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
-        state = 'SELECT * FROM user_role '\
-        'INNER JOIN users on user_role.user_id= users.id'\
-        'INNER JOIN role on user_role.role_id = role.id '\
-        'WHERE username=username'
-        role_list = []
-        for row in db.query(statement):
-            role_list.append({'id':row['role_id'],'name':row['name']})
-        return role_list
-        
-    def add_role(self, username,role_id):
-        db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
-        user_role_table = db['user_role']
-        user_role_table.insert({'user_id':,'role_id':role_id})
+def get_user_id( username):
+    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
+    return str(first(db.query('SELECT COUNT(*) FROM history_input WHERE user_id ='+str(user_obj['id'])))['COUNT(*)'])
+    
 
-    def get_user_id(self, username):
-        db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
-        return str(first(db.query('SELECT COUNT(*) FROM history_input WHERE user_id ='+str(user_obj['id'])))['COUNT(*)'])
-        
+def email_veri_pass(name):
+    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
+    user_dict = next(iter(db.query('SELECT * FROM users where username ="'+name+'"')))
+    print(user_dict)
+    user_obj = first(db.query('SELECT * FROM register_veri_code where user_id ="'+str(user_dict['id'])+'"'))
+    if user_obj == None:
+        return False
+    else:
+        return True