def get_or_abort(model, object_id, code=404): """ get an object with his given id or an abort error (404 is the default) """ result = model.query.get(object_id) if result is None: abort(code) return result
def delete_one_product(id): product = db.session.query(Product).get(id) if product is None: abort(404) db.session.delete(product) db.session.commit() ########################################################## #TODO# Comment tester si le delete est OK ? # Est-il possible de récupérer le code retour du commit ? ########################################################## return '', 204
def update_one_product(id): data = request.get_json() # sample data input : { "name": "titi" } name = data.get('name') if data['name'] is None: abort(400) product = db.session.query(Product).get(id) if product is None: abort(404) product.name = name db.session.commit() ########################################################## #TODO# Comment tester si le patch est OK ? # Est-il possible de récupérer le code retour du commit ? ########################################################## return '', 204
def create_one_product(): product = Product() product.name = "Ajout via API create_one_product" db.session.add(product) db.session.commit() ########################################################## #TODO# Comment tester si le commit est OK ? # failed=False # try: # db_session.commit() # except Exception as e: # #log your exception in the way you want -> log to file, log as error with default logging, send by email. It's upon you # db_session.rollback() # db_session.flush() # for resetting non-commited .add() # failed=True #some use of failed var, specific for your case if you need it # Est-il possible de récupérer le code retour du commit ? ########################################################## if product is None: abort(422) return product_schema.jsonify(product), 201
def read_one_product(id): product = db.session.query(Product).get(id) if product is None: abort(404) return product_schema.jsonify(product), 200