示例#1
0
文件: api.py 项目: abettadapur/Planr
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
示例#2
0
文件: api.py 项目: abettadapur/Planr
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
示例#3
0
文件: api.py 项目: abettadapur/Planr
class PlanShareResource(Resource):
    def __init__(self):
        self.plan_repository = PlanRepository()
        super(PlanShareResource, self).__init__()

    @authenticate
    def post(self, plan_id, **kwargs):
        plan = query(self.plan_repository.get(user_id=kwargs['user'].id, id=plan_id)).single_or_default(
            default=None)
        if not plan:
            abort(404, message="This plan does not exist")

        post_body = request.json
        if type(post_body) is not list:
            on_error(error_message="Invalid post body")

        result = ChangeResult()
        try:
            shares = query(post_body)
            for shared_user in plan.shared_users:
                if not shares.contains(shared_user, lambda lhs, rhs: rhs['user_id'] == lhs.id):
                    result.add_child_result(
                        self.plan_repository.unshare(plan, shared_user.id))

            for share in post_body:
                user_id = share['user_id']
                permission = share['permission']
                result.add_child_result(
                    self.plan_repository.share(plan, user_id, permission))

        except KeyError as ke:
            on_error(error_message="Invalid post body")

        if not result.success():
            on_error(error_message="Could not share plan", result=result)

        self.plan_repository.save_changes()
        return plan
示例#4
0
文件: api.py 项目: abettadapur/Planr
class ItemResource(Resource):
    def __init__(self):
        self.plan_repository = PlanRepository()
        self.item_repository = ItemRepository()
        self.category_repository = YelpCategoryRepository()
        super(ItemResource, self).__init__()

    @authenticate
    def get(self, plan_id, item_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")
        item = query(plan.items).where(lambda i: i.id ==
                                                 item_id).single_or_default(default=None)
        if not item:
            abort(404, message="This item does not exist")
        return item

    @authenticate
    def put(self, plan_id, item_id, **kwargs):
        user = kwargs['user']
        json = request.json
        new_item = Item.from_json(json)

        if new_item.id != item_id:
            on_error(error_message="Could not update item, ids do not match")

        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")

        old_item = query(plan.items).where(lambda i: i.id ==
                                                 item_id).single_or_default(default=None)
        if not old_item:
            abort(404, message="This item does not exist")

        result = self.item_repository.add_or_update(new_item)

        if not result.success():
            on_error(error_message="Could not update item", result=result)

        self.item_repository.save_changes()
        return new_item

    @authenticate
    def delete(self, plan_id, item_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")

        item = query(plan.items).where(lambda i: i.id ==
                                                 item_id).single_or_default(default=None)
        if not item:
            abort(404, message="This item does not exist")

        plan.remove_item(item)
        self.item_repository.delete(id)
        self.item_repository.save_changes()
        return {"message": "Deleted item"}
示例#5
0
文件: api.py 项目: abettadapur/Planr
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