def update_a_list(list_id): """ This endpoint will update a list of with a given id :param list_id: :return: json response """ data = request.json user_id = models.User.decode_token(utility.get_token()) the_list = models.List.query.filter_by(list_id=list_id, user_id=user_id).first() if the_list is not None and 'title' in data: try: description = data['description'] except Exception as ex: shoplist_api.logger.warning(ex.message) description = "" # the_list.list_name = data['title'] the_list.description = description the_list.update() shoplist_api.logger.debug("list with id: <%s> has been updated " % the_list.list_id) response = jsonify({'list': dict(id=the_list.list_id, title=the_list.list_name, description=the_list.description), 'status': 'pass', 'message': 'list updated'}) return response, 201 shoplist_api.logger.error("list with id: <%s> has not been updated " % list_id) return jsonify({'status': 'fail', 'message': 'list not updated'}), 400
def view_all_lists(): """ This endpoint will return all the lists for a logged in user and if the q parameter is provided, it will implement a search query based on the list name. Other parameters search as limit and page refine the results for the user of the API :return: """ user_id = models.User.decode_token(utility.get_token()) results = [] # query parameters q = request.args.get('q', None) # this parameter contains the name of the list limit = request.args.get('limit', 50) # limits the number of records to 50 per page (optional) page = request.args.get('page', 1) # page one is default, but page can be passed as an argument (optional) if q is not None: lists = models.List.query.filter( models.List.list_name.like("%" + q.strip() + "%")).\ filter_by(user_id=user_id).paginate(page, limit, False).items else: lists = models.List.query.filter_by(user_id=user_id).paginate(page, limit, False).items for a_list in lists: result = { 'id': a_list.list_id, 'title': a_list.list_name, 'description': a_list.description } results.append(result) if len(results) > 0: return jsonify({'lists': results, 'count': str(len(results)), 'status': 'pass', 'message': 'lists found'}), 200 return jsonify({'count': '0', 'status': 'fail', 'message': 'no lists found'}), 404
def add_a_list(): """ This endpoint will create a shopping list for a logged in user :return: json response """ data = request.json user_id = models.User.decode_token(utility.get_token()) if 'title' in data: try: description = data['description'] except Exception as ex: shoplist_api.logger.warning(ex.message) description = "" # create a list the_list = models.List(user_id=int(user_id), list_name=data['title'], description=description) the_list.add() shoplist_api.logger.debug("created {0} for user:<{1}>".format(the_list, the_list.user_id)) response = jsonify({'id': the_list.list_id, 'title': the_list.list_name, 'description': the_list.description, 'status': 'pass', 'message': 'list created successfully' }) return response, 201 shoplist_api.logger.error("title is missing in the data") return jsonify({'status': 'fail', 'message': 'title is missing in the data'}), 400
def delete_a_list(list_id): """ This endpoint will delete a list with a given id :param list_id: :return: json response """ user_id = models.User.decode_token(utility.get_token()) the_list = models.List.query.filter_by(list_id=list_id, user_id=user_id).first() if the_list is not None: the_list.delete() shoplist_api.logger.debug("list with id<%s> has been deleted " % list_id) return jsonify({'status': 'pass', 'message': 'list deleted'}), 200 return jsonify({'status': 'fail', 'message': 'list not deleted'}), 404
def update_user(): """ This endpoint will update user details such as firstname, lastname, description :return: json response """ data = request.json shoplist_api.logger.debug("/user: incoming request data %s " % data) # get the user id from the token user_id = models.User.decode_token(utility.get_token()) # locate the user whose details are to be updated user = models.User.query.filter_by(user_id=user_id).first() if user and isinstance(int(user_id), int): ''' Each field is in a try block of it's own to give the api user the ability to update a single field independent of the other fields in the User model/table ''' err_count = 0 # first name try: user.firstname = data['firstname'] except Exception as ex: user.firstname = "" err_count += 1 shoplist_api.logger.warning(ex.message) # last name try: user.lastname = data['lastname'] except Exception as ex: user.lastname = "" err_count += 1 shoplist_api.logger.warning(ex.message) # description try: user.description = data['description'] except Exception as ex: user.description = "" err_count += 1 shoplist_api.logger.warning(ex.message) # update the user if err_count == 3: # this means non of the fields was updated return jsonify({'status': 'fail', 'message': 'user not updated'}), 200 user.update() return jsonify({'status': 'pass', 'message': 'user updated'}), 201 shoplist_api.logger.error("user does't exist") return jsonify({'status': 'fail', 'message': 'user not found'}), 404
def get_a_list(list_id): """ This endpoint will return a list of a given id :param list_id: :return: json response """ user_id = models.User.decode_token(utility.get_token()) a_list = models.List.query.filter_by(list_id=list_id, user_id=user_id).first() if a_list is not None: shoplist_api.logger.debug("list %s found" % a_list) response = jsonify({'list': dict(id=a_list.list_id, title=a_list.list_name, description=a_list.description), 'count': '1', 'status': 'pass', 'message': 'list found'}) return response, 200 shoplist_api.logger.debug("list with id<%s> not found" % list_id) return jsonify({'count': '0', 'status': 'pass', 'message': 'list not found'}), 404
def get_user_details(): """ This endpoint will return details on a single user :return: json response """ user_id = models.User.decode_token(utility.get_token()) user = models.User.query.filter_by(user_id=user_id).first() lists = models.List.query.filter_by(user_id=user_id) num_of_items, num_of_lists = 0, 0 for a_list in lists: # count items on each list and total them up num_of_lists += 1 # increment number of lists by 1 on each iteration num_of_items += models.Item.query.filter_by(list_id=a_list.list_id).count() if user: return jsonify({'user': dict(id=user.user_id, username=user.email, firstname=user.firstname, lastname=user.lastname, description=user.description, num_of_lists=num_of_lists, num_of_items=num_of_items), 'status': 'pass', 'message': 'user found'}), 201 shoplist_api.logger.error("user does't exist") return jsonify({'status': 'fail', 'message': 'user not found'}), 404