def store_item(): """ Store the new requested item. """ # Get the category and check if it's exist or fail to 404 page if not category = Category.find_or_fail(request.form['category_id']) # Create new item item = Item() item.title = request.form['title'] item.description = request.form['description'] # Associate the new item with the category item.category().associate(category) # Associate the new item to the current user item.user().associate(current_user) # The below is for validate the unique slug, if it # was exists before, then the ORM will not fall to # the ModelNotFound exception and the code will # continue flashing the error message to the user. # Otherwise, if the slug is not exists, then the # request is completely valid and the ORM will # going to fall to the ModelNotFound exception # then save the new Item try: # Get the item and check if it's exist or fail to 404 page if not Item.where('slug', item.slug).first_or_fail() flash('The title seems similar to another' ' title, please choose another one.') return render_template('items/add.html', categories=Category.all(), current_category=category, item=item) except ModelNotFound: # Save the item item.save() return redirect( url_for('view_item', category_slug=category.slug, item_slug=item.slug))
def modify_item(): """ Endpoint to modify a new `Item` """ logger.info("Modify Item...") params = request.get_json() logger.debug(params) if not params: raise errors.ApiError(70001, "Missing required key params") # Verify needed key-values _needed_params = {'item_uuid'} if not _needed_params.issubset(params.keys()): raise errors.ApiError(70001, "Missing required key params") # Call to save Item _item = Item(params) _item.save() return jsonify({ "status": "OK", "message": _item.message, "item_uuid": _item.item_uuid })
def post(self, shopping_id=None): quantity = int(request.get_json().get('quantity', 0)) item_name = str(request.get_json().get('item_name', '')) if shopping_id != None and item_name != None and quantity != 0: item = Item(name=item_name) item.shopping_id = shopping_id item.quantity = quantity item.save() response = jsonify({ 'id': item.item_id, 'quantity': item.quantity, 'item_name': item.item_name, 'date_created': item.date_created }) response.status_code = 201 return response else: response = jsonify({ "error_code": 302, "error_message": "Missing parameters" }) response.status_code = 302 return response