示例#1
0
    def get(self, region=None, lon=None, lat=None, uri=None, id=None):
        collection = self.collection

        args = self.parsers["get"].parse_args()

        if "odt_level" in args and args[
                "odt_level"] != "all" and "lines" not in collection:
            abort(404,
                  message=
                  "bad request: odt_level filter can only be applied to lines")

        if region is None and lat is None and lon is None:
            if "external_code" in args and args["external_code"]:
                type_ = collections_to_resource_type[collection]
                for instance in i_manager.get_regions():
                    res = i_manager.instances[instance].has_external_code(
                        type_, args["external_code"])
                    if res:
                        region = instance
                        id = res
                        break
                if not region:
                    abort(404,
                          message="Unable to find an object for the uri %s" %
                          args["external_code"])
            else:
                abort(503, message="Not implemented yet")
        else:
            user = authentication.get_user(token=authentication.get_token())
            authentication.has_access(region, 'ALL', abort=True, user=user)
        self.region = i_manager.get_region(region, lon, lat)

        #we store the region in the 'g' object, which is local to a request
        set_request_timezone(self.region)

        if not self.region:
            return {"error": "No region"}, 404
        if collection and id:
            args["filter"] = collections_to_resource_type[collection] + ".uri="
            args["filter"] += '"' + id + '"'
        elif uri:
            if uri[-1] == "/":
                uri = uri[:-1]
            uris = uri.split("/")
            if collection is None:
                collection = uris[-1] if len(uris) % 2 != 0 else uris[-2]
            args["filter"] = self.get_filter(uris, args)
        #else:
        #    abort(503, message="Not implemented")
        response = i_manager.dispatch(args,
                                      collection,
                                      instance_name=self.region)
        return response
示例#2
0
 def _filter_authorized_instances(self, instances, api):
     if not instances:
         return None
     user = authentication.get_user(token=authentication.get_token())
     valid_instances = [i for i in instances
                        if authentication.has_access(i.name, abort=False, user=user, api=api)]
     if not valid_instances:
         authentication.abort_request(user)
     return valid_instances
示例#3
0
    def _filter_authorized_instances(self, instances, api):
        if not instances:
            return None
        # During the period database is not accessible, all the instances are valid for the user.
        if not can_connect_to_database():
            return instances

        user = authentication.get_user(token=authentication.get_token())
        valid_instances = [
            i for i in instances if authentication.has_access(
                i.name, abort=False, user=user, api=api)
        ]
        if not valid_instances:
            context = 'User has no access to any instance'
            authentication.abort_request(user, context)
        return valid_instances
示例#4
0
文件: Uri.py 项目: niko64fx/navitia
    def get(self, region=None, lon=None, lat=None, uri=None, id=None):
        collection = self.collection

        args = self.parsers["get"].parse_args()

        # handle headsign
        if args.get("headsign"):
            f = u"vehicle_journey.has_headsign({})".format(
                protect(args["headsign"]))
            if args.get("filter"):
                args["filter"] += " and " + f
            else:
                args["filter"] = f

        # for retrocompatibility purpose
        for forbid_id in args['__temporary_forbidden_id[]']:
            args['forbidden_uris[]'].append(forbid_id)

        if "odt_level" in args and args[
                "odt_level"] != "all" and "lines" not in collection:
            abort(404,
                  message=
                  "bad request: odt_level filter can only be applied to lines")

        if region is None and lat is None and lon is None:
            if "external_code" in args and args["external_code"]:
                type_ = collections_to_resource_type[collection]
                for instance in i_manager.get_regions():
                    res = i_manager.instances[instance].has_external_code(
                        type_, args["external_code"])
                    if res:
                        region = instance
                        id = res
                        break
                if not region:
                    abort(404,
                          message="Unable to find an object for the uri %s" %
                          args["external_code"])
            else:
                abort(503, message="Not implemented yet")
        else:
            user = authentication.get_user(token=authentication.get_token())
            authentication.has_access(region, 'ALL', abort=True, user=user)
        self.region = i_manager.get_region(region, lon, lat)

        #we store the region in the 'g' object, which is local to a request
        set_request_timezone(self.region)

        # change dt to utc
        if args['since']:
            args['_original_since'] = args['since']
            args['since'] = date_to_timestamp(
                self.convert_to_utc(args['since']))
        if args['until']:
            args['_original_until'] = args['until']
            args['until'] = date_to_timestamp(
                self.convert_to_utc(args['until']))

        if not self.region:
            return {"error": "No region"}, 404
        if uri:
            if uri[-1] == "/":
                uri = uri[:-1]
            uris = uri.split("/")
            if collection is None:
                collection = uris[-1] if len(uris) % 2 != 0 else uris[-2]
            args["filter"] = self.get_filter(uris, args)
        if collection and id:
            f = u'{o}.uri={v}'.format(
                o=collections_to_resource_type[collection], v=protect(id))
            if args.get("filter"):
                args["filter"] += " and " + f
            else:
                args["filter"] = f

        response = i_manager.dispatch(args,
                                      collection,
                                      instance_name=self.region)
        return response