def manage_channels_required(messages_json, client_id): channels_required = models.Channel.get_channels_required(client_id) if channels_required: for channel_required in channels_required: if channel_required.id not in messages_json or messages_json[channel_required.id] is None: raise exceptions.InvalidJson('Channel {} is required.'.format(channel_required.id)) elif not messages_json[channel_required.id]['text']: raise exceptions.InvalidJson('Empty property \'text\' is not allowed for channel {}.'.format(channel_required.id))
def manage_wordings(db_object, json_wordings): db_object.delete_wordings() for json_wording in json_wordings: db_wording = models.Wording() key = json_wording["key"].strip() if key == '': raise exceptions.InvalidJson( 'Json invalid: key is empty, you give : {}'.format( json_wordings)) db_wording.key = json_wording["key"] db_wording.value = json_wording["value"] db_object.wordings.append(db_wording) db_object.wording = db_object.wordings[0].value
def manage_wordings(db_object, json): db_object.delete_wordings() # handle wordings wordings = json['wordings'] for json_wording in wordings: db_wording = models.Wording() key = json_wording["key"].strip() if key == '': raise exceptions.InvalidJson('Json invalid: key is empty, you give : {}'.format(wordings)) db_wording.key = json_wording["key"] db_wording.value = json_wording["value"] db_object.wordings.append(db_wording) # handle wording wording = db_object.wordings[0].value if 'wording' in json: wording = json['wording'] db_object.wording = wording
def fill_and_add_line_section(navitia, all_objects, pt_object_json): """ :param navitia: Class Navitia :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 if 'line_section' not in pt_object_json: raise exceptions.InvalidJson( 'Object of type line_section must have a line_section entry') 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['start_point']['type'], line_section_json['start_point']['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['end_point']['type'], line_section_json['end_point']['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'])) # 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