def book_delete(book_id): if not current_user.is_admin: abort(403) b = Book.get(book_id) if not b: abort(404) b.delete() return redirect(url_for("admin_public"))
def book_share(book_id): b = Book.get(book_id) if not b: abort(404) if b.added_by_login != current_user.login: abort(403) b.is_public = not b.is_public b.save() return redirect(url_for("main"))
def book_get(book_id): b = Book.get(book_id) if not b: abort(404) if not b.is_public: abort(403) if not current_user.has_book(b): current_user.books.append(b) current_user.save() return redirect(url_for("public"))
def book_remove(book_id): b = Book.get(book_id) if not b: abort(404) if not b.is_public: abort(403) if current_user.has_book(b): current_user.books.remove(b) current_user.save() return redirect(url_for("main"))
def createBook(): if request.method == 'POST': name = request.form['createTitle'] author = request.form['createAuthor'] description = request.form['createDescription'] price = request.form['createPrice'] user_id = current_user.id print("book") print(request.files) if request.files: image = request.files['createImage'] if(image.filename == ''): print("OHNE IMAGE FILE") print(user_id) new_Book = Book(name, author, description, price, user_id) db.session.add(new_Book) db.session.commit() flash('Book added.') return redirect(url_for('renderHomepage')) if not allowed_file(image.filename): flash( "Filename has to be on of the following types: 'pdf', 'png', 'jpg', 'jpeg'.") return redirect(url_for("renderHomepage")) else: filename = secure_filename(image.filename) image.save(os.path.join(IMAGE_UPLOADS, filename)) book_with_image = Book( name, author, description, price, user_id,filename) db.session.add(book_with_image) db.session.commit() flash('Book with image added.') return redirect(url_for('renderHomepage')) return redirect(url_for('renderHomepage'))
def book_create(): if request.method == "GET": return render_template("book/create.html") validator = Book.get_input_validator() try: args = parser.parse(validator, request) except Exception as e: return render_template("book/create.html", error=str(e.data["exc"].arg_name)) book = Book() book.author = args["author"] book.name = args["name"] book.year = args["year"] # file file = request.files["file"] extension = file.filename.split(".")[-1] or "" book.file = book.id + "." + extension file.save(os.path.join(app.config["UPLOAD_FOLDER"], book.file)) # book.added_by_login = current_user.login book.save() return redirect(url_for("main"))
def search_by_isbn(self, isbn): schema = BookSchema() book = Book.query.filter(Book.isbn == isbn).first() if book: dumps = schema.dump(book).data else: url = self.isbn_url.format(isbn) data = HTTP.get(url) if not data: self.total = 0 return dumps = schema.dump(data).data with db.auto_commit(): book = Book(**dumps) db.session.add(book) self.__fill_single(dumps)
def add(): """ Function to display page for add item. """ form = AddForm(request.form) if request.method == 'GET': return render_template('add.html', title = 'Adding new book', form = form) elif request.method == 'POST': if form.validate_on_submit(): book = Book() form.populate_obj(book) flash('Book "' + book.name + '" added!') session.add(book) return redirect('/') else: flash('Adding failed! Fill all fields!') return redirect('/add')
def setUp(self): # 更新配置 app.config.update( TESTING=True, # 首先将 TESTING 设为 True 来开启测试模式,这样在出错时不会输出多余信息. SQLALCHEMY_DATABASE_URI= 'sqlite:///:memory:' # 这会使用 SQLite 内存型数据库,不会干扰开发时使用的数据库文件,你也可以使用不同文件名的 SQLite 数据库文件,但内存型数据库速度更快。 ) # 创建数据库和表 db.create_all() # 创建测试数据,一个用户,一个电影条目 user = User(name='Test', username='******') user.set_password('123') movie = Book(title='Test Movie Title', year='2020') # 使用 add_all() 方法一次添加多个模型类实例,传入列表 db.session.add_all([user, movie]) db.session.commit() self.client = app.test_client( ) # 创建测试客户端 返回一个测试客户端对象,可以用来模拟客户端(浏览器),我们创建类属性 self.client 来保存它。 # 对它调用 get() 方法就相当于浏览器向服务器发送 GET 请求,调用 post() 则相当于浏览器向服务器发送 POST 请求,以此类推。 self.runner = app.test_cli_runner() # 创建测试命令运行器
def libros(): with open("p_scrap/libros_V2.json", "r") as f: d = json.loads(f.read()) for book in d: if book != "null": reg = Book(title=d[book]["title"], author=d[book]["author"], isbn=d[book]["isbn"], asin=d[book]["asin"], publisher=d[book]["publisher"], published=d[book]["published"], url=d[book]["url"], image=d[book]["image"], ean=d[book]["ean"]) for node in d[book]["nodes"]: n = Node.query.filter_by(pk=node).first() if n: reg.nodes.append(n) db.session.add(reg) db.session.commit() print("Done " + d[book]["title"])
def add_book(isbn, course, amz_info, amz_image): # on the rare occasion amazon doesn't have a book if isinstance(amz_info, basestring): amz_info = get_chegg_info(isbn) print 'chegged' # if chegg also doesn't have it, have to manually update if isinstance(amz_info, basestring): amz_info = {'url': None, 'title': None, 'authors': None} print 'not in chegg either' authors = amz_info['authors'] if authors is not None: authors = list(set(authors)) b = Book(isbn=isbn, title=amz_info['title'], author=authors, amazon_url=amz_info['url'], image=amz_image, courses=[course]) db.session.add(b) db.session.commit()
def book_public_json(): books = Book.get_public() return jsonify({"books": [{"id": b.id, "text": b.author + " - " + b.name} for b in books]})
def make_entity(self, data): return Book(**data)
def public(): books = Book.get_public() return render_template('publiclib.html', books=books)
from app import Reader, Book, Review b1 = Book(id=123, title='Demian', author_name="Hermann", author_surname='Hesse', month="February", year=2020) r1 = Reader(id=342, name='Ann', surname='Adams', email='*****@*****.**') print("My first reader:", r1.name) #Checkpoint 1: b2 = Book(id=533, title='The Stranger', author_name='Albert', author_surname='Camus', month='April', year=2019) #Checkpoint 2: r2 = Reader(id=765, name='Sam', surname='Adams', email='*****@*****.**') #Checkpoint 3: print(b2.author_surname) #Checkpoint 4: print(len(r2.email)) # Review entry rev2 = Review(id=450, text="This book is difficult!", stars=2, reviewer_id=r2.id, book_id=b2.id)
def admin_public(): books = Book.get_public() return render_template('/admin/publiclib.html', books=books)
r5 = Reader(id=753, name='Nova', surname='Yeni', email='*****@*****.**') r6 = Reader(id=653, name='Tom', surname='Grey', email='*****@*****.**') db.session.add(r1) db.session.add(r2) db.session.add(r3) db.session.add(r4) db.session.add(r5) db.session.add(r6) try: db.session.commit() except Exception: db.session.rollback() b1 = Book(id=12, title='Hundred years of solitude', author_name='Gabriel', author_surname='Garcia Marquez', month='April', year='2020') b2 = Book(id=13, title='The Stranger', author_name='Albert', author_surname='Camus', month='May', year='2020') b3 = Book(id=14, title='The Book of Why', author_name='Judea', author_surname='Pearl', month='September', year='2019') b4 = Book(id=18,
def main(): user_books = current_user.get_books() owned_books = Book.get_by_login(current_user.login) user_books += owned_books last_added_books = Book.get_last(10) return render_template('main.html', user_books=user_books, last_added_books=last_added_books)
from app import db, Book libro = Book("El Quijote", "Miguel de Cervantes", "1234-5678-9012", "15", "Julio de la Cruz", "787-123-4567", "*****@*****.**", "El libro esta en perfectas condiciones.") db.session.add(libro) db.session.commit()