def dehydrate(self, bundle): bundle = super(JoinFeatureResource, self).dehydrate(bundle) def map_related_to_field_and_label(field_name, related_model): """ Creates a mapping from the _id attribute of ForeignKeys to to the related attribute (e.g. built_form_id to built_form). It puts the related attribute in the main dict with its resource_uri and puts a label representation in the __labels dict so that the related attribute can map from its id to a label. This is similar to what the regular FeatureResource does but it is more manual since we only have the _id attribute and must resolve the related instance ourselves """ client_field_name = self.client_field_name(field_name) related_id = getattr(bundle.obj, client_field_name) if hasattr(bundle.obj, client_field_name) else None if related_id: related_model_instance = related_model.objects.get(id=related_id) related_field_name = field_name.replace('_id', '') return { # Get the resource class of the related model so we can make a uri related_field_name: FootprintResource.resolve_resource_class(related_model)().get_resource_uri(related_model_instance), # The label representation '__labels': {related_field_name: LayerSelection.resolve_instance_label(related_model_instance)} } return None related_field_lookup = self.related_field_lookup if related_field_lookup: # For join queries, we need to manually add related models--not the joins, # rather related models on the main feature, such as a BuiltForm reference bundle.data = deep_merge(bundle.data, *compact(map_dict( map_related_to_field_and_label, related_field_lookup ))) return bundle
def create_layer_from_layer_selection(self, params): """ Used to create a new Layer from the current LayerSelection features :param params: :return: """ # Resolve the source layer from the layer_selection__id source_layer = self.resolve_layer(params) config_entity = source_layer.config_entity db_entity = source_layer.db_entity_interest.db_enitty feature_class = FeatureClassCreator(config_entity, db_entity).dynamic_model_class() layer = Layer.objects.get(presentation__config_entity=config_entity, db_entity_key=db_entity.key) layer_selection = get_or_create_layer_selection_class_for_layer( layer, config_entity, False).objects.all()[0] # TODO no need to do geojson here feature_dict = dict(type="Feature") feature_dicts = map( lambda feature: deep_merge(feature_dict, {"geometry": geojson.loads(feature.wkb_geometry.json)}), layer_selection.selected_features or feature_class.objects.all()) json = dict({"type": "FeatureCollection", "features": feature_dicts}) db_entity_configuration = update_or_create_db_entity( config_entity, **dict(class_scope=FutureScenario, name='Import From Selection Test', key='import_selection_test', url='file://notusingthis')) self.make_geojson_db_entity(config_entity, db_entity_configuration, data=json)
def create_layer_from_layer_selection(self, params): """ Used to create a new Layer from the current LayerSelection features :param params: :return: """ # Resolve the source layer from the layer_selection__id source_layer = self.resolve_layer(params) config_entity = source_layer.config_entity db_entity = source_layer.db_entity_interest.db_enitty feature_class = FeatureClassCreator(config_entity, db_entity).dynamic_model_class() layer = Layer.objects.get(presentation__config_entity=config_entity, db_entity_key=db_entity.key) layer_selection = get_or_create_layer_selection_class_for_layer(layer, config_entity, False).objects.all()[0] # TODO no need to do geojson here feature_dict = dict( type="Feature" ) feature_dicts = map(lambda feature: deep_merge(feature_dict, {"geometry":geojson.loads(feature.wkb_geometry.json)}), layer_selection.selected_features or feature_class.objects.all()) json = dict({"type": "FeatureCollection", "features": feature_dicts}) db_entity_configuration = update_or_create_db_entity(config_entity, **dict( class_scope=FutureScenario, name='Import From Selection Test', key='import_selection_test', url='file://notusingthis' )) self.make_geojson_db_entity(config_entity, db_entity_configuration, data=json)
def test(self): result = deep_merge({ 1: { 1: 1 }, 'a': { 'b': { 'b': 'b' } } }, { 1: { 2: 2 }, 2: 2 }, { 3: 3, 'a': { 'b': { 'c': 'c' } } }) assert_equal(result, { 1: { 1: 1, 2: 2 }, 2: 2, 3: 3, 'a': { 'b': { 'b': 'b', 'c': 'c' } } })
def test(self): result = deep_merge({1:{1:1}, 'a':{'b':{'b':'b'}}}, {1:{2:2}, 2:2}, {3:3, 'a':{'b':{'c':'c'}}}) assert_equal(result, {1:{1:1, 2:2}, 2:2, 3:3, 'a':{'b':{'b':'b', 'c':'c'}}})