def bucketlist(): """Retrieves all bucketlists for a logged in user and returns a JSON response for (GET request) to /bucketlists. POST requests to this route creates a bucketlist for the logged in user. """ user_id = auth.get_current_user_id() if request.method == "GET": query = BucketList.query.filter_by(created_by=user_id) limit = request.args.get("limit", 20) q = request.args.get("q") page = request.args.get("page", 1) result_data = None if query.all(): if not 0 <= int(limit) <= 100: raise NotAcceptable("Maximum limit per page is 100.") else: result_data = query if q: result_data = query.filter(BucketList.name.ilike("%{0}%".format(q))) result_data = list_object_transform(result_data.paginate(page, int(limit), False).items) return {"message": result_data} raise NotFound("There are no bucketlist for this user") else: name = request.form.get("name") a_bucketlist = BucketList(created_by=user_id, name=name) a_bucketlist.save() return {"message": "Bucketlist was created successfully", "bucketlist": a_bucketlist.to_json()}, 201
def bucketlist(): '''Retrieves all bucketlists for a logged in user and returns a JSON response for (GET request) to /bucketlists. POST requests to this route creates a bucketlist for the logged in user. ''' user_id = auth.get_current_user_id() if request.method == "GET": query = BucketList.query.filter_by(created_by=user_id) limit = request.args.get('limit', 20) q = request.args.get('q') page = request.args.get('page', 1) result_data = None if query.all(): if not 0 <= int(limit) <= 100: raise NotAcceptable('Maximum limit per page is 100.') else: result_data = query if q: result_data = query.filter( BucketList.name.ilike('%{0}%'.format(q))) result_data = list_object_transform( result_data.paginate(page, int(limit), False).items) return {'message': result_data} raise NotFound('There are no bucketlist for this user') else: name = request.form.get("name") a_bucketlist = BucketList(created_by=user_id, name=name) a_bucketlist.save() return { "message": "Bucketlist was created successfully", "bucketlist": a_bucketlist.to_json() }, 201
def bucketlist(): """ Return a JSON response with all bucketlists """ user_id = auth.get_current_user() results_data = None if request.method == 'GET': # pagination limit results = BucketList.get_all(user_id) limit = request.args.get('limit', 20) q = request.args.get('q') page = request.args.get('page', 1) if page < 1: raise NotFound('Please specify a valid page (positive number)') if results.all(): results_data = results limit = 100 if int(limit) > 100 else limit if q: results_data = results.filter( BucketList.name.ilike('%{0}%'.format(q))) # serialize result objects to json result_list = [] for item in results_data.paginate( page, int(limit), False).items: if callable(getattr(item, 'to_json')): result = item.to_json() result_list.append(result) return {'message': result_list} raise NotFound('User has no bucketlist') else: name = request.form.get("name") bucketlist = BucketList(created_by=user_id, name=name) bucketlist.save() return { "message": "Bucketlist was created successfully", "bucketlist": bucketlist.to_json()}, 201