def post(self): try: args = self.reqparse.parse_args() category = args['categories'] author = args['authors'] del args['categories'] del args['authors'] book = Book(**args) if author: for a in author: authors = Author.query.filter_by(name=a).first() if authors is None: authors = Author(name=a) db.session.add(authors) db.session.commit() book.authors.append(authors) if category: for c in category: categories = Category.query.filter_by(name=c).first() if categories is None: categories = Category(name=c) db.session.add(categories) db.session.commit() book.categories.append(categories) if (args['user_id'] and args['organization_id']) or ( (not args['user_id'] and not args['organization_id'])): return {'message': 'Bad Request'}, log__(400, g.user) if args['user_id'] and (not User.query.get(args['user_id'])): return {'message': 'User not found'}, log__(404, g.user) if args['organization_id']: org = Organization.query.get(args['organization_id']) if args['organization_id'] and ( (org is None) or (g.user not in org.managers and g.user.admin == 0)): if org is None: return { 'message': 'Organization not found' }, log__(404, g.user) if g.user not in org.managers and g.user.admin == 0: return { 'message': 'You are not authorized to access this area.' }, log__(401, g.user) elif args['user_id']: book.is_organization = False user = User.query.get_or_404(args['user_id']) user.points_update(10) else: book.is_organization = True db.session.add(book) db.session.commit() return {'data': book.serialize}, log__(201, g.user) except Exception as error: print(error) return {'message': 'Bad Request'}, log__(400, g.user)
def criar_livro(): data = request.get_json() livro = Book(nome_book=data['nome_book'], genero=data['genero'], autor=data['autor'], ano=data['ano']) db.session.add(livro) db.session.commit() return jsonify({'message': 'Livro criado com sucesso!'})
def novolivro(): form = LivroForm() if form.validate_on_submit(): f = Book(title=form.title.data, autor=form.autor.data, datainicio=form.datainicio.data, datafim=form.datafim.data) db.session.add(f) db.session.commit() return redirect(url_for("estante")) return render_template("cadastrolivro.html", form=form)
def cadbook(): if request.form: book = Book(title=request.form.get("title"), number_of_pages=request.form.get("numberpages"), rate=request.form.get("rate")) db.session.add(book) db.session.commit() flash('Cadastro efetuado com sucesso') return redirect(url_for('cadbook')) return render_template("cadbook.html")
def new_book(): form = NewBookForm() if form.validate_on_submit(): b = Book(title=form.title.data, author=form.author.data, publisher=form.publisher.data, gender=form.gender.data) db.session.add(b) db.session.commit() flash(u'New book added!') return redirect(url_for('search')) elif form.errors: flash(u'Invalid submit. Please, try again!') return render_template("books/new_book.html", form=form)
def post_livro(): filename = photos.save(request.files['imagem']) cropImge(filename) livro = Book(request.form['nome_book'], request.form['descricao'], request.form['genero'], request.form['autor'], request.form['ano'], foto=filename) db.session.add(livro) db.session.commit() flash('Cadastrado com sucesso!') return redirect(url_for('index.index'))