Example #1
0
def rating():
    """
    书籍评分
    :return: update
    """
    userid = session['userid']
    try:
        if request.method == 'POST':
            rank = request.values.get('rank')
            bookid = request.values.get('book_id')
            sql = '''SELECT COUNT(1) as count FROM Bookrating WHERE UserID="{0}" and BookID="{1}" '''.format(
                userid, bookid)
            count = mysql.fetchone_db(sql)
            if count['count']:
                sql = '''UPDATE Bookrating SET Rating='{2}' WHERE UserID="{0}" and BookID="{1}"  '''.format(
                    userid, bookid,
                    int(rank) * 2)
            else:
                sql = '''INSERT INTO Bookrating (UserID,BookID,Rating) values ('{0}','{1}','{2}') '''.format(
                    userid, bookid,
                    int(rank) * 2)
            mysql.exe(sql)
            logger.info("update book rating success,sql:{}".format(sql))
    except Exception as e:
        logger.exception("rating books error: {}".format(e))
    return redirect(url_for('root'))
Example #2
0
def is_valid(username):
    """
    登录验证
    :param username: 用户名
    :return: True/False
    """
    try:
        sql = "SELECT UserID as Username FROM User where UserID='{}'".format(username)
        result = mysql.fetchone_db(sql)

        if result:
            logger.info('username:{}: has login success'.format(username))
            return True
        else:
            logger.info('username:{}: has login filed'.format(username))
            return False
    except Exception as e:
        logger.exception('username:{}: has login error'.format(username))
        return False
Example #3
0
def bookinfo():
    """
    书籍详情
    :return: BookInfo.html
    """
    # 获取用户IP
    score = 0
    if 'userid' not in session:
        userid = None
        login = False
    else:
        userid = session['userid']
        login = True
    try:
        if request.method == 'GET':
            bookid = request.args.get('bookid')
            sql = """SELECT BookTitle,
                            BookID,
                            PubilcationYear,
                            BookAuthor,
                            ImageM from Books where BookID="{}" """.format(
                bookid)
            book_info = mysql.fetchall_db(sql)
            book_info = [v for k, v in book_info[0].items()]
            update_recommend_book(userid, bookid)
        if userid:
            sql = '''SELECT Rating FROM Bookrating 
                            where UserID="{}" and BookID="{}"'''.format(
                userid, bookid)
            score = mysql.fetchone_db(sql)
            if score:
                score = int(score['Rating'])
                score = math.ceil(score / 2)
                if score > 10: score = 10

    except Exception as e:
        logger.exception("select book info error: {}".format(e))
    return render_template('BookInfo.html',
                           book_info=book_info,
                           login=login,
                           useid=userid,
                           score=score)
Example #4
0
def user():
    """
    个人信息
    :return: UserInfo.html
    """
    login, userid = False, None
    if 'userid' not in session:
        return redirect(url_for('app.loginForm'))
    else:
        login, userid = True, session['userid']
    userinfo = []
    try:
        sql = "select UserID,Location,Age from User where UserID='{}'".format(userid)
        userinfo = mysql.fetchone_db(sql)
        userinfo = [v for k, v in userinfo.items()]
    except Exception as e:
        logger.exception("select UserInfo error: {}".format(e))
    return render_template("UserInfo.html",
                           login=login,
                           useid=userid,
                           userinfo=userinfo)
Example #5
0
def update_recommend_book(UserID, BookID):
    """
    更新推荐数据
    """

    sql = '''SELECT score FROM Booktuijian WHERE UserID="{0}" and BookID="{1}"'''.format(UserID, BookID)
    score = mysql.fetchone_db(sql)
    if score:
        score = int(score['score'])
    
        if score + 0.5 > 10: score =10
        else: score += 0.5
        sql = '''UPDATE Booktuijian SET score='{2}' WHERE UserID="{0}" and BookID="{1}" '''.format(UserID, BookID,
                                                                                                   int(score))
        logger.info("update_recommend_book, sql:{}".format(sql))
        mysql.exe(sql)
    else:
        score = 0.5
        sql = ''' insert into Booktuijian (UserID,BookID,score) values ('{0}','{1}','{2}') '''.format(UserID, BookID,
                                                                                                      int(score))
        logger.info("update_recommend_book, sql:{}".format(sql))
        mysql.exe(sql)
Example #6
0
def add():
    '''
    添加购物车
    '''
    login, userid = False, None
    if 'userid' not in session:
        return redirect(url_for('app.loginForm'))
    else:
        login, userid = True, session['userid']
    try:
        if request.method == 'GET':
            bookid = request.values.get('bookid')

            sql = '''SELECT COUNT(1) as count FROM Cart WHERE UserID="{0}" and BookID="{1}" '''.format(userid,
                                                                                                       bookid)
            count = mysql.fetchone_db(sql)
            if not count['count']:
                sql = '''INSERT INTO Cart (UserID,BookID ) values ('{0}','{1}') '''.format(userid, bookid)
                mysql.exe(sql)
                logger.info("update Cart  success,sql:{}".format(sql))
    except Exception as e:
        logger.exception("update Cart  books error: {}".format(e))
    return redirect(url_for('app.order'))