Example #1
0
def gen_user(uname, u_pass):
    query = "SELECT username FROM users WHERE username = %s"
    username = db_helper.fetchone(query, uname)
    if username:
        return "It Exist"

    insert_user = "******"

    password = make_password(u_pass, TOKEN_SALT)
    values = (uname, password, datetime.now())
    return db_helper.execute_commit(insert_user, values)
Example #2
0
    def resolve_article(root, info, id):
        sql = "SELECT id, title, image_url, date, source, audio_url, transcript FROM news WHERE id = %s"
        article = db_helper.fetchone(sql, id)
        article_dict = _map_base_article(article)

        *_, source, audio_url, transcript = article

        article_dict["source"] = source
        article_dict["src"] = FILE_SERVER_URL + "/audio/" + audio_url
        article_dict["transcript"] = transcript
        return article_dict
Example #3
0
def check_user_authed(token):
    try:
        user_payload = jwt.decode(token, TOKEN_SALT, algorithms=["HS256"])
        user_id = user_payload.get("id")
        username = user_payload.get("username")
        if user_id:
            sql = "SELECT id, username FROM users WHERE id=%s and username=%s"
            return db_helper.fetchone(sql, (user_id, username))
        raise ValueError("Auth failed")
    except jwt.ExpiredSignatureError as e:
        raise jwt.ExpiredSignature("Signature has expired")
    except jwt.InvalidSignatureError as e:
        raise jwt.InvalidSignatureError("Signature is invalid")
    except jwt.InvalidTokenError as e:
        raise jwt.InvalidTokenError("Invalid token type")
    except Exception as e:
        raise e
Example #4
0
def login():
    data = request.get("data")
    if not data:
        return show_reponse(code=Status.no_auth)
    username = data.get("username")
    password = data.get("password")
    query = "SELECT id FROM users WHERE username = %s and password = %s"
    user = db_helper.fetchone(query,
                              (username, make_password(password, TOKEN_SALT)))
    if not user:
        return show_reponse(code=Status.no_auth)
    payload = {
        "id": str(head(user)),
        "username": username,
        "exp": time.time() + TOKEN_EXP,
    }
    token = jwt.encode(payload, TOKEN_SALT, algorithm="HS256")
    return show_reponse(data={"token": token, "username": username})
Example #5
0
File: web.py Project: 1r21/taishan
def get_news_by_id():
    data = request.get("data")
    if not data:
        return show_reponse(code=Status.other, message="param error")
    article_id = data.get("id")
    sql = "SELECT title, source, image_url, transcript, date, audio_url FROM news WHERE id = %s"
    article = db_helper.fetchone(sql, article_id)
    if article:
        title, source, image_url, transcript, date, audio_url = article
        detail = dict(
            title=title,
            transcript=transcript,
            src=FILE_SERVER_URL + "/audio/" + audio_url,
            source=source,
            cover=FILE_SERVER_URL + "/image/" + image_url,
            date=date.strftime("%Y-%m-%d"),
        )
        return show_reponse(data=detail)
    return show_reponse(code=Status.other, message="News is not exist!")
Example #6
0
def send_dd_message():
    data = request.get("data")
    err_code = Status.other
    err_message = "News is not exist!"
    if not data:
        return show_reponse(code=err_code, message="Param Error")
    article_id = data.get("id")
    sql = "SELECT date, title, image_url FROM news WHERE id = %s"
    article = db_helper.fetchone(sql, article_id)
    if article:
        date, title, image_url = article
        dd_bot = DDBot()
        template = DDBot.get_template(
            title=date.strftime("%d-%m-%Y"),
            content=title,
            pic_url=f"{FILE_SERVER_URL}/image/{image_url}",
            msg_url=f"{WEB_APP_URL}/detail/{article_id}",
        )
        dd_info = dd_bot.send(template)
        code = dd_info.get("errcode")
        err_message = dd_info.get("errmsg")
        if code == 0:
            err_code = Status.success
    return show_reponse(code=err_code, message=err_message)
Example #7
0
 def __has_base_article(self):
     query = "SELECT id, image_url, audio_url, audio_from, transcript FROM news WHERE title = %s"
     return db_helper.fetchone(query, self.title)