def put(self, _id): # Verify current user is authorized on this route if not session.get('check'): return 401 else: req = request.get_json(silent=True) name, price, desc = (req[i] for i in req) item = ItemModel.query.get(_id) # Creates new item if none exists if not item: new_item = ItemModel(name, price, description) db.session.add(new_item) else: # Create item dict item.name = name item.price = price item.description = description # Commit changes to postgres db.session.commit() return {'msg': f'Item {item.name} was successfully updated.'}, 201
def post(): new_items = [] items = ItemList.parser.parse_args()['items'] if items: for item in items: abort_if_item_exist(item['name']) store_id = item['store_id'] if StoreModel.query.filter_by(id=store_id).all(): try: price = int(item['price']) except: return abort(404, message='price parameter must be integer') else: abort(404, message="store with id={} doesn't exist".format( store_id)) for item in items: new_item = { 'name': item['name'], 'price': item['price'], 'store_id': item['store_id'] } new_item = ItemModel(**new_item) new_item.add_item() new_items.append(new_item.json()) return {'new_items': new_items}, 201 return {"message": "items list mustn't be empty"}, 404
def post(self): parser = reqparse.RequestParser() parser.add_argument('name', type=str, required=True, help="This field cannot be left blank!") data = parser.parse_args() user_token, error, code = validate_token(request.headers) if error: return error, code token = TokenModel.find_by_token(user_token) if not token: return {"message": "Token is incorrect"}, 403 if data['name'] == "": return {'message': 'The name of item cannot be left blank!'}, 400 if ItemModel.find_by_name(data['name'], token.user_id): return { 'message': "An item with name '{}' already exists.".format(data['name']) }, 400 user_id = token.user_id item = ItemModel(data['name'], user_id) try: item.save_to_db() except: return {'message': 'An error occurred inserting the item.'}, 400 return item.json(), 201 # for created
def post(self, name): if ItemModel.get_item(name): return {'message': f"item '{name}' already exists"}, 400 price = ItemResource.parser.parse_args()['price'] item = ItemModel(name=name, price=price) item.add_item() return {'name': name, 'price': price}, 201
def put(self, name): price = ItemResource.parser.parse_args()['price'] if not ItemModel.get_item(name): item = ItemModel(name=name, price=price) item.add_item() return {'name': name, 'price': price}, 201 ItemModel.change_item(name, price) return {'name': name, 'price': price}, 200
def put(self, name): request_item_price = Item.parser.parse_args() item = ItemModel.find_by_name(name) if item: updated_item = ItemModel(name, **request_item_price) try: updated_item.save() return item.json() except: return {"massage": "updating item {} was not successful".format(name)}, 500 else: try: item = ItemModel(name, **request_item_price) item.save() return item.json() except: return {"massage": "inserting item {} was not successful".format(name)}, 500
def put(self, name): data = Item.parser.parse_args() item = ItemModel.find_by_name(name) if ItemModel.find_by_name(name): item.price = data['price'] else: item = ItemModel(name, **data) item.save_to_db() return item.json()
def put(self, name): data = Item.parser.parse_args() item = ItemModel.find_by_name(name) if item: item.quantity = data['quantity'] else: item = ItemModel(name, **data) item.save_to_db() return item.json()
def put(self, name): data = Item.parser.parse_args() item = ItemModel.item_by_name(name) if item is None: item = ItemModel(name, data['price']) else: item.price = data['price'] item.save_to_db() return item.json()
def put(self, name): data = ItemResource.parser.parse_args() item = ItemModel.find_by_name(name) if item: item.price = data["price"] else: item = ItemModel(name, **data) item.insert() return item.to_json()
def put(self, name): data = Item.parser.parse_args() data = Item.parser.parse_args() item = ItemModel.find_by_name(name) if item is None: item = ItemModel(name, **data) else: item.price = data['price'] item.save_to_db()
def post(self, name): if ItemModel.find_by_name(name): return {'message' : "An item with name '{}' already exists!".format(name)}, 400 data = Item.parser.parse_args() item = ItemModel(name, **data) try: item.save_to_db() except: return {'message':'An error occurred inserting the item.'}, 500 #internal server error (not user fault, 400) return item.json(), 201
def post(self, name): if ItemModel.find_by_name(name): return {'Message': f"item with name '{name}' already exists."}, 400 data = Item.parser.parse_args() item = ItemModel(name, **data) try: item.save_to_db() except: return {'message': 'An error occurred inserting the item.'}, 500 return item.json(), 201
def post(self, name): if ItemModel.find_by_name(name): return {"massage": "item {} already exits.".format(name)}, 400 request_item_price = Item.parser.parse_args() item = ItemModel(name, **request_item_price) try: item.save() return item.json() except: return {"massage": "inserting item {} was not successful".format(name)}, 500
def post(self, name): if ItemModel.find_by_name(name): return {'message': f"item {name} already exists"}, 400 data = Item.parser.parse_args() item = ItemModel(name, data['price'], data['store_id']) try: item.save_to_db() except: return {'message': "An Error occurred while inserting"}, 500 return item.json(), 201
def post(self, name): if ItemModel.find_by_name(name) is not None: return { "message": "Item with name {} already exists".format(name) }, 400 data = ItemResource.parser.parse_args() item = ItemModel(name, **data) item.insert() return item.to_json(), 201
def post(self, name): if ItemModel.find_by_name(name): return Item.existing, 400 data = Item.parser.parse_args() item = ItemModel(name, **data) try: item.save_to_db() except: return Item.error, 500 return {'message': Item.created, 'item': item.json()}, 201
def post(name): abort_if_item_exist(name) price = Item.parser.parse_args()['price'] try: price = int(price) new_item = {'name': name, 'price': price} new_item = ItemModel(**new_item) new_item.add_item() return {'new_item': new_item.json()}, 201 except: abort(404, message='price parameter must be integer')
def post(self, name): if ItemModel.find_by_name(name): return {'message': "Item'{}' already exists.".format(name)} data = Item.parser.parse_args() item = ItemModel(name, **data) try: item.save_to_db() except Exception: return {"message": "Insertion error "}, 500 return item.json()
def put(self, name): data = Item.parser.parse_args() item = ItemModel.find_by_name(name) if not item: item = ItemModel(name, data['price'], data['store_id']) else: item.price = data['price'] item.store_id = data['store_id'] item.save_to_db() return item.json()
def post(self): try: identity = get_jwt_identity() kwargs = _parser.parse_args(strict=True) print(kwargs) ItemModel(**kwargs, user_id=identity).save() return {"message": "Success!"}, 201 except BadRequest as e: return {"error": "Bad Request"}, 400 except BaseException as e: return self.return_error(e)
def post(self, name): if ItemModel.find_by_name(name): return {"message":"Item {} already exists".format(name)}, 400 data = Item.parser.parse_args() item = ItemModel(name, data['price']) try: item.insert() except: return {"message": "An error occured while trying to insert the item into database"} return item.json(), 201
def post(self): data = parser.parse_args() item_name = data["item_name"] restaurant_name = data["restaurant_name"] restaurant = RestaurantModel.find_by_restaurant_name(restaurant_name) item = ItemModel(item_name, restaurant.id) try: item.save_to_db() except: return {"message": "An error occurred saving the item."}, 500 return item.json(), 201
def post(self,name): if ItemModel.find_by_name(name): return {'message' : 'Item already exists'} data = Item.parser.parse_args() item = ItemModel(name, data['price'], data['store_id']) try: item.savetodb() except: {'message' : 'An error occured'} return item.json()
def post(self, name): if ItemModel.find_by_name(name): return {'message': f'An item with name{name} already exists'}, 400 data = Item.parser.parse_args() new_item = ItemModel(name, data['price'], data['store_id']) try: new_item.save_to_db() except: return {'message': 'An error occured inserting the item'}, 500 return new_item.json(), 201
def put(self, name): req_data = Item.parser.parse_args() item = ItemModel.find_by_name(name) # Insert new into db if item not found if item: item.price = req_data['price'] # Update item price only if item found in db else: item = ItemModel(name, **req_data) item.save_to_db() item.store.update_item_count( ) # Everytime an item get's created, update item count return item.json()
def post(self): items = ItemListResource.parser.parse_args()['items'] errors = [] for item in items: if ItemModel.get_item(item['name']): errors.append(item['name']) if errors: return {'message': f'{errors} already exist'}, 400 for item in items: new_item = ItemModel(name=item['name'], price=item['price']) new_item.add_item() return {'items': items}, 201
def post(self, name): data = Item.parser.parse_args() item = ItemModel.find_by_name(name) if item: return "An item with the name {} already exists".format(name) new_item = { "name": name, "price": data['price'], "store_id": data['store_id'] } item = ItemModel(name, **data) item.save_to_db() return new_item
def put(self, name): data = Item.parser.parse_args() item = ItemModel.find_by_name(name) if item: item.price = data["price"] item.store_id = data["store_id"] else: item = ItemModel(name, **data) try: item.save_to_db() except: return {'message': 'An error occurred inserting the item.'}, 500 return item.json()
def post(self): items = ItemListResource.parser.parse_args()['items'] bad_item_names = [] for item in items: existing_item = ItemModel.get_item(item['name']) if existing_item: bad_item_names.append(existing_item.name) if bad_item_names: return {'message': f"items '{bad_item_names}' already exist"}, 400 for item in items: new_item = ItemModel(name=item.name, price=item['price']) new_item.add_item() return {'items': items}, 201