def dangsach(): form = DangSach() if form.validate_on_submit(): validatefile = request.files['imageFile'] # save the image only when user chooses a file if validatefile and allowed_file(validatefile.filename): filename = id_generator() + secure_filename(form.imageFile.data.filename) form.imageFile.data.save(uf + filename) else: flash('please choose image File') return render_template("users/dang-sach.html", form=form, is_auth = g.user.is_authenticated(), username = g.user.nickname) book = Book( truong=form.truong.data, khuvuc=request.form['khuvuc'], chuyennganh=request.form['chuyennganh'], giaovien=form.giaovien.data,\ tensach=form.tensach.data, tacgia=form.tacgia.data, theloai=form.theloai.data, tinhtrang=form.tinhtrang.data, giaban=form.giaban.data, \ noigapmat=form.noigapmat.data, thoigiangapmat=form.thoigiangapmat.data, lienhe=form.lienhe.data, \ author=g.user, thoigiandang=datetime.utcnow(), image=(filename)) # Insert the record in our database and commit it db.session.add(book) db.session.commit() # Update number of books in db of that user g.user.sosachdang = g.user.sosachdang + 1 db.session.add(g.user) db.session.commit() flash(u'Đăng Sách Thành Công! ' ) return redirect(url_for('users.home')) return render_template("users/dang-sach.html", form=form, is_auth = g.user.is_authenticated(), username = g.user.nickname)
def suathongtinsach(bookid): form = DangSach() book = Book.query.get(bookid) # Check if the book is not owned by user, then redirect to users.home if book not in g.user.books: flash(u"Bạn không sở hữu sách này") return redirect(url_for('users.home')) # This is the book is owned by user elif form.validate_on_submit(): book.truong = form.truong.data book.khuvuc = request.form['khuvuc'] book.chuyennganh = request.form['chuyennganh'] book.giaovien = form.giaovien.data book.tensach = form.tensach.data book.tacgia = form.tacgia.data book.theloai = request.form['theloai'] book.tinhtrang = request.form['tinhtrang'] book.giaban = form.giaban.data book.noigapmat = form.noigapmat.data book.thoigiangapmat = form.thoigiangapmat.data book.lienhe = form.lienhe.data book.thoigiandang = datetime.utcnow() ''' # Handling file upload filename = secure_filename(form.imageFile.data.filename) validatefile = request.files['imageFile'] # save the image only when user chooses a file, otherwise keep the same picture if validatefile: form.imageFile.data.save(uf + filename) book.image = filename ''' # Overwrite the current book in database validatefile = request.files['imageFile'] if validatefile and allowed_file(validatefile.filename): os.remove(uf + book.image) filename = id_generator() + secure_filename(form.imageFile.data.filename) form.imageFile.data.save(uf + filename) book.image = filename db.session.add(book) db.session.commit() flash(u'Cập Nhật Thông Tin Sách Thành Công!') return redirect(url_for('users.sachdadang')) else: form.truong.data = book.truong form.giaovien.data = book.giaovien form.tensach.data = book.tensach form.tacgia.data = book.tacgia form.giaban.data = book.giaban form.noigapmat.data = book.noigapmat form.thoigiangapmat.data = book.thoigiangapmat form.lienhe.data = book.lienhe return render_template("users/sua-thong-tin-sach.html", form=form, is_auth = g.user.is_authenticated(), username = g.user.nickname, \ last_image=book.image)