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 post(self): '''POST method to create new bucketlist.''' args = self.reqparse.parse_args() bucketname = args['bucket_name'] bucket_list = BucketList.query.filter_by(bucket_name=bucketname,\ created_by=g.user.user_id).first() if bucket_list: responseObject = { 'status': 'fail', 'message': 'Bucket list already exists.' } return responseObject, 409 try: bucketlist = BucketList(bucket_name=bucketname, created_by=g.user.user_id) db.session.add(bucketlist) db.session.commit() return marshal(bucketlist, bucketlist_fields), 200 except: responseObject = { 'status': 'fail', 'message': 'An error occured. Try again.' } return responseObject, 500
def post(self): name = request.form.get("name") token = request.headers.get('Token') current_user = get_current_user_id(token) bucketlist = BucketList.query.filter_by( name=name, created_by=current_user).first() if name: if bucketlist: return messages["bucketlist_exist"] else: bucketlist = BucketList(name, current_user) return jsonify({"message": "Saved", "name": name, "created_by": current_user }) if bucketlist.save() \ else messages["bucketlist_not_saved"] else: return messages["no_bucketlist_name"]
def post(self): """Create a new bucketlist.""" parser.add_argument('list_name', help='List name cannot be blank') arg = parser.parse_args() if arg['list_name']: list_name = arg['list_name'] else: return {'message': 'Invalid value passed.'} created_by = current_user.user_id bucketlistexist = session.query(BucketList).filter_by( list_name=list_name, creator=created_by).first() if bucketlistexist: return {'message': 'Bucketlist already exists'}, 400 else: bucketlist = BucketList(list_name=list_name, creator=created_by) session.add(bucketlist) session.commit() return marshal(bucketlist, bucketlists), 201
def post(self, id=None): """ Method to create new bucketlists """ if id: abort(400, 'The requested url is not valid') parser = reqparse.RequestParser() parser.add_argument('name', type=str, help='A name is required') parser.add_argument('description', type=str, default='') args = parser.parse_args() name = args.get("name") description = args["description"] # set parsed items to an object of model class b_list = BucketList(name=name, description=description, created_by=g.user.id) if not name: response = jsonify( {'message': 'Please provide a name for the bucketlist'}) response.status_code = 400 return response # check for similar name on bucketlists created by current user bucks = BucketList.query.filter_by(name=name, created_by=g.user.id).first() if bucks: response = jsonify( {'message': 'You already have a bucketlist with that name'}) response.status_code = 400 return response try: db.session.add(b_list) db.session.commit() message = {'message': 'Bucket List updated Successfully'} response = marshal(b_list, bucketlists_serializer) response.update(message) response.status_code = 201 return response except Exception: response = jsonify( {'message': 'There was an error saving the bucketlist'}) response.status_code = 400 return response
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