def manage_pt_object_without_line_section(navitia, db_objects, json_attribute, json_data): ''' :param navitia: :param db_objects: pt_object in database models : localisations, objects :param json_pt_object: attribute in json :param json_data: data :return: ''' pt_object_db = dict() for ptobject in db_objects: pt_object_db[ptobject.uri] = ptobject pt_object_dict = dict() if json_attribute in json_data: for pt_object_json in json_data[json_attribute]: if pt_object_json["type"] == 'line_section': continue try: ptobject = fill_and_get_pt_object(navitia, pt_object_dict, pt_object_json, False) except exceptions.ObjectUnknown: raise exceptions.ObjectUnknown( 'ptobject {} doesn\'t exist'.format(pt_object_json['id'])) if ptobject.uri not in pt_object_db: db_objects.append(ptobject) for ptobject_uri in pt_object_db: if ptobject_uri not in pt_object_dict: db_objects.remove(pt_object_db[ptobject_uri])
def fill_and_get_pt_object(navitia, all_objects, json, add_to_db=True): """ :param all_objects: dictionary of objects to be added in this session :param json: Flux which contains json information of pt_object :param add_to_db: ptobject insert into database :return: a pt_object and modify all_objects param """ if json["id"] in all_objects: return all_objects[json["id"]] pt_object = models.PTobject.get_pt_object_by_uri(json["id"]) if pt_object: all_objects[json["id"]] = pt_object return pt_object if not navitia.get_pt_object(json['id'], json['type']): raise exceptions.ObjectUnknown() pt_object = models.PTobject() mapper.fill_from_json(pt_object, json, mapper.object_mapping) if add_to_db: db.session.add(pt_object) all_objects[json["id"]] = pt_object return pt_object
def get(cls, id, client_id): severity = cls.query.filter_by(id=id, client_id=client_id, is_visible=True).first() if severity is None: raise exceptions.ObjectUnknown( 'The severty with id {} does not exist for this client'.format( id)) return severity
def manage_properties(disruption, json): """ Add properties linked to a post|put disruption by creating associate_disruption_property objects. The property has to be present in database or the function will end in error. Json format expected: "properties": [ { "property_id": "", "value": "" }, ... ] """ if 'properties' in json: properties_json = list() properties_db = list( (adp.property_id, adp.disruption_id, adp.value,) for adp in disruption.properties ) for json_property in json['properties']: property_db = models.Property.get( disruption.client.id, json_property['property_id'] ) if property_db is None: raise exceptions.ObjectUnknown( 'property {} not found'.format(json_property['property_id']) ) adp_db = create_adp( disruption, property_db.id, json_property['value'] ) properties_json.append( (adp_db.property_id, adp_db.disruption_id, adp_db.value,) ) difference = set(properties_db) - set(properties_json) for diff in difference: adp = models.AssociateDisruptionProperty.get(*diff) db.session.delete(adp)
def fill_and_add_line_section(navitia, impact_id, all_objects, pt_object_json): """ :param impact_id: impact_id to construct uri of line_section object :param all_objects: dictionary of objects to be added in this session :param pt_object_json: Flux which contains json information of pt_object :return: pt_object and modify all_objects param """ ptobject = models.PTobject() mapper.fill_from_json(ptobject, pt_object_json, mapper.object_mapping) #Here we treat all the objects in line_section like line, start_point, end_point line_section_json = pt_object_json['line_section'] ptobject.uri = ":".join((line_section_json['line']['id'], ptobject.id)) line_section = models.LineSection(ptobject.id) try: line_object = fill_and_get_pt_object(navitia, all_objects, line_section_json['line']) except exceptions.ObjectUnknown: raise exceptions.ObjectUnknown('{} {} doesn\'t exist'.format( line_section_json['line']['type'], line_section_json['line']['id'])) line_section.line = line_object try: start_object = fill_and_get_pt_object(navitia, all_objects, line_section_json['start_point']) except exceptions.ObjectUnknown: raise exceptions.ObjectUnknown('{} {} doesn\'t exist'.format( line_section_json['line']['type'], line_section_json['line']['id'])) line_section.start_point = start_object try: end_object = fill_and_get_pt_object(navitia, all_objects, line_section_json['end_point']) except exceptions.ObjectUnknown: raise exceptions.ObjectUnknown('{} {} doesn\'t exist'.format( line_section_json['line']['type'], line_section_json['line']['id'])) line_section.end_point = end_object #Here we manage routes in line_section #"routes":[{"id":"route:MTD:9", "type": "route"}, {"id":"route:MTD:Nav23", "type": "route"}] if 'routes' in line_section_json: for route in line_section_json["routes"]: try: route_object = fill_and_get_pt_object(navitia, all_objects, route, True) line_section.routes.append(route_object) except exceptions.ObjectUnknown: raise exceptions.ObjectUnknown('{} {} doesn\'t exist'.format( route['type'], route['id'])) #Here we manage via in line_section #"via":[{"id":"stop_area:MTD:9", "type": "stop_area"}, {"id":"stop_area:MTD:Nav23", "type": "stop_area"}] if 'via' in line_section_json: for via in line_section_json["via"]: try: via_object = fill_and_get_pt_object(navitia, all_objects, via, True) line_section.via.append(via_object) except exceptions.ObjectUnknown: raise exceptions.ObjectUnknown('{} {} doesn\'t exist'.format( via['type'], via['id'])) #Fill sens from json if 'sens' in line_section_json: line_section.sens = line_section_json["sens"] #Fill wordings from json #"meta":[{"key":"direction", "value": "1234"}, {"key":"direction", "value": "5678"}] if 'metas' in line_section_json: try: metas = {} metas['wordings'] = line_section_json['metas'] manage_wordings(line_section, metas) except exceptions.InvalidJson: raise ptobject.insert_line_section(line_section) return ptobject