def create(): """ 点击购买后跳转创建评论页面 :return: """ user_id = g.user['id'] deal = get_db().execute('select * from comment where user_id = ?', (user_id, )).fetchall() current_app.logger.debug(f"user {user_id} wether add comment: {len(deal)}") if len(deal) > 0: flash("已经评论过") return redirect(url_for("goods.detail")) if request.method == 'POST': comment_body = request.form['comment'] comment_stars = request.form['stars'] error = None if not comment_body: error = 'comment is required.' if not comment_stars: error = 'comment is required.' if error is not None: flash(error) else: db = get_db() db.execute( 'INSERT INTO comment (user_id, comment, stars)' ' VALUES (?, ?, ?)', (session['user_id'], comment_body, comment_stars)) db.commit() return redirect(url_for('goods.detail')) seller_id = get_seller_id() # price = request.form['price'] price = 65 error = None if not seller_id: error = 'seller_id is required.' if not price: error = 'price is required.' if error is not None: flash(error) else: db = get_db() db.execute( 'INSERT INTO deal (user_id, receiver_id, price)' ' VALUES (?, ?, ?)', (session['user_id'], seller_id, price)) db.commit() return render_template("comment/comment.html")
def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] db = get_db() error = None user = db.execute('SELECT * FROM user WHERE username = ?', (username, )).fetchone() if user is None: error = 'Incorrect username.' elif not check_password_hash(user['password'], password): error = 'Incorrect password.' if error is None: # 参数正确 session.clear() session['user_id'] = user['id'] # return redirect(url_for('index')) # return render_template('home.html', user={'username': username}) # return redirect(url_for('home')) return redirect(url_for('goods.list_all')) # else: # 参数错误 # current_app.logger.debug(f"login error: {error}") # # return render_template('auth/login.html') flash(error) # 消息闪回 # return redirect(url_for('index')) return render_template('auth/login.html')
def register(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] db = get_db() error = None if not username: error = 'Username is required.' elif not password: error = 'Password is required.' elif db.execute('SELECT id FROM user WHERE username = ?', (username, )).fetchone() is not None: error = 'User {} is already registered.'.format(username) if error is None: # 用户存在 db.execute('INSERT INTO user (username, password) VALUES (?, ?)', (username, generate_password_hash(password))) db.commit() return redirect(url_for('auth.login')) # else: # 用户不存在 # return redirect(url_for('auth.login')) flash(error) return render_template('auth/register.html')
def load_logged_in_user(): user_id = session.get('user_id') if user_id is None: g.user = None else: g.user = get_db().execute('SELECT * FROM user WHERE id = ?', (user_id, )).fetchone()
def get_seller_id(): post = get_db().execute('SELECT id FROM user WHERE username = \'seller\'', # (id,) ).fetchone() if post is None: current_app.logger.debug("get seller id error.") return post["id"]
def get_seller_deals(): seller_id = get_seller_id() deals = get_db().execute( 'SELECT id, user_id, receiver_id, created, price' ' FROM deal WHERE receiver_id = ?', (seller_id, )).fetchall() if deals is None: current_app.logger.debug("get deal error") current_app.logger.debug(f"get deal :{deals}") return deals
def back(id): """ 退款,记录退款数据到交易记录里 """ back_deal = get_deal(id) current_app.logger.debug(f"back deal {id}") db = get_db() db.execute( 'INSERT INTO deal (user_id, receiver_id, price)' ' VALUES (?, ?, ?)', (back_deal['receiver_id'], back_deal['user_id'], back_deal['price'])) db.commit() return redirect(url_for('goods.detail'))
def get_all_comments(): db = get_db() db.row_factory = dict_factory comments = db.execute( 'SELECT c.id, user_id, u.username, created, comment, stars from comment c join' ' user u on c.user_id = u.id ', ).fetchall() # 查询是否有'退款'记录,然后计算 for comment in comments: # 查询购买记录 user_id = comment['user_id'] order_deal = db.execute('select * from deal where user_id = ?', (user_id, )).fetchone() if order_deal is None: current_app.logger.debug("get comment deal error") # 查询退款记录 receiver_id = comment['user_id'] user_id = get_seller_id() back_deal = db.execute( 'SELECT * FROM deal WHERE user_id = ? AND receiver_id = ?', (user_id, receiver_id)).fetchone() if back_deal is not None: if order_deal['price'] == back_deal['price']: # 计算币天,标记退款 comment['back'] = True delta = back_deal['created'] - order_deal['created'] current_app.logger.debug(f"coin delta date: {delta}") comment['coin_day'] = int( delta.total_seconds() * int(order_deal['price']) / timedelta(days=1).total_seconds() * 100) continue # 计算本评论的币天 delta = datetime.now() - order_deal['created'] current_app.logger.debug(f"coin delta date: {delta}") comment['coin_day'] = int(delta.total_seconds() * int(order_deal['price']) / timedelta(days=1).total_seconds() * 100) if comments is None: current_app.logger.debug("get comments error.") current_app.logger.debug(f"comments: {comments}") return comments
def get_user_name(user_id): username = get_db().execute('SELECT username' ' FROM user where id = ?', (user_id, )) return username
def get_deal(id): db = get_db() deal = db.execute( 'SELECT user_id, receiver_id, price FROM deal' ' WHERE id = ?', (id, )).fetchone() return deal