Beispiel #1
0
        def wrapper(*args, **kwargs):
            response, status, other = f(*args, **kwargs)
            api = "departures" if "departures" in response else "arrivals" if "arrivals" in response else None
            if not api:
                return response, status, other
            passages = response[api]

            max_dt = "19000101T000000"
            min_dt = "29991231T235959"
            time_field = "arrival_date_time" if api == "arrivals" else "departure_date_time"
            for passage_ in passages:
                dt = passage_["stop_date_time"][time_field]
                if min_dt > dt:
                    min_dt = dt
                if max_dt < dt:
                    max_dt = dt
            if "links" not in response:
                response["links"] = []
            kwargs_links = dict(deepcopy(request.args))
            if "region" in kwargs:
                kwargs_links["region"] = kwargs["region"]
            if "uri" in kwargs:
                kwargs_links["uri"] = kwargs["uri"]
            if 'from_datetime' in kwargs_links:
                kwargs_links.pop('from_datetime')
            delta = datetime.timedelta(seconds=1)
            dt = datetime.datetime.strptime(min_dt, "%Y%m%dT%H%M%S")
            if g.stat_interpreted_parameters.get('data_freshness') != 'realtime':
                kwargs_links['until_datetime'] = (dt - delta).strftime("%Y%m%dT%H%M%S")
                response["links"].append(create_external_link("v1."+api, rel="prev", _type=api, **kwargs_links))
                kwargs_links.pop('until_datetime')
                kwargs_links['from_datetime'] = (datetime.datetime.strptime(max_dt, "%Y%m%dT%H%M%S") + delta).strftime("%Y%m%dT%H%M%S")
                response["links"].append(create_external_link("v1."+api, rel="next", _type=api, **kwargs_links))
            return response, status, other
Beispiel #2
0
        def wrapper(*args, **kwargs):
            objects = f(*args, **kwargs)
            if objects[1] != 200 or 'journeys' not in objects[0]:
                return objects
            for journey in objects[0]['journeys']:
                args = dict(request.args)
                allowed_ids = {
                    o['stop_point']['id']
                    for s in journey.get('sections', []) if 'from' in s
                    for o in (s['from'], s['to']) if 'stop_point' in o
                }

                if 'region' in kwargs:
                    args['region'] = kwargs['region']
                if "sections" not in journey:  #this mean it's an isochrone...
                    if 'to' not in args:
                        args['to'] = journey['to']['id']
                    if 'from' not in args:
                        args['from'] = journey['from']['id']
                    args['rel'] = 'journeys'
                    journey['links'] = [
                        create_external_link('v1.journeys', **args)
                    ]
                elif allowed_ids and 'public_transport' in (
                        s['type'] for s in journey['sections']):
                    # exactly one first_section_mode
                    if any(s['type'].startswith('bss')
                           for s in journey['sections'][:2]):
                        args['first_section_mode[]'] = 'bss'
                    else:
                        args['first_section_mode[]'] = journey['sections'][
                            0].get('mode', 'walking')

                    # exactly one last_section_mode
                    if any(s['type'].startswith('bss')
                           for s in journey['sections'][-2:]):
                        args['last_section_mode[]'] = 'bss'
                    else:
                        args['last_section_mode[]'] = journey['sections'][
                            -1].get('mode', 'walking')

                    args['min_nb_transfers'] = journey['nb_transfers']
                    args['direct_path'] = 'only' if 'non_pt' in journey[
                        'tags'] else 'none'
                    args['min_nb_journeys'] = 5
                    args['is_journey_schedules'] = True
                    allowed_ids.update(args.get('allowed_id[]', []))
                    args['allowed_id[]'] = list(allowed_ids)
                    args['_type'] = 'journeys'
                    args['rel'] = 'same_journey_schedules'
                    journey['links'] = [
                        create_external_link('v1.journeys', **args)
                    ]
            return objects
Beispiel #3
0
 def get(self):
     response = {
         "links": [
             create_external_link(self.module_name + '.coverage',
                                  rel='coverage',
                                  description='Coverage of navitia'),
             # TODO find a way to display {long:lat} in the url
             create_external_link(self.module_name + '.coord', rel='coord',
                                  templated=True,
                                  description='Inverted geocoding for a given coordinate',
                                  lon=.0, lat=.0),
             create_external_link(self.module_name + '.journeys',
                                  rel='journeys',
                                  description='Compute journeys'),
         ]
     }
     return response, 200
Beispiel #4
0
 def get(self):
     resp = {"versions": []}
     for module_name, module in ModulesLoader.modules.items():
         mod = {
             'value': module_name,
             'description': module.description,
             'status': module.status,
             'links': [
                 create_external_link(module_name + '.' + module.index_endpoint, rel='api', _type='api')
             ],
         }
         resp['versions'].append(mod)
     return resp
Beispiel #5
0
 def wrapper(*args, **kwargs):
     objects = f(*args, **kwargs)
     if objects[1] != 200 or 'journeys' not in objects[0]:
         return objects
     for journey in objects[0]['journeys']:
         if "sections" not in journey:#this mean it's an isochrone...
             args = dict(request.args)
             if 'to' not in args:
                 args['to'] = journey['to']['id']
             if 'from' not in args:
                 args['from'] = journey['from']['id']
             if 'region' in kwargs:
                 args['region'] = kwargs['region']
             journey['links'] = [create_external_link('v1.journeys', rel='journeys', **args)]
     return objects
Beispiel #6
0
    def format(self, value):
        # note: some request args can be there several times,
        # but when there is only one elt, flask does not want lists
        args = {}
        for e in value.kwargs:
            if len(e.values) > 1:
                args[e.key] = [v for v in e.values]
            else:
                args[e.key] = e.values[0]

        return create_external_link('v1.{}'.format(value.ressource_name),
                                    rel=value.rel,
                                    _type=value.type,
                                    templated=value.is_templated,
                                    description=value.description,
                                    **args)
Beispiel #7
0
    def get_links(self, obj):
        # note: some request args can be there several times,
        # but when there is only one elt, flask does not want lists
        response = []
        for value in obj.links:
            args = {}
            for e in value.kwargs:
                if len(e.values) > 1:
                    args[e.key] = [v for v in e.values]
                else:
                    args[e.key] = e.values[0]

            args["_type"] = value.type
            args["templated"] = value.is_templated
            args["description"] = value.description
            args["rel"] = value.rel
            response.append(create_external_link('v1.{}'.format(value.ressource_name), **args))
        return response
Beispiel #8
0
        def wrapper(*args, **kwargs):
            objects = f(*args, **kwargs)
            if has_invalid_reponse_code(objects) or journeys_absent(objects):
                return objects

            for journey in objects[0]['journeys']:
                args = dict(request.args)
                allowed_ids = {
                    o['stop_point']['id']
                    for s in journey.get('sections', []) if 'from' in s
                    for o in (s['from'], s['to']) if 'stop_point' in o
                }

                if 'region' in kwargs:
                    args['region'] = kwargs['region']
                if "sections" not in journey:  # this mean it's an isochrone...
                    if 'to' not in args:
                        args['to'] = journey['to']['id']
                    if 'from' not in args:
                        args['from'] = journey['from']['id']
                    args['rel'] = 'journeys'
                    journey['links'] = [
                        create_external_link('v1.journeys', **args)
                    ]
                elif allowed_ids and 'public_transport' in (
                        s['type'] for s in journey['sections']):
                    # exactly one first_section_mode
                    if any(s['type'].startswith('bss')
                           for s in journey['sections'][:2]):
                        args['first_section_mode[]'] = 'bss'
                    else:
                        args['first_section_mode[]'] = journey['sections'][
                            0].get('mode', 'walking')

                    # exactly one last_section_mode
                    if any(s['type'].startswith('bss')
                           for s in journey['sections'][-2:]):
                        args['last_section_mode[]'] = 'bss'
                    else:
                        args['last_section_mode[]'] = journey['sections'][
                            -1].get('mode', 'walking')

                    args['min_nb_transfers'] = journey['nb_transfers']
                    args['direct_path'] = 'only' if 'non_pt' in journey[
                        'tags'] else 'none'
                    args['min_nb_journeys'] = 5
                    args['is_journey_schedules'] = True
                    allowed_ids.update(args.get('allowed_id[]', []))
                    args['allowed_id[]'] = list(allowed_ids)
                    args['_type'] = 'journeys'

                    # Delete arguments that are contradictory to the 'same_journey_schedules' concept
                    if '_final_line_filter' in args:
                        del args['_final_line_filter']
                    if '_no_shared_section' in args:
                        del args['_no_shared_section']

                    # Add datetime depending on datetime_represents parameter
                    if 'datetime_represents' not in args:
                        args['datetime'] = journey['departure_date_time']
                    else:
                        args['datetime'] = (journey['departure_date_time']
                                            if 'departure'
                                            in args.get('datetime_represents')
                                            else journey['arrival_date_time'])

                    # Here we create two links same_journey_schedules and this_journey
                    args['rel'] = 'same_journey_schedules'
                    same_journey_schedules_link = create_external_link(
                        'v1.journeys', **args)
                    args['rel'] = 'this_journey'
                    args['min_nb_journeys'] = 1
                    args['count'] = 1
                    this_journey_link = create_external_link(
                        'v1.journeys', **args)
                    journey['links'] = [
                        same_journey_schedules_link, this_journey_link
                    ]
            return objects