def add_author(): if not session.get("logged"): abort(401) form = form_handler.AddAuthorForm(request.form) if request.method == "POST" and form.validate(): db_handler.add_author(form.name.data) flash("New author save successfully") return redirect(url_for('index')) return render_template("add_author.html", form=form)
def registration(): form = form_handler.RegistrationForm(request.form) if request.method == "POST" and form.validate(): db_handler.add_user(form.username.data, form.password.data) session["logged"] = True flash("You have successfully registered") return redirect(url_for('index')) else: for field in form: for error in field.errors: flash(error) return render_template('register.html', form=form)
def index(): form = form_handler.SearchBooksForm(request.form) form_valid = form.validate() if request.method == "POST" and form_valid: books = db_handler.get_books(form.search_by_title.data, form.search_by_author.data) else: books = db_handler.get_all_books() for field in form: for error in field.errors: flash(error) if not form_valid: form.search_by_title.data = "" form.search_by_author.data = "" return render_template("list_books.html", books = books, form=form)
def add_book(): if not session.get("logged"): abort(401) form = form_handler.AddBookForm(request.form) authors = db_handler.get_all_authors() form.choose_authors.choices = [(g.id, g.name) for g in authors] if request.method == "POST" and form.validate(): db_handler.add_book(form.title.data, form.choose_authors.data) flash("New book save successfully") return redirect(url_for('index')) else: for field in form: for error in field.errors: flash(error) return render_template("add_book.html", form=form)
def index(): form = form_handler.SearchBooksForm(request.form) form_valid = form.validate() if request.method == "POST" and form_valid: books = db_handler.get_books(form.search_by_title.data, form.search_by_author.data) else: books = db_handler.get_all_books() for field in form: for error in field.errors: flash(error) if not form_valid: form.search_by_title.data = "" form.search_by_author.data = "" return render_template("list_books.html", books=books, form=form)
def change_author(author_id): if not session.get("logged"): abort(401) if request.method != "POST": author = db_handler.get_author(author_id) form = form_handler.AddAuthorForm(name = author.name) else: form = form_handler.AddAuthorForm(request.form) if form.validate(): if db_handler.change_author(author_id, form.name.data): flash("Changes author ({0}) save successfully".format(form.name.data)) return redirect(url_for('index')) else: for field in form: for error in field.errors: flash(error) return render_template("change_author.html", form=form, id = author_id)
def change_author(author_id): if not session.get("logged"): abort(401) if request.method != "POST": author = db_handler.get_author(author_id) form = form_handler.AddAuthorForm(name=author.name) else: form = form_handler.AddAuthorForm(request.form) if form.validate(): if db_handler.change_author(author_id, form.name.data): flash("Changes author ({0}) save successfully".format( form.name.data)) return redirect(url_for('index')) else: for field in form: for error in field.errors: flash(error) return render_template("change_author.html", form=form, id=author_id)
def change_book(book_id): if not session.get("logged"): abort(401) if request.method != "POST": book = db_handler.get_book(book_id) list_author_id = [author.id for author in book.authors_by] form = form_handler.AddBookForm(title = book.title, choose_authors = list_author_id) else: form = form_handler.AddBookForm(request.form) authors = db_handler.get_all_authors() form.choose_authors.choices = [(g.id, g.name) for g in authors] if request.method == "POST" and form.validate(): db_handler.change_book(book_id, form.title.data, form.choose_authors.data) flash("Changes book ({0}) save successfully".format(form.title.data)) return redirect(url_for('index')) elif request.method == "POST": for field in form: for error in field.errors: flash(error) return render_template("change_book.html", form=form, id=book_id)
def change_book(book_id): if not session.get("logged"): abort(401) if request.method != "POST": book = db_handler.get_book(book_id) list_author_id = [author.id for author in book.authors_by] form = form_handler.AddBookForm(title=book.title, choose_authors=list_author_id) else: form = form_handler.AddBookForm(request.form) authors = db_handler.get_all_authors() form.choose_authors.choices = [(g.id, g.name) for g in authors] if request.method == "POST" and form.validate(): db_handler.change_book(book_id, form.title.data, form.choose_authors.data) flash("Changes book ({0}) save successfully".format(form.title.data)) return redirect(url_for('index')) elif request.method == "POST": for field in form: for error in field.errors: flash(error) return render_template("change_book.html", form=form, id=book_id)
def login(): form = form_handler.LoginForm(request.form) if request.method == "POST" and form.validate(): user = db_handler.get_user(form.username.data) if user: if form.password.data == user.password: session["logged"] = True flash("You was logged sucsesfully") return redirect(url_for('index')) else: flash("Wrong password") else: flash("Wrong username") else: for field in form: for error in field.errors: flash(error) return render_template("login.html", form=form)
def delete_author(author_id): if db_handler.delete_author(author_id): flash('The author successfully removed') return redirect(url_for('index'))
def delete_book(book_id): if db_handler.delete_book(book_id): flash('The book successfully removed') return redirect(url_for('index'))
flash("Wrong username") else: for field in form: for error in field.errors: flash(error) return render_template("login.html", form=form) @app.route('/logout') def logout(): session.pop("logged", None) flash("You were logged out") return redirect(url_for('index')) @app.route('/registration', methods = ['GET', 'POST']) def registration(): form = form_handler.RegistrationForm(request.form) if request.method == "POST" and form.validate(): db_handler.add_user(form.username.data, form.password.data) session["logged"] = True flash("You have successfully registered") return redirect(url_for('index')) else: for field in form: for error in field.errors: flash(error) return render_template('register.html', form=form) if __name__ == "__main__": if not os.path.exists(app.config["DATABASE"]): db_handler.init_db(app.config["PATH_DB_SCHEMA"], app.config["DATABASE"], app) app.run()
flash(error) return render_template("login.html", form=form) @app.route('/logout') def logout(): session.pop("logged", None) flash("You were logged out") return redirect(url_for('index')) @app.route('/registration', methods=['GET', 'POST']) def registration(): form = form_handler.RegistrationForm(request.form) if request.method == "POST" and form.validate(): db_handler.add_user(form.username.data, form.password.data) session["logged"] = True flash("You have successfully registered") return redirect(url_for('index')) else: for field in form: for error in field.errors: flash(error) return render_template('register.html', form=form) if __name__ == "__main__": if not os.path.exists(app.config["DATABASE"]): db_handler.init_db(app.config["PATH_DB_SCHEMA"], app.config["DATABASE"], app) app.run()