def edit_item(current_user, bucket_id, item_id):
"""
Edit an item with a valid Id. The request content-type must be json and also the Bucket
in which the item belongs must be among the user`s Buckets.
The name of the item must be present in the payload but the description is optional.
:param current_user: User
:param bucket_id: Bucket Id
:param item_id: Item Id
:return: Response of Edit Item
"""
if not request.content_type == 'application/json':
return response('failed', 'Content-type must be application/json', 401)
try:
int(item_id)
except ValueError:
return response('failed', 'Provide a valid item Id', 202)
# Get the user Bucket
bucket = get_user_bucket(current_user, bucket_id)
if bucket is None:
return response('failed', 'User has no Bucket with Id ' + bucket_id, 202)
# Get the item
item = bucket.items.filter_by(id=item_id).first()
if not item:
abort(404)
# Check for Json data
request_json_data = request.get_json()
item_new_name = request_json_data.get('name')
item_new_description = request_json_data.get('description', None)
if not request_json_data:
return response('failed', 'No attributes specified in the request', 401)
if not item_new_name:
return response('failed', 'No name or value attribute found', 401)
# Update the item record
item.update(item_new_name, item_new_description)
return response_with_bucket_item('success', item, 200)
评论列表
文章目录