class PoiSerializer(PbGenericSerializer): coord = CoordSerializer(required=False) label = jsonschema.Field(schema_type=str) administrative_regions = AdminSerializer(many=True, display_none=False) poi_type = PoiTypeSerializer(display_none=False) properties = jsonschema.MethodField(schema_metadata={ 'type': 'object', 'additionalProperties': { 'type': 'string' } }) address = AddressSerializer() stands = LiteralField(None, schema_type=StandsSerializer, display_none=False) def get_properties(self, obj): return {p.type: p.value for p in obj.properties}
class ImpactedStopSerializer(PbNestedSerializer): stop_point = jsonschema.MethodField( schema_type=lambda: StopPointSerializer()) base_arrival_time = TimeField(attr='base_stop_time.arrival_time') base_departure_time = TimeField(attr='base_stop_time.departure_time') amended_arrival_time = TimeField(attr='amended_stop_time.arrival_time') amended_departure_time = TimeField(attr='amended_stop_time.departure_time') cause = jsonschema.Field(schema_type=str, display_none=True) stop_time_effect = EnumField(attr='effect', pb_type=StopTimeUpdateStatus) departure_status = EnumField() arrival_status = EnumField() is_detour = jsonschema.Field(schema_type=bool, display_none=True) def get_stop_point(self, obj): if obj.HasField(str('stop_point')): return StopPointSerializer(obj.stop_point, display_none=False).data else: return None
class AddressSerializer(serpy.DictSerializer): id = CoordId(display_none=True) coord = CoordField() house_number = jsonschema.MethodField(display_none=True) label = NestedPropertyField(attr='properties.geocoding.label', display_none=True) name = NestedPropertyField(attr='properties.geocoding.name', display_none=True) administrative_regions = AdministrativeRegionsSerializer() def get_house_number(self, obj): geocoding = obj.get('properties', {}).get('geocoding', {}) hn = 0 import re numbers = re.findall(r'^\d+', geocoding.get('housenumber') or "0") if len(numbers) > 0: hn = numbers[0] return int(hn)
class JourneySerializer(PbNestedSerializer): duration = jsonschema.Field( schema_type=int, display_none=True, description='Duration of the journey (seconds)') nb_transfers = jsonschema.Field( schema_type=int, display_none=True, description='Number of transfers along the journey') departure_date_time = DateTimeField( description='Departure date and time of the journey') arrival_date_time = DateTimeField( description='Arrival date and time of the journey') requested_date_time = DateTimeField(deprecated=True) to = PlaceSerializer(deprecated=True, attr='destination') _from = PlaceSerializer(deprecated=True, attr='origin', label='from') type = jsonschema.Field( schema_type=str, display_none=True, description= 'Used to qualify the journey (can be "best", "comfort", "non_pt_walk", ...', ) status = jsonschema.Field( schema_type=str, attr="most_serious_disruption_effect", display_none=True, description='Status from the whole journey taking into account the most ' 'disturbing information retrieved on every object used ' '(can be "NO_SERVICE", "SIGNIFICANT_DELAYS", ...', ) tags = StringListField(display_none=True) co2_emission = AmountSerializer(display_none=True) durations = DurationsSerializer() distances = DistancesSerializer() fare = FareSerializer(display_none=True) calendars = CalendarSerializer(many=True, display_none=False) sections = SectionSerializer(many=True, display_none=False) debug = jsonschema.MethodField(schema_type=JourneyDebugSerializer(), display_none=False) def get_debug(self, obj): if not hasattr(g, 'debug') or not g.debug: return None return JourneyDebugSerializer(obj, display_none=False).data
class RouteDisplayInformationSerializer(PbNestedSerializer): commercial_mode = jsonschema.Field(schema_type=str) network = jsonschema.Field(schema_type=str) direction = jsonschema.Field(schema_type=str, display_none=True) label = jsonschema.MethodField(schema_type=str) def get_label(self, obj): if obj.HasField(str('code')) and obj.code != '': return obj.code elif obj.HasField(str('name')): return obj.name else: return None color = jsonschema.Field(schema_type=str) code = jsonschema.Field(schema_type=str) name = jsonschema.Field(schema_type=str) links = DisruptionLinkSerializer(attr='impact_uris', display_none=True) text_color = jsonschema.Field(schema_type=str)
class StopPointSerializer(GenericSerializer): comments = CommentSerializer(many=True, display_none=False) comment = FirstCommentField(attr='comments', display_none=False) codes = CodeSerializer(many=True, display_none=False) label = jsonschema.Field(schema_type=str) coord = CoordSerializer(required=False) links = DisruptionLinkSerializer(attr='impact_uris', display_none=True) commercial_modes = CommercialModeSerializer(many=True, display_none=False) physical_modes = PhysicalModeSerializer(many=True, display_none=False) administrative_regions = AdminSerializer(many=True, display_none=False) stop_area = jsonschema.MethodField(schema_type=lambda: StopAreaSerializer(), display_none=False) equipments = Equipments(attr='has_equipments', display_none=True) address = AddressSerializer(display_none=False) def get_stop_area(self, obj): if obj.HasField(str('stop_area')): return StopAreaSerializer(obj.stop_area, display_none=False).data else: return None
class PtObjectSerializer(GenericSerializer): quality = jsonschema.Field(schema_type=int, required=False, display_none=True) stop_area = jsonschema.MethodField( schema_type=lambda: StopAreaSerializer()) line = jsonschema.MethodField(schema_type=lambda: LineSerializer()) network = jsonschema.MethodField(schema_type=lambda: NetworkSerializer()) route = jsonschema.MethodField(schema_type=lambda: RouteSerializer()) commercial_mode = jsonschema.MethodField( schema_type=lambda: CommercialModeSerializer()) trip = jsonschema.MethodField(schema_type=lambda: TripSerializer()) embedded_type = EnumField(attr='embedded_type') def get_trip(self, obj): if obj.HasField(str('trip')): return TripSerializer(obj.trip, display_none=False).data else: return None def get_commercial_mode(self, obj): if obj.HasField(str('commercial_mode')): return CommercialModeSerializer(obj.commercial_mode, display_none=False).data else: return None def get_route(self, obj): if obj.HasField(str('route')): return RouteSerializer(obj.route, display_none=False).data else: return None def get_network(self, obj): if obj.HasField(str('network')): return NetworkSerializer(obj.network, display_none=False).data else: return None def get_line(self, obj): if obj.HasField(str('line')): return LineSerializer(obj.line, display_none=False).data else: return None def get_stop_area(self, obj): if obj.HasField(str('stop_area')): return StopAreaSerializer(obj.stop_area, display_none=False).data else: return None
class RouteSerializer(PbGenericSerializer): is_frequence = StrField(schema_metadata={ "enum": ["False"], "type": "string" }) direction_type = jsonschema.Field(schema_type=str, display_none=True) physical_modes = PhysicalModeSerializer(many=True, display_none=False) comments = CommentSerializer(many=True, display_none=False) codes = CodeSerializer(many=True, display_none=False) direction = PlaceSerializer() geojson = MultiLineStringField(display_none=False) links = DisruptionLinkSerializer(attr='impact_uris', display_none=True) line = jsonschema.MethodField(schema_type=lambda: LineSerializer()) stop_points = StopPointSerializer(many=True, display_none=False) def get_line(self, obj): if obj.HasField(str('line')): return LineSerializer(obj.line, display_none=False).data else: return None
class VehicleJourneySerializer(PbGenericSerializer): journey_pattern = JourneyPatternSerializer(display_none=False) stop_times = StopTimeSerializer(many=True) comments = CommentSerializer(many=True, display_none=False) comment = FirstCommentField(attr='comments', display_none=False) codes = CodeSerializer(many=True, required=False) validity_pattern = ValidityPatternSerializer() calendars = CalendarSerializer(many=True) trip = TripSerializer() disruptions = DisruptionLinkSerializer(attr='impact_uris', display_none=True) start_time = TimeField(display_none=False) end_time = TimeField(display_none=False) headway_secs = jsonschema.MethodField(schema_type=int, display_none=False) def get_headway_secs(self, obj): if obj.HasField(str('headway_secs')): return obj.headway_secs else: return None
class TicketSerializer(PbNestedSerializer): id = jsonschema.Field(schema_type=str, display_none=True, description='Identifier of the object') name = jsonschema.Field(schema_type=str, display_none=True, description='Name of the object') comment = jsonschema.Field(schema_type=str) found = jsonschema.BoolField() cost = CostSerializer(display_none=True) links = jsonschema.MethodField(schema_type=LinkSchema(many=True)) def get_links(self, obj): if not hasattr(obj, 'section_id'): return None return [ create_internal_link(id=value, rel='sections', _type='section') for value in obj.section_id ]
class StopAreaSerializer(serpy.DictSerializer): id = NestedPropertyField(attr='properties.geocoding.id', display_none=True) coord = CoordField() label = NestedPropertyField(attr='properties.geocoding.label', display_none=True) name = NestedPropertyField(attr='properties.geocoding.name', display_none=True) administrative_regions = AdministrativeRegionsSerializer(display_none=False) timezone = NestedPropertyField(attr='properties.geocoding.timezone') commercial_modes = NestedDictGenericField(attr='properties.geocoding.commercial_modes', many=True) physical_modes = NestedDictGenericField(attr='properties.geocoding.physical_modes', many=True) comments = NestedDictCommentField(attr='properties.geocoding.comments', many=True) comment = jsonschema.MethodField(display_none=True) codes = NestedDictCodeField(attr='properties.geocoding.codes', many=True) properties = NestedPropertiesField(attr='properties.geocoding.properties', display_none=False) def get_comment(self, obj): # To be compatible with old version, we create the "comment" field in addition. # This field is a simple string, so we take only one comment (In our case, the first # element of the list). comments = obj.get('properties', {}).get('geocoding', {}).get('comments') if comments: return next(iter(comments or []), None).get('name')
class GeocodePlacesSerializer(serpy.DictSerializer): places = jsonschema.MethodField() warnings = BetaEndpointsSerializer() feed_publishers = LiteralField( [raw_feed_publisher_bano, raw_feed_publisher_osm]) def get_places(self, obj): map_serializer = { 'city': GeocodeAdminSerializer, 'street': GeocodeAddressSerializer, 'house': GeocodeAddressSerializer, 'poi': GeocodePoiSerializer, 'public_transport:stop_area': GeocodeStopAreaSerializer } res = [] for feature in obj.get('features', {}): type_ = feature.get('properties', {}).get('geocoding', {}).get('type') if not type_ or type_ not in map_serializer: abort(404, message='Unknown places type {}'.format(type_)) res.append(map_serializer[type_](feature).data) return res
class StopAreaSerializer(PbGenericSerializer): comments = CommentSerializer(many=True, display_none=False) comment = FirstCommentField(attr='comments', display_none=False, deprecated=True) codes = CodeSerializer(many=True, display_none=False) timezone = jsonschema.Field(schema_type=str) label = jsonschema.Field(schema_type=str, description=LABEL_DESCRIPTION) coord = CoordSerializer(required=False) links = DisruptionLinkSerializer(attr='impact_uris', display_none=True) commercial_modes = CommercialModeSerializer(many=True, display_none=False) physical_modes = PhysicalModeSerializer(many=True, display_none=False) administrative_regions = AdminSerializer( many=True, display_none=False, description='Administrative regions of the stop area in which is the stop area', ) stop_points = StopPointSerializer( many=True, display_none=False, description='Stop points contained in this stop area' ) lines = jsonschema.MethodField(schema_type=lambda: LineSerializer(many=True), display_none=False) def get_lines(self, obj): return LineSerializer(obj.lines, many=True, display_none=False).data
class FareSerializer(PbNestedSerializer): found = jsonschema.BoolField() total = CostSerializer() links = jsonschema.MethodField(schema_type=LinkSchema(many=True), attr='ticket_id', display_none=True) def get_links(self, obj): if not hasattr(obj, 'ticket_id'): return [] return [create_internal_link(id=value, rel='tickets', _type='ticket') for value in obj.ticket_id] #TODO check that retro compatibility is really useful def to_value(self, value): if value is None: return { 'found': False, 'links': [], 'total': { 'currency': '', 'value': '0.0' } } return super(FareSerializer, self).to_value(value)
class DisruptionSerializer(PbNestedSerializer): id = jsonschema.Field(schema_type=str, display_none=True, attr='uri') disruption_id = jsonschema.Field(schema_type=str, attr='disruption_uri') impact_id = jsonschema.Field(schema_type=str, attr='uri') title = jsonschema.Field(schema_type=str), application_periods = PeriodSerializer(many=True) status = EnumField(attr='status', pb_type=ActiveStatus) updated_at = DateTimeField() tags = StringListField(display_none=False) cause = jsonschema.Field(schema_type=str, display_none=True) category = jsonschema.MethodField(schema_type=str, display_none=False) def get_category(self, obj): if obj.HasField(str("category")) and obj.category: return obj.category return None severity = SeveritySerializer() messages = MessageSerializer(many=True) impacted_objects = ImpactedSerializer(many=True, display_none=False) uri = jsonschema.Field(schema_type=str, attr='uri') disruption_uri = jsonschema.Field(schema_type=str) contributor = jsonschema.Field(schema_type=str, display_none=True)
class SectionSerializer(PbNestedSerializer): id = jsonschema.Field(schema_type=str, display_none=True) duration = jsonschema.Field( schema_type=int, display_none=True, description='Duration of the section (seconds)') co2_emission = AmountSerializer(display_none=True) transfer_type = EnumField(attr='transfer_type', pb_type=TransferType) departure_date_time = DateTimeField( attr='begin_date_time', description='Departure date and time of the section') arrival_date_time = DateTimeField( attr='end_date_time', description='Arrival date and time of the section') base_departure_date_time = DateTimeField( attr='base_begin_date_time', description='Base-schedule departure date and time of the section') base_arrival_date_time = DateTimeField( attr='base_end_date_time', description='Base-schedule arrival date and time of the section') data_freshness = EnumField(attr="realtime_level", pb_type=RTLevel, display_none=False) to = jsonschema.MethodField(schema_type=PlaceSerializer(), attr='destination') def get_to(self, obj): if obj.HasField(str('type')): enum = obj.DESCRIPTOR.fields_by_name[ 'type'].enum_type.values_by_number ret_value = enum[getattr(obj, 'type')].name if ret_value == 'WAITING': return None return PlaceSerializer(obj.destination).data _from = jsonschema.MethodField(schema_type=PlaceSerializer(), attr='origin', label='from') def get__from(self, obj): if obj.HasField(str('type')): enum = obj.DESCRIPTOR.fields_by_name[ 'type'].enum_type.values_by_number ret_value = enum[getattr(obj, 'type')].name if ret_value == 'WAITING': return None return PlaceSerializer(obj.origin).data additional_informations = EnumListField( attr='additional_informations', pb_type=SectionAdditionalInformationType) geojson = SectionGeoJsonField( display_none=False, description='GeoJSON of the shape of the section') mode = NestedEnumField(attr='street_network.mode', pb_type=StreetNetworkMode) type = SectionTypeEnum(attr='type', pb_type=SectionType) display_informations = VJDisplayInformationSerializer( attr='pt_display_informations', display_none=False) links = jsonschema.MethodField(display_none=True, schema_type=LinkSchema(many=True)) def get_links(self, obj): response = [] if obj.HasField(str("uris")): for type_, value in obj.uris.ListFields(): response.append({"type": type_.name, "id": value}) if obj.HasField(str('pt_display_informations')): response.extend(base.make_notes(obj.pt_display_informations.notes)) if obj.HasField(str('ridesharing_information')): response.extend([{ "type": "ridesharing_ad", "rel": l.key, "href": l.href, "internal": False } for l in obj.ridesharing_information.links]) return response stop_date_times = StopDateTimeSerializer(many=True) path = PathSerializer(attr="street_network.path_items", many=True, display_none=False) ridesharing_informations = RidesharingInformationSerializer( attr='ridesharing_information', display_none=False) ridesharing_journeys = jsonschema.MethodField( schema_type=lambda: JourneySerializer(display_none=False, many=True)) def get_ridesharing_journeys(self, obj): if not hasattr(obj, 'ridesharing_journeys') or not obj.ridesharing_journeys: return None return JourneySerializer(obj.ridesharing_journeys, display_none=False, many=True).data cycle_lane_length = PbIntField(display_none=False)
class SectionSerializer(PbNestedSerializer): id = jsonschema.Field(schema_type=str) duration = jsonschema.Field( schema_type=int, display_none=True, description='Duration of the section (seconds)') co2_emission = AmountSerializer(required=True, display_none=True) transfer_type = EnumField() departure_date_time = DateTimeField( attr='begin_date_time', description='Departure date and time of the section') arrival_date_time = DateTimeField( attr='end_date_time', description='Arrival date and time of the section') base_departure_date_time = DateTimeField( attr='base_begin_date_time', description='Base-schedule departure date and time of the section') base_arrival_date_time = DateTimeField( attr='base_end_date_time', description='Base-schedule arrival date and time of the section') to = jsonschema.MethodField(schema_type=PlaceSerializer(), attr='destination') def get_to(self, obj): if obj.HasField(str('type')): enum = obj.DESCRIPTOR.fields_by_name[ 'type'].enum_type.values_by_number ret_value = enum[getattr(obj, 'type')].name if ret_value == 'WAITING': return None return PlaceSerializer(obj.destination).data _from = jsonschema.MethodField(schema_type=PlaceSerializer(), attr='origin', label='from') def get__from(self, obj): if obj.HasField(str('type')): enum = obj.DESCRIPTOR.fields_by_name[ 'type'].enum_type.values_by_number ret_value = enum[getattr(obj, 'type')].name if ret_value == 'WAITING': return None return PlaceSerializer(obj.origin).data additional_informations = EnumListField( attr='additional_informations', pb_type=SectionAdditionalInformationType) geojson = SectionGeoJsonField( display_none=False, description='GeoJSON of the shape of the section') mode = NestedEnumField(attr='street_network.mode') type = SectionTypeEnum() display_informations = DisplayInformationSerializer( attr='pt_display_informations', display_none=False) links = jsonschema.MethodField(display_none=True, schema_type=LinkSchema(many=True)) def get_links(self, obj): response = [] if obj.HasField(str("uris")): for type_, value in obj.uris.ListFields(): response.append({"type": type_.name, "id": value}) if obj.HasField(str('pt_display_informations')): for value in obj.pt_display_informations.notes: response.append({ "type": 'notes', "id": value.uri, 'value': value.note }) return response stop_date_times = StopDateTimeSerializer(many=True) path = PathSerializer(attr="street_network.path_items", many=True, display_none=False)
class GeocodeAdminSerializer(PlacesCommonSerializer): administrative_region = jsonschema.MethodField() def get_administrative_region(self, obj): return AdministrativeRegionSerializer(obj).data
class AccessPointSerializer(PbGenericSerializer): coord = CoordSerializer(required=False) access_point_code = jsonschema.MethodField(schema_type=str, display_none=False) def get_access_point_code(self, obj): return get_proto_attr_or_default(obj, 'stop_code')
class PathWaySerializer(PbGenericSerializer): access_point = jsonschema.MethodField(schema_type=AccessPointSerializer(), display_none=False) is_entrance = jsonschema.MethodField(schema_type=bool, display_none=False) is_exit = jsonschema.MethodField(schema_type=bool, display_none=False) length = jsonschema.MethodField(schema_type=int, display_none=False) traversal_time = jsonschema.MethodField(schema_type=int, display_none=False) pathway_mode = jsonschema.MethodField(schema_type=int, display_none=False) stair_count = jsonschema.MethodField(schema_type=int, display_none=False) max_slope = jsonschema.MethodField(schema_type=int, display_none=False) min_width = jsonschema.MethodField(schema_type=int, display_none=False) signposted_as = jsonschema.MethodField(schema_type=str, display_none=False) reversed_signposted_as = jsonschema.MethodField(schema_type=str, display_none=False) def get_is_entrance(self, obj): return get_proto_attr_or_default(obj, 'is_entrance') def get_is_exit(self, obj): return get_proto_attr_or_default(obj, 'is_exit') def get_length(self, obj): return get_proto_attr_or_default(obj, 'length') def get_traversal_time(self, obj): return get_proto_attr_or_default(obj, 'traversal_time') def get_pathway_mode(self, obj): return get_proto_attr_or_default(obj, 'pathway_mode') def get_stair_count(self, obj): return get_proto_attr_or_default(obj, 'stair_count') def get_max_slope(self, obj): return get_proto_attr_or_default(obj, 'max_slope') def get_min_width(self, obj): return get_proto_attr_or_default(obj, 'min_width') def get_signposted_as(self, obj): return get_proto_attr_or_default(obj, 'signposted_as') def get_reversed_signposted_as(self, obj): return get_proto_attr_or_default(obj, 'reversed_signposted_as') def get_access_point(self, obj): return AccessPointSerializer(obj).data