示例#1
0
def list_all_in_system(
    system_id, alerts_detail: views.AlertsDetail = None
) -> typing.List[views.Agency]:
    system = systemqueries.get_by_id(system_id, only_return_active=True)
    if system is None:
        raise exceptions.IdNotFoundError(models.System, system_id=system_id)
    agencies = genericqueries.list_in_system(models.Agency, system_id)
    response = list(map(views.Agency.from_model, agencies))
    helpers.add_alerts_to_views(
        response, agencies, alerts_detail or views.AlertsDetail.CAUSE_AND_EFFECT
    )
    return response
示例#2
0
def list_all_in_system(system_id,
                       alerts_detail=None) -> typing.List[views.Stop]:
    system = systemqueries.get_by_id(system_id, only_return_active=True)
    if system is None:
        raise exceptions.IdNotFoundError(models.System, system_id=system_id)

    stops = stopqueries.list_all_in_system(system_id)
    response = list(map(views.Stop.from_model, stops))
    helpers.add_alerts_to_views(
        response,
        stops,
        alerts_detail or views.AlertsDetail.NONE,
    )
    return response
示例#3
0
def get_in_system_by_id(
    system_id, agency_id, alerts_detail: views.AlertsDetail = None
) -> views.AgencyLarge:
    agency = genericqueries.get_in_system_by_id(models.Agency, system_id, agency_id)
    if agency is None:
        raise exceptions.IdNotFoundError(
            models.Route, system_id=system_id, route_id=agency_id
        )
    response = views.AgencyLarge.from_model(agency)
    helpers.add_alerts_to_views(
        [response], [agency], alerts_detail or views.AlertsDetail.MESSAGES
    )
    response.routes = list(map(views.Route.from_model, agency.routes))
    return response
示例#4
0
def list_all_in_system(
    system_id, alerts_detail: views.AlertsDetail = None
) -> typing.List[views.Route]:
    system = systemqueries.get_by_id(system_id, only_return_active=True)
    if system is None:
        raise exceptions.IdNotFoundError(models.System, system_id=system_id)

    response = []
    routes = list(routequeries.list_in_system(system_id))
    for route in routes:
        route_response = views.Route.from_model(route)
        response.append(route_response)
    helpers.add_alerts_to_views(
        response, routes, alerts_detail or views.AlertsDetail.CAUSE_AND_EFFECT,
    )
    return response
示例#5
0
def get_in_route_by_id(
    system_id, route_id, trip_id, alerts_detail: views.AlertsDetail = None
):
    trip = tripqueries.get_in_route_by_id(system_id, route_id, trip_id)
    if trip is None:
        raise exceptions.IdNotFoundError(
            models.Trip, system_id=system_id, route_id=route_id, trip_id=trip_id
        )
    trip_response = views.Trip.from_model(trip)
    trip_response.route = views.Route.from_model(trip.route)
    trip_response.stop_times = []
    for stop_time in trip.stop_times:
        stop_time_response = views.TripStopTime.from_model(stop_time)
        stop_time_response.stop = views.Stop.from_model(stop_time.stop)
        trip_response.stop_times.append(stop_time_response)
    helpers.add_alerts_to_views(
        [trip_response], [trip], alerts_detail or views.AlertsDetail.MESSAGES
    )
    return trip_response
示例#6
0
def get_in_system_by_id(
    system_id, route_id, alerts_detail: views.AlertsDetail = None
) -> views.RouteLarge:
    route = routequeries.get_in_system_by_id(system_id, route_id)
    if route is None:
        raise exceptions.IdNotFoundError(
            models.Route, system_id=system_id, route_id=route_id
        )

    periodicity = routequeries.calculate_periodicity(route.pk)
    if periodicity is not None:
        periodicity = int(periodicity / 6) / 10
    result = views.RouteLarge.from_model(route, periodicity)
    if route.agency is not None:
        result.agency = views.Agency.from_model(route.agency)
    helpers.add_alerts_to_views(
        [result], [route], alerts_detail or views.AlertsDetail.MESSAGES,
    )
    result.service_maps = servicemapmanager.build_route_service_maps_response(route.pk)
    return result
示例#7
0
def list_all_in_route(
    system_id, route_id, alerts_detail: views.AlertsDetail = None
) -> typing.List[views.Trip]:
    route = routequeries.get_in_system_by_id(system_id, route_id)
    if route is None:
        raise exceptions.IdNotFoundError(
            models.Route, system_id=system_id, route_id=route_id
        )

    response = []
    trips = tripqueries.list_all_in_route_by_pk(route.pk)
    trip_pk_to_last_stop = tripqueries.get_trip_pk_to_last_stop_map(
        trip.pk for trip in trips
    )
    for trip in trips:
        trip_response = views.Trip.from_model(trip)
        last_stop = trip_pk_to_last_stop.get(trip.pk)
        if last_stop is not None:
            trip_response.last_stop = views.Stop.from_model(last_stop)
        response.append(trip_response)
    helpers.add_alerts_to_views(
        response, trips, alerts_detail or views.AlertsDetail.CAUSE_AND_EFFECT
    )
    return response
示例#8
0
def get_in_system_by_id(
    system_id,
    stop_id,
    return_only_stations=True,
    earliest_time=None,
    latest_time=None,
    minimum_number_of_trips=None,
    include_all_trips_within=None,
    exclude_trips_before=None,
    alerts_detail=None,
):
    """
    Get information about a specific stop.
    """
    stop = stopqueries.get_in_system_by_id(system_id, stop_id)
    if stop is None:
        raise exceptions.IdNotFoundError(models.Stop,
                                         system_id=system_id,
                                         stop_id=stop_id)

    stop_tree = _StopTree(stop,
                          stopqueries.list_all_stops_in_stop_tree(stop.pk))
    all_station_pks = set(stop.pk for stop in stop_tree.all_stations())
    transfers = stopqueries.list_all_transfers_at_stops(all_station_pks)

    # The descendant stops are used as the source of trip stop times
    descendant_stop_pks = list(stop.pk for stop in stop_tree.descendents())
    direction_name_matcher = _DirectionNameMatcher(
        stopqueries.list_direction_rules_for_stops(descendant_stop_pks))
    trip_stop_times = stopqueries.list_stop_time_updates_at_stops(
        descendant_stop_pks,
        earliest_time=earliest_time,
        latest_time=latest_time,
    )
    trip_pk_to_last_stop = tripqueries.get_trip_pk_to_last_stop_map(
        trip_stop_time.trip.pk for trip_stop_time in trip_stop_times)

    # On the other hand, the stop tree graph that is returned consists of all
    # stations in the stop's tree
    stop_pk_to_service_maps_response = servicemapmanager.build_stop_pk_to_service_maps_response(
        all_station_pks.union(transfer.to_stop_pk for transfer in transfers))

    # Using the data retrieved, we then build the response
    response = views.StopLarge.from_model(stop)
    stop_tree_base: views.Stop = _build_stop_tree_response(
        stop_tree, stop_pk_to_service_maps_response, return_only_stations)
    response.parent_stop = stop_tree_base.parent_stop
    response.child_stops = stop_tree_base.child_stops
    response.service_maps = stop_tree_base.service_maps
    response.directions = list(direction_name_matcher.all_names())
    response.transfers = _build_transfers_response(
        transfers, stop_pk_to_service_maps_response)

    stop_time_filter = _TripStopTimeFilter(
        inclusion_interval_start=exclude_trips_before,
        inclusion_interval_end=include_all_trips_within,
        min_trips_per_direction=minimum_number_of_trips,
    )
    for trip_stop_time in trip_stop_times:
        direction = direction_name_matcher.match(trip_stop_time)
        if stop_time_filter.remove(trip_stop_time, direction):
            continue
        response.stop_times.append(
            _build_trip_stop_time_response(trip_stop_time, direction,
                                           trip_pk_to_last_stop))
    helpers.add_alerts_to_views(
        [response],
        [stop],
        alerts_detail or views.AlertsDetail.CAUSE_AND_EFFECT,
    )
    return response