def create(): if request.method == 'POST': title = request.form['title'] picture = request.files['picture'] description = request.form['description'] price = request.form['price'] discount = request.form['discount'] amount = request.form['amount'] error = None if not title: error = 'Title is required.' elif picture.filename == '': error = '请上传图片。' if error is not None: flash(error) else: filename = secure_filename(picture.filename) picture.save(os.path.join('BooksOnline\static\img\\book', filename)) db = get_db() db.execute( 'INSERT INTO book (picture,owner,price,discount,amount,description,title)' ' VALUES (?, ?, ?, ?, ?, ?, ?)', (filename, g.user['id'], price, discount, amount, description, title)) db.commit() return redirect(url_for('explore.index')) return render_template('explore/create.html')
def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] passcode1 = request.form['passcode1'] passcode2 = request.form['passcode2'] db = get_db() error = None user = db.execute('SELECT * FROM user WHERE username = ?', (username, )).fetchone() if user is None: error = '用户名错误' elif not check_password_hash(user['password'], password): error = '密码错误' elif passcode1 != passcode2: error = '验证码错误' if error is None: session.clear() session['user_id'] = user['id'] return redirect(url_for('explore.index')) flash(error) passcodes = ['hlia', 'ikdd', 'plcc', 'sdqt'] passcode = passcodes[random.randint(0, len(passcodes) - 1)] return render_template('auth/login.html', passcode=passcode)
def register(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] sex = request.form['sex'] db = get_db() error = None if not username: error = '请输入用户名' elif not password: error = '请输入密码' elif db.execute('SELECT id FROM user WHERE username = ?', (username, )).fetchone() is not None: error = '用户名 {} 已经被注册了'.format(username) if error is None: db.execute( 'INSERT INTO user (username, password, sex) VALUES (?, ?, ?)', (username, generate_password_hash(password), sex)) db.commit() return redirect(url_for('auth.login')) flash(error) return render_template('auth/register.html')
def cart(): user = g.user['id'] db = get_db() books = db.execute( 'SELECT b.id,picture,title,price,description,amount,discount' ' FROM cart c JOIN book b ON c.book = b.id' ' WHERE c.user=? ORDER BY c.created DESC ', (user, )).fetchall() return render_template('check/cart.html', books=books)
def delete(id): print('hello1') get_book(id) db = get_db() db.execute('DELETE FROM book WHERE id = ?', (id, )) db.commit() print('hello2') return redirect(url_for('explore.index'))
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 update(id): book = get_book(id) if request.method == 'POST': title = request.form['title'] picture = request.files['picture'] print(bool(picture.filename)) description = request.form['description'] price = request.form['price'] discount = request.form['discount'] amount = request.form['amount'] error = None if not title: error = 'Title is required.' if error is not None: flash(error) elif not picture.filename: print('hi') db = get_db() db.execute( 'UPDATE book SET title = ?, description = ?, price = ?, discount = ?, amount = ?' ' WHERE id = ?', (title, description, price, discount, amount, id)) db.commit() return redirect(url_for('explore.index')) else: filename = picture.filename picture.save(os.path.join('BooksOnline\static\img\\book', filename)) db = get_db() db.execute( 'UPDATE book SET title = ?, picture = ?, description = ?, price = ?, discount = ?, amount = ?' ' WHERE id = ?', (title, filename, description, price, discount, amount, id)) db.commit() return redirect(url_for('explore.index')) return render_template('explore/update.html', book=book)
def add(): book = request.args.get('id') user = g.user['id'] cart_book = get_cart_book(user, book) print(cart_book) if cart_book is not None: print(cart_book) flash('已经添加过了') db = get_db() db.execute('INSERT INTO cart (user,book) VALUES (?,?)', (user, book)) db.commit() flash("成功加入购物车") return redirect(url_for('explore.index'))
def get_book(id, check_author=True): book = get_db().execute( 'SELECT b.id, title, description, created, owner, price, discount, amount, username, picture' ' FROM book b JOIN user u ON b.owner = u.id' ' WHERE b.id = ?', (id, )).fetchone() if book is None: abort(404, "Post id {0} doesn't exist.".format(id)) # if check_author and book['owner'] != g.user['id']: # abort(403) return book
def index(): n = request.args.get('n') if not n: n = 0 else: n = int(n) if n - 5 < 0: n = 0 db = get_db() books = db.execute( 'SELECT p.id, title, description, created, owner, price, discount, amount, username, picture' ' FROM book p JOIN user u ON p.owner = u.id' ' ORDER BY created DESC LIMIT 5 OFFSET ?', (n, )).fetchall() return render_template('explore/index.html', books=books, n=n)
def get_cart_book(user, book): db = get_db() cart_book = db.execute('SELECT id FROM cart WHERE user=? AND book=?', (user, book)).fetchall() return cart_book