Пример #1
0
    def wrapper(*args, **kwargs):
        region = None
        if 'region' in kwargs:
            region = kwargs['region']
        elif 'lon' in kwargs and 'lat' in kwargs:
            try:
                region = i_manager.key_of_coord(lon=kwargs['lon'],
                                                lat=kwargs['lat'])
            except RegionNotFound:
                pass
        elif 'from' in request.args:
            #used for journeys api
            try:
                region = i_manager.key_of_id(request.args['from'])
                if 'to' in request.args:
                    region_to = i_manager.key_of_id(request.args['to'])
                    if region != region_to:
                        abort(503,
                              message="Unable to compute journeys "
                              "between to different instances (%s, %s) " %
                              (region, region_to))
            except RegionNotFound:
                pass

        if not region or authenticate(region, 'ALL', abort=True):
            return func(*args, **kwargs)
Пример #2
0
    def get(self, region=None, lon=None, lat=None, uri=None):
        args = self.parsers['get'].parse_args()
        # TODO : Changer le protobuff pour que ce soit propre
        if args['destination_mode'] == 'vls':
            args['destination_mode'] = 'bss'
        if args['origin_mode'] == 'vls':
            args['origin_mode'] = 'bss'

        #count override min_nb_journey or max_nb_journey
        if 'count' in args and args['count']:
            args['min_nb_journeys'] = args['count']
            args['max_nb_journeys'] = args['count']

        # for last and first section mode retrocompatibility
        if 'first_section_mode' in args and args['first_section_mode']:
            args['origin_mode'] = args['first_section_mode']
        if 'last_section_mode' in args and args['last_section_mode']:
            args['destination_mode'] = args['last_section_mode']

        if region or (lon and lat):
            self.region = i_manager.get_region(region, lon, lat)
            if uri:
                objects = uri.split('/')
                if objects and len(objects) % 2 == 0:
                    args['origin'] = objects[-1]
                else:
                    abort(503,
                          message="Unable to compute journeys "
                          "from this object")
        else:
            if args['origin']:
                self.region = i_manager.key_of_id(args['origin'])
            elif args['destination']:
                self.region = i_manager.key_of_id(args['destination'])
            # else:
            #    raise RegionNotFound("")

        #we transform the origin/destination url to add information
        if args['origin']:
            args['origin'] = self.transform_id(args['origin'])
        if args['destination']:
            args['destination'] = self.transform_id(args['destination'])

        if not args['datetime']:
            args['datetime'] = datetime.now().strftime('%Y%m%dT1337')
        api = None
        if args['destination']:
            api = 'journeys'
        else:
            api = 'isochrone'

        if not args["origin"]:
            abort(400, message="from argument is required")

        response = i_manager.dispatch(args, api, instance_name=self.region)
        return response
Пример #3
0
def compute_regions(args):
    """
    method computing the region the journey has to be computed on
    The complexity comes from the fact that the regions in jormungandr can overlap.

    return the kraken instance key

    rules are easy:
    we fetch the different regions the user can use for 'origin' and 'destination'
    we do the intersection and sort the list
    """
    _region = None
    possible_regions = set()
    from_regions = set()
    to_regions = set()
    if args['origin']:
        from_regions = set(i_manager.key_of_id(args['origin'], only_one=False))
        #Note: if the key_of_id does not find any region, it raises a RegionNotFoundException

    if args['destination']:
        to_regions = set(
            i_manager.key_of_id(args['destination'], only_one=False))

    if not from_regions:
        #we didn't get any origin, the region is in the destination's list
        possible_regions = to_regions
    elif not to_regions:
        #we didn't get any origin, the region is in the destination's list
        possible_regions = from_regions
    else:
        #we need the intersection set
        possible_regions = from_regions.intersection(to_regions)

    logging.debug("orig region = {o}, dest region = {d} => set = {p}".format(
        o=from_regions, d=to_regions, p=possible_regions))

    if not possible_regions:
        raise RegionNotFound(
            custom_msg="cannot find a region with {o} and {d} in the same time"
            .format(o=args['origin'], d=args['destination']))

    sorted_regions = list(possible_regions)

    _region = choose_best_instance(sorted_regions)

    return _region
Пример #4
0
def Redirect(*args, **kwargs):
    id = kwargs["id"]
    collection = kwargs["collection"]
    region = i_manager.key_of_id(id)
    if not region:
        region = "{region.id}"
    url = url_for("v1.uri", region=region, collection=collection, id=id)
    return redirect(url, 303)
Пример #5
0
 def get(self, region=None):
     args = self.parsers["get"].parse_args()
     if region is None:
         region = i_manager.key_of_id(args["origin"])
     response = i_manager.dispatch(args, "isochrone", instance_name=region)
     if response.journeys:
         (before, after) = extremes(response, request)
         if before and after:
             response.prev = before
             response.next = after
     return protobuf_to_dict(response, use_enum_labels=True), 200
Пример #6
0
    def get(self, region=None):
        args = self.parsers["get"].parse_args()

        #default value for compatibility with v1
        args["min_nb_journeys"] = None
        args["max_nb_journeys"] = None
        args["show_codes"] = False

        if region is None:
            region = i_manager.key_of_id(args["origin"])
        response = i_manager.dispatch(args, "journeys", instance_name=region)
        if response.journeys:
            (before, after) = extremes(response, request)
            if before and after:
                response.prev = before
                response.next = after

        return protobuf_to_dict(response, use_enum_labels=True), 200
Пример #7
0
    def get(self, uri=None, region=None, lon=None, lat=None):
        args = self.parsers["get"].parse_args()
        args["nb_stoptimes"] = args["count"]
        args["interface_version"] = 1
        if uri is None:
            first_filter = args["filter"].lower().split("and")[0].strip()
            parts = first_filter.lower().split("=")
            if len(parts) != 2:
                error = "Unable to parse filter {filter}"
                return {"error": error.format(filter=args["filter"])}, 503
            else:
                self.region = i_manager.key_of_id(parts[1].strip())
        else:
            self.collection = 'schedules'
            args["filter"] = self.get_filter(uri.split("/"))
            self.region = i_manager.get_region(region, lon, lat)
        if not args["from_datetime"]:
            args["from_datetime"] = datetime.now().strftime("%Y%m%dT1337")

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