Selaa lähdekoodia

add user util & fix page bug

ming 3 vuotta sitten
vanhempi
commit
4fb7fb895b
4 muutettua tiedostoa jossa 79 lisäystä ja 1 poistoa
  1. 20 0
      api/mailer.py
  2. 1 1
      api/templates/make_video.html
  3. BIN
      api/util/__pycache__/user.cpython-39.pyc
  4. 58 0
      api/util/user.py

+ 20 - 0
api/mailer.py

@@ -35,3 +35,23 @@ def send_left_not_enough(msg_in,email):
         traceback.print_exc()
         print ('Something went wrong...')
 
+def register_verify(msg_in,email):
+    to = [email]
+    msg = MIMEMultipart()
+    msg['Subject'] = '驗證您的信箱'
+    
+    msgAlternative = MIMEMultipart('alternative')
+    msg.attach(msgAlternative)
+
+    text = MIMEText(msg_in,'html','utf-8')
+
+    msgAlternative.attach(text)
+    try:
+        server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
+        server.ehlo()
+        server.login(gmail_user, gmail_password)
+        server.sendmail(sent_from, to, msg.as_string())
+        server.close()
+    except:
+        traceback.print_exc()
+

+ 1 - 1
api/templates/make_video.html

@@ -91,7 +91,7 @@
           </fieldset>
           <fieldset>
             <h3 class="fs-subtitle" set-lan="html:lines">台詞</h3>
-            <input id=slide_raw_url type="text" name='t1' class='title_new' value="" placeholder="標題" /><label for="myCheck" set-lan="html:add_eng">加入英文:</label> 
+ <label for="myCheck" set-lan="html:add_eng">加入英文:</label> 
             <input type="checkbox" id="multiLang" > <br/>
              <div class="subtitle-inputs">
             

BIN
api/util/__pycache__/user.cpython-39.pyc


+ 58 - 0
api/util/user.py

@@ -0,0 +1,58 @@
+class user_util():
+
+    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
+            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 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 AI_anchor.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_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