Ejemplo n.º 1
0
    def _add_first_last_links(self, resp, params):
        soonest_departure_ts = min(j.departure_date_time for j in resp.journeys)
        soonest_departure = timestamp_to_datetime(soonest_departure_ts)
        if soonest_departure:
            soonest_departure = soonest_departure.replace(hour=0, minute=0, second=0)
            params['datetime'] = dt_to_str(soonest_departure)
            params['datetime_represents'] = 'departure'
            add_link(resp, rel='first', **params)

        tardiest_arrival_ts = max(j.arrival_date_time for j in resp.journeys)
        tardiest_arrival = timestamp_to_datetime(tardiest_arrival_ts)
        if tardiest_arrival:
            tardiest_arrival = tardiest_arrival.replace(hour=23, minute=59, second=59)
            params['datetime'] = dt_to_str(tardiest_arrival)
            params['datetime_represents'] = 'arrival'
            add_link(resp, rel='last', **params)
Ejemplo n.º 2
0
        def generate_request_id():

            path = str(request.path)
            args_for_id = dict(request.args)
            if "_override_scenario" in args_for_id:
                scenario = str(args_for_id["_override_scenario"][0])
                args_for_id["_override_scenario"] = ""
            else:
                scenario = "new_default"

            json_repr = json.dumps(args_for_id,
                                   sort_keys=True,
                                   ensure_ascii=True)
            # we could use the json_repr as an id, but we hash it to have something smaller
            m = hashlib.sha256()
            m.update(json_repr.encode("UTF-8"))
            json_hash = m.hexdigest()

            now = dt_to_str(datetime.datetime.utcnow())

            result = "journeys_{}_{}#{}#".format(json_hash, now, scenario)

            logger.info("Generating id : {} for request : {}".format(
                result, request.url))

            return result
Ejemplo n.º 3
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
Ejemplo n.º 4
0
 def _add_ridesharing_link(self, resp, params, instance):
     req = request.args.to_dict(flat=False)
     req['partner_services[]'] = 'ridesharing'
     req['datetime'] = dt_to_str(params.original_datetime)
     req['region'] = instance.name
     add_link(resp, rel='ridesharing_journeys', **req)
Ejemplo n.º 5
0
 def to_value(self, value):
     date = datetime.fromtimestamp(value).date()
     return dt_to_str(date, _format="%Y%m%d")
Ejemplo n.º 6
0
 def to_value(self, value):
     return dt_to_str(value, _format=DATETIME_FORMAT)