def post(self): user_id = get_jwt_identity() body = request.get_json() user = User.objects.get(id=user_id) book = Book(**body, added_by=user) book.save() user.update(push__books=book) user.save() id = book.id return {'id': str(id)}, 200
def post(self): try: body = request.get_json() book = Book(**body) book.save() id = book.id return {'id': str(id)}, 200 except FieldDoesNotExist: raise SchemaValidationError except NotUniqueError: raise BookAlreadyExistsError
def post(self): try: user_id = get_jwt_identity() body = request.get_json() user = User.objects.get(id=user_id) book = Book(**body, added_by=user) book.save() user.update(push__books=book) user.save() id = book.id return {'id': str(id)}, 200 except (FieldDoesNotExist, ValidationError): raise SchemaValidationError except NotUniqueError: raise BookAlreadyExistsError except Exception: raise InternalServerError
def create_book(data): publisher_name = data.get('publisher') publisher = Publisher.query.filter(Publisher.name == publisher_name).one() book = Book(data.get('title'), data.get('author'), data.get('genre'), data.get('description'), data.get('price'), data.get('availability'), publisher) db.session.add(book) db.session.commit()
def put(self, req, resp, pk): req_body = req.json properties_to_update = Book.get_all_properties() if all(x in req_body for x in properties_to_update): book = Book.query.get(_id=ObjectId(pk)) for key, value in req_body.items(): setattr(book, key, value) resp.json = book.dictify()
def add_book(): if request.form.get('cancel'): return redirect(url_for('index')) form = generate_form(AddBookForm) if request.method == 'POST': if form.validate_on_submit(): book = Book() book.title = form.title.data book.author = form.author.data book.year = form.year.data try: db.session.add(book) db.session.commit() return redirect(url_for('get_books')) except Exception as error: msg = f"{datetime.now()} Error occurred adding {book.title}!\nError: {error}" logging.warning(msg=msg) db.session.rollback() form = generate_form(AddBookForm) flash(f'Error while adding {book.title}! Try again.') return render_template('add_book.html', form=form, user=current_user)
def get(self): books = Book.objects().to_json() return Response(books, mimetype="application/json", status=200)
def post(self, req, resp): b = Book(**req.json) resp.json = b.dictify()