class PlanRandomizeResource(Resource): def __init__(self): self.plan_repository = PlanRepository() @authenticate def post(self, id, **kwargs): user = kwargs['user'] plan = query(self.plan_repository.get(user=user, id=id) ).single_or_default(default=None) if plan is None: abort(404, message="No plan with that id was found") self.plan_repository.clear_plan(plan) populate_sample_plan(plan) result = self.plan_repository.add_or_update(plan) if not result.success: on_error(error_message="Could not randomize plan", result=result) self.plan_repository.save_changes() polyline = get_polyline(plan) plan_dict = plan.as_dict() plan_dict['polylines'] = polyline return plan_dict
class ItemListResource(Resource): def __init__(self): self.plan_repository = PlanRepository() self.item_repository = ItemRepository() self.yelp_category_repository = YelpCategoryRepository() super(ItemListResource, self).__init__() @authenticate def get(self, plan_id, **kwargs): user = kwargs['user'] plan = query(self.plan_repository.get(user_id=user.id, id=plan_id)).single_or_default( default=None) if not plan: abort(404, message="This plan does not exist") return plan.items @authenticate def post(self, plan_id, **kwargs): user = kwargs['user'] json = request.json item = Item.from_json(json) plan = query(self.plan_repository.get(user_id=user.id, id=plan_id)).single_or_default( default=None) if not plan: abort(404, message="This plan does not exist") result = plan.add_item(item) if not result.success(): on_error(error_message="Could not create item for plan", result=result) result = self.plan_repository.add_or_update(plan) if not result.success(): on_error(error_message="Could not create item for plan", result=result) self.plan_repository.save_changes() return item
class PlanResource(Resource): def __init__(self): self.update_parser = RequestParser() self.update_parser.add_argument( 'name', type=str, required=False, location='json', help='No name provided') self.update_parser.add_argument('start_time', type=str, required=False, location='json', help='No start_time provided') self.update_parser.add_argument('end_time', type=str, required=False, location='json', help='No end_time provided') self.update_parser.add_argument('public', type=bool, required=False, location='json', help='No publicity provided') self.plan_repository = PlanRepository() super(PlanResource, self).__init__() # Get an plan by id @authenticate def get(self, id, **kwargs): user = kwargs['user'] plan = self.plan_repository.find(id) if not plan: abort(404, message="No plan with that id exists") if plan.user.id != user.id and not query(plan.shared_users).contains(user, lambda lhs, rhs: lhs.id == rhs.id): abort(404, message="No plan with that id exists") if 'include_polyline' in request.args and request.args['include_polyline']: polyline = get_polyline(plan) plan_dict = plan.as_dict() plan_dict['polylines'] = polyline return plan_dict return plan # Update an plan by id @authenticate def put(self, id, **kwargs): user = kwargs['user'] args = self.update_parser.parse_args() plan = self.plan_repository.find(id) if not plan: abort(404, message="No plan with that id exists") if plan.user.id != user.id: permission = self.plan_repository.get_shared_user_permission( id, user.id) if not permission: abort(404, message="No plan with that id exists") elif permission == 'READ': abort(403, message="This user does not have edit permissions") plan.update_from_dict(args) result = self.plan_repository.add_or_update(plan) if not result.success(): on_error(error_message="Could not update plan", result=result) self.plan_repository.save_changes() return plan @authenticate def delete(self, id, **kwargs): user = kwargs['user'] plan = self.plan_repository.find(id) if not plan: abort(404, message="No plan with that id exists") if plan.user.id != user.id: abort(404, message="No plan with that id exists") self.plan_repository.delete(id) self.plan_repository.save_changes() return {"message": "Deleted plan"}
class PlanListResource(Resource): def __init__(self): self.get_parser = RequestParser() self.get_parser.add_argument('shared', type=bool, required=False) self.create_parser = RequestParser() self.create_parser.add_argument( 'name', type=str, required=True, location='json', help='No name provided') self.create_parser.add_argument('start_time', type=str, required=True, location='json', help='No start_time provided') self.create_parser.add_argument('end_time', type=str, required=True, location='json', help='No end_time provided') self.create_parser.add_argument( 'starting_address', type=str, required=True, location='json', help='No starting address provided') self.create_parser.add_argument( 'starting_coordinate', type=str, required=True, location='json', help='No starting coordinate provided') self.create_parser.add_argument('public', type=bool, required=True, location='json', help='No publicity provided') self.plan_repository = PlanRepository() self.item_repository = ItemRepository() super(PlanListResource, self).__init__() # List all plans @authenticate def get(self, **kwargs): try: user = kwargs['user'] filter_args = request.args.to_dict() if 'shared' in filter_args: filter_args['shared'] = bool(filter_args['shared']) plans = self.plan_repository.get(user=user, **filter_args) return plans except InvalidRequestError as ireq: on_error(str(ireq)) # Create a new plan @authenticate def post(self, **kwargs): user = kwargs['user'] json = request.json if 'items' not in json or len(json['items']) == 0: on_error(error_message="No items were provided") plan = Plan.from_json(json, user) items = self.item_repository.from_list(json['items']) if plan.starting_coordinate is not None: plan.city = get_city(plan.starting_coordinate.latitude, plan.starting_coordinate.longitude) if plan.starting_address is None: plan.set_start_item(items[0]) result = self.plan_repository.add_or_update(plan) if not result.success(): on_error(error_message="Could not create plan", result=result) for item in items: result.add_child_result(plan.add_item(item)) if not result.success(): on_error(error_message="Could not add items to plan", result=result) self.plan_repository.save_changes() return plan