Exemple #1
0
        def wrapper(*args, **kwargs):
            objects = f(*args, **kwargs)
            if has_invalid_reponse_code(objects) or journeys_absent(objects):
                return objects

            if 'tickets' not in objects[0]:
                return objects

            ticket_by_section = defaultdict(list)
            for t in objects[0]['tickets']:
                if "links" in t:
                    for s in t['links']:
                        ticket_by_section[s['id']].append(t['id'])

            for j in objects[0]['journeys']:
                if "sections" not in j:
                    continue
                for s in j['sections']:

                    # them we add the link to the different tickets needed
                    for ticket_needed in ticket_by_section[s["id"]]:
                        s['links'].append(create_internal_link(_type="ticket", rel="tickets", id=ticket_needed))
                    if "ridesharing_journeys" not in s:
                        continue
                    for rsj in s['ridesharing_journeys']:
                        if "sections" not in rsj:
                            continue
                        for rss in rsj['sections']:
                            # them we add the link to the different ridesharing-tickets needed
                            for rs_ticket_needed in ticket_by_section[rss["id"]]:
                                rss['links'].append(
                                    create_internal_link(_type="ticket", rel="tickets", id=rs_ticket_needed)
                                )

            return objects
Exemple #2
0
        def wrapper(*args, **kwargs):
            objects = f(*args, **kwargs)
            if has_invalid_reponse_code(objects) or journeys_absent(objects):
                return objects

            for j in objects[0]['journeys']:
                if "sections" not in j:
                    continue
                for s in j['sections']:
                    # For a section with type = on_demand_transport
                    if s.get('type') == 'on_demand_transport':
                        # get network uri from the link
                        network_id = next(
                            (link['id'] for link in s.get('links', []) if link['type'] == "network"), None
                        )
                        if not network_id:
                            continue

                        region = kwargs.get('region')
                        if region is None:
                            continue

                        # Get the Network details and verify if it contains codes with type = "app_code"
                        instance = i_manager.instances.get(region)
                        network_details = instance.ptref.get_objs(
                            type_pb2.NETWORK, 'network.uri={}'.format(network_id)
                        )
                        network_dict = protobuf_to_dict(next(network_details))
                        app_value = next(
                            (
                                code['value']
                                for code in network_dict.get('codes', [])
                                if code.get('type') == "app_code"
                            ),
                            None,
                        )
                        if not app_value:
                            continue

                        # Prepare parameters for the deeplink of external service
                        from_embedded_type = s.get('from').get('embedded_type')
                        to_embedded_type = s.get('to').get('embedded_type')
                        from_coord = s.get('from').get(from_embedded_type).get('coord')
                        to_coord = s.get('to').get(to_embedded_type).get('coord')
                        args = dict()
                        date_utc = local_str_date_to_utc(s.get('departure_date_time'), instance.timezone)
                        args['departure_latitude'] = from_coord.get('lat')
                        args['departure_longitude'] = from_coord.get('lon')
                        args['destination_latitude'] = to_coord.get('lat')
                        args['destination_longitude'] = to_coord.get('lon')
                        args['requested_departure_time'] = dt_to_str(date_utc, _format=UTC_DATETIME_FORMAT)
                        url = "{}://home?".format(app_value)
                        tad_link = make_external_service_link(
                            url=url, rel="tad_dynamic_link", _type="tad_dynamic_link", **args
                        )
                        s['links'].append(tad_link)

            return objects
Exemple #3
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