def get(self, shoppinglist_id): user_id = middleware() if isinstance(user_id, int): shoppinglistitems = ShoppingListItem.query.filter_by( shoppinglist_id=shoppinglist_id, owner_id=user_id) results = [] for shoppinglistitem in shoppinglistitems: obj = { 'item_id': shoppinglistitem.item_id, 'item_title': shoppinglistitem.item_title, 'item_description': shoppinglistitem.item_description, 'shoppinglist_id': shoppinglistitem.shoppinglist_id, 'date_created': shoppinglistitem.date_created, 'date_modified': shoppinglistitem.date_modified, 'owner_id': shoppinglistitem.owner_id } results.append(obj) if len(results) == 0: response = { 'status': 'success', 'message': "You don't have any items for now" } return response, 202 response = jsonify(results) response.status_code = 202 return response else: return user_id
def delete(self, shoppinglist_id): user_id = middleware() if isinstance(user_id, int): shoppinglist = ShoppingList.query.filter_by( id=shoppinglist_id).first() if shoppinglist: shoppinglist.delete_shoppinglist() response = { 'status': 'success', 'message': 'Shopping List \'{}\' deleted successfuly'.format( shoppinglist.title) } return response, 201 response = { 'status': 'fail', 'message': 'Requested value \'{}\' was not found'.format( shoppinglist_id) } return response, 202 else: return user_id
def get(self): user_id = middleware() if isinstance(user_id, int): shoppinglists = ShoppingList.query.filter_by(owner_id=user_id) results = [] for shoppinglist in shoppinglists: obj = { 'id': shoppinglist.id, 'title': shoppinglist.title, 'description': shoppinglist.description, 'date_created': shoppinglist.date_created, 'date_modified': shoppinglist.date_modified, 'owner_id': shoppinglist.owner_id } results.append(obj) if len(results) == 0: response = { 'message': "You don't have any shoppinglists for now." } return response, 200 response = jsonify(results) response.status_code = 202 return response else: return user_id
def post(self, shoppinglist_id): user_id = middleware() if isinstance(user_id, int): post_data = ['item_title', 'item_description'] for arg in range(len(post_data)): parser.add_argument(post_data[arg]) args = parser.parse_args() item_title = args['item_title'] item_description = args['item_description'] valid_item_title = is_valid(value=item_title, min_length=10, _type="text") valid_item_description = is_valid(value=item_description, min_length=10, _type="text") if valid_item_title is True and valid_item_description is True: check_exists = ShoppingListItem.query.filter_by( item_title=item_title).first() if check_exists is None: shoppinglistitem = ShoppingListItem( item_title=item_title, item_description=item_description, shoppinglist_id=shoppinglist_id, owner_id=user_id) shoppinglistitem.save_shoppinglistitem() # Return Response response = { 'item_id': shoppinglistitem.item_id, 'owner_id': shoppinglistitem.owner_id, 'shoppinglist_id': shoppinglistitem.shoppinglist_id, 'item_title': shoppinglistitem.item_title, 'item_description': shoppinglistitem.item_description, 'message': 'Shopping list item {} created successfuly'.format( item_title) } return response, 201 response = { 'message': 'Shopping List item {} already exists'.format( item_title) } return response, 202 else: if valid_item_title is True: message = valid_item_description elif valid_item_description is True: message = valid_item_title else: message = [valid_item_title, valid_item_description] response = {'message': message, 'status': 'fail'} return response, 202 else: return user_id
def put(self, shoppinglist_id): user_id = middleware() if isinstance(user_id, int): post_data = ['title', 'description'] for arg in range(len(post_data)): parser.add_argument(post_data[arg]) args = parser.parse_args() title = args['title'] description = args['description'] valid_title = is_valid(value=title, min_length=10, _type="text") valid_description = is_valid(value=description, min_length=10, _type="text") if valid_title is True and valid_description is True: title_exists = ShoppingList.query.filter_by( title=title).first() if not title_exists: shoppinglist = ShoppingList.query.filter_by( owner_id=user_id, id=shoppinglist_id).first() shoppinglist.title = title shoppinglist.description = description shoppinglist.save_shoppinglist() # Return Response response = { 'id': shoppinglist.id, 'owner': shoppinglist.owner_id, 'title': shoppinglist.title, 'description': shoppinglist.description, 'message': 'Shopping List updated successfuly', 'status': 'success' } return response, 200 response = { 'status': "fail", 'message': 'Shopping List {} already exists'.format(title) } return response, 202 else: if valid_title is True: message = valid_description elif valid_description is True: message = valid_title else: message = [valid_title, valid_description] response = {'message': message, 'status': 'fail'} return response, 202 else: return user_id
def post(self): user_id = middleware() if isinstance(user_id, int): post_data = ['title', 'description'] for arg in range(len(post_data)): parser.add_argument(post_data[arg]) args = parser.parse_args() title = args['title'] description = args['description'] valid_title = is_valid(value=title, min_length=10, _type="text") valid_description = is_valid(value=description, min_length=10, _type="text") if valid_title is True and valid_description: check_exists = ShoppingList.query.filter_by( title=title).first() if check_exists is None: shoppinglist = ShoppingList(title=title, description=description, owner_id=user_id) shoppinglist.save_shoppinglist() # Return Response response = { 'id': shoppinglist.id, 'owner': shoppinglist.owner_id, 'title': shoppinglist.title, 'description': shoppinglist.description, 'message': 'Shopping List created successfuly' } return response, 201 response = { 'message': 'Shopping List {} already exists'.format(title) } return response, 202 response = {'message': valid_title} return response, 202 else: return user_id
def get(self, shoppinglist_id): user_id = middleware() if isinstance(user_id, int): shoppinglist = ShoppingList.query.filter_by( id=shoppinglist_id).first() if shoppinglist: response = { 'id': shoppinglist.id, 'owner': shoppinglist.owner_id, 'title': shoppinglist.title, 'description': shoppinglist.description, 'status': 'success' } return response, 201 response = { 'message': 'Requested value \'{}\' was not found'.format( shoppinglist_id) } return response, 202 else: return user_id
def delete(self, shoppinglist_id, shoppinglistitem_id): user_id = middleware() if isinstance(user_id, int): shoppinglistitem = ShoppingListItem.query.filter_by( item_id=shoppinglistitem_id, owner_id=user_id, shoppinglist_id=shoppinglist_id).first() if shoppinglistitem: shoppinglistitem.delete_shoppinglistitem() response = { 'message': 'Shopping list item \'{}\' deleted successfuly'.format( shoppinglistitem.item_title) } return response, 201 response = { 'message': 'Requested value \'{}\' was not found'.format( shoppinglistitem_id) } return response, 202 else: return user_id