def view_delete_tabular_map(request): """ Attempt to delete a dataverse-created WorldMap layer """ if not request.POST: raise Http404('Delete Not Found.') d = get_common_lookup(request) d['WORLDMAP_SERVER_URL'] = settings.WORLDMAP_SERVER_URL d['DATAVERSE_SERVER_URL'] = settings.DATAVERSE_SERVER_URL d['page_title'] = PANEL_TITLE_DELETE_MAP d['IS_DELETE_PAGE'] = True # Check the delete request f = DeleteMapForm(request.POST) if not f.is_valid(): d['ERROR_FOUND'] = True d['FAILED_TO_VALIDATE'] = True return render(request, 'worldmap_layers/view_delete_layer.html', d) # Form params look good worldmap_layer_info = f.get_worldmap_layer_info() if not worldmap_layer_info: raise Http404('WorldMap Layer info no longer available') # depending on the type: tabular_info, shapefile_info, etc # if worldmap_layer_info.is_shapefile_layer(): d['is_shapefile_layer'] = True else: d['is_tabular_layer'] = True gis_data_info = worldmap_layer_info.get_gis_data_info() d['gis_data_info'] = gis_data_info # ----------------------------------- # Delete map from WorldMap # ----------------------------------- flag_delete_local_worldmap_info = False (success, err_msg_or_None) = delete_map_layer(gis_data_info, worldmap_layer_info) if success is False: LOGGER.error("Failed to delete WORLDMAP layer: %s", err_msg_or_None) if err_msg_or_None and err_msg_or_None.find( '"Existing layer not found."') > -1: pass else: d['ERROR_FOUND'] = True d['WORLDMAP_DATA_DELETE_FAILURE'] = True d['ERR_MSG'] = err_msg_or_None return render(request, 'worldmap_layers/view_delete_layer.html', d) else: # At this point, the layer no longer exists on WorldMap, # set a flag to delete it from geoconnect, even if the Dataverse # delete fails flag_delete_local_worldmap_info = True # ----------------------------------- # Delete metadata from dataverse # ----------------------------------- (success2, err_msg_or_None2 ) = MetadataUpdater.delete_dataverse_map_metadata(worldmap_layer_info) # Delete the Geoconnect WorldMap info -- regardless of # whether the data was removed from Dataverse if flag_delete_local_worldmap_info: msgt('Delete worldmap_layer_info: %s' % worldmap_layer_info) worldmap_layer_info.delete() if success2 is False: LOGGER.error("Failed to delete Map Metadata from Dataverse: %s", err_msg_or_None) d['ERROR_FOUND'] = True d['DATAVERSE_DATA_DELETE_FAILURE'] = True d['ERR_MSG'] = err_msg_or_None2 return render(request, 'worldmap_layers/view_delete_layer.html', d) d['DELETE_SUCCESS'] = True d['page_title'] = PANEL_TITLE_REMAP return render(request, 'worldmap_layers/view_delete_layer.html', d)
def view_delete_tabular_map(request): """ Attempt to delete a dataverse-created WorldMap layer """ if not request.POST: raise Http404('Delete Not Found.') d = get_common_lookup(request) d['WORLDMAP_SERVER_URL'] = settings.WORLDMAP_SERVER_URL d['DATAVERSE_SERVER_URL'] = settings.DATAVERSE_SERVER_URL d['page_title'] = PANEL_TITLE_DELETE_MAP d['IS_DELETE_PAGE'] = True # Check the delete request f = DeleteMapForm(request.POST) if not f.is_valid(): d['ERROR_FOUND'] = True d['FAILED_TO_VALIDATE'] = True return render(request, 'worldmap_layers/view_delete_layer.html', d) # Form params look good worldmap_layer_info = f.get_worldmap_layer_info() if not worldmap_layer_info: raise Http404('WorldMap Layer info no longer available') # depending on the type: tabular_info, shapefile_info, etc # if worldmap_layer_info.is_shapefile_layer(): d['is_shapefile_layer'] = True else: d['is_tabular_layer'] = True gis_data_info = worldmap_layer_info.get_gis_data_info() d['gis_data_info'] = gis_data_info # ----------------------------------- # Delete map from WorldMap # ----------------------------------- flag_delete_local_worldmap_info = False (success, err_msg_or_None) = delete_map_layer(gis_data_info, worldmap_layer_info) if success is False: LOGGER.error("Failed to delete WORLDMAP layer: %s", err_msg_or_None) if err_msg_or_None and err_msg_or_None.find('"Existing layer not found."') > -1: pass else: d['ERROR_FOUND'] = True d['WORLDMAP_DATA_DELETE_FAILURE'] = True d['ERR_MSG'] = err_msg_or_None return render(request, 'worldmap_layers/view_delete_layer.html', d) else: # At this point, the layer no longer exists on WorldMap, # set a flag to delete it from geoconnect, even if the Dataverse # delete fails flag_delete_local_worldmap_info = True # ----------------------------------- # Delete metadata from dataverse # ----------------------------------- (success2, err_msg_or_None2) = MetadataUpdater.delete_dataverse_map_metadata(worldmap_layer_info) # Delete the Geoconnect WorldMap info -- regardless of # whether the data was removed from Dataverse if flag_delete_local_worldmap_info: msgt('Delete worldmap_layer_info: %s' % worldmap_layer_info) worldmap_layer_info.delete() if success2 is False: LOGGER.error("Failed to delete Map Metadata from Dataverse: %s", err_msg_or_None) d['ERROR_FOUND'] = True d['DATAVERSE_DATA_DELETE_FAILURE'] = True d['ERR_MSG'] = err_msg_or_None2 return render(request, 'worldmap_layers/view_delete_layer.html', d) d['DELETE_SUCCESS'] = True d['page_title'] = PANEL_TITLE_REMAP return render(request, 'worldmap_layers/view_delete_layer.html', d)
def test_02_delete_dataverse_metadata(self): """Test Dataverse "delete metadata" url endpoint. Only testing fail conditions, e.g. can't contact server, etc.""" msgt(self.test_02_delete_dataverse_metadata.__doc__) tab_file_info = TabularFileInfo.objects.get( pk=15) # Election precinct test # -------------------------------------------- # Attach actual file -- path from fixture is not correct # -------------------------------------------- elect_filepath = join(dirname(__file__), 'input', 'election_precincts2.csv') tab_file_info.dv_file.save(\ 'election_precincts2.csv', File(open(elect_filepath, 'r')), save=False) self.assertEqual(tab_file_info.id, 15) # ------------------------------------------ # Load successful info # ------------------------------------------ tab_map_info = WorldMapTabularLayerInfo.build_from_worldmap_json(\ tab_file_info,\ json.loads(self.json_join_data_string)) self.assertTrue(tab_map_info.id is not None) # ------------------------------------------ # Make sure data loading as expected # ------------------------------------------ self.assertEqual(type(tab_map_info.core_data), dict) self.assertEqual(type(tab_map_info.attribute_data), list) self.assertEqual(type(tab_map_info.download_links), dict) # ------------------------------------------ # Send message to non-existent server # ------------------------------------------ msgt('Send message to non-existent server') url_non_existent = 'https://nope.dataverse.harvard.edu' success, err_msg_or_None = MetadataUpdater.delete_dataverse_map_metadata(\ tab_map_info, url_non_existent) self.assertEqual(success, False) self.assertTrue(err_msg_or_None.startswith(\ ERROR_DV_NO_SERVER_RESPONSE)) # ------------------------------------------ # Send message to server without an endpoint # ------------------------------------------ msgt('Send message to server without an endpoint') url_no_endpoint = 'http://www.harvard.edu' success, err_msg_or_None = MetadataUpdater.delete_dataverse_map_metadata(\ tab_map_info, url_no_endpoint) self.assertEqual(success, False) self.assertTrue(err_msg_or_None.startswith(\ ERROR_DV_PAGE_NOT_FOUND)) # ------------------------------------------ # No token in request to Dataverse # ------------------------------------------ msgt(('No token in request to Dataverse' ' (requires working endpoint at https://dataverse.harvard.edu)')) url_no_endpoint = 'https://dataverse.harvard.edu' success, err_msg_or_None = MetadataUpdater.delete_dataverse_map_metadata(\ tab_map_info, url_no_endpoint) self.assertEqual(success, False) self.assertEqual(err_msg_or_None, 'Token not found in JSON request.')
def test_02_delete_dataverse_metadata(self): """Test Dataverse "delete metadata" url endpoint. Only testing fail conditions, e.g. can't contact server, etc.""" msgt(self.test_02_delete_dataverse_metadata.__doc__) tab_file_info = TabularFileInfo.objects.get(pk=15) # Election precinct test # -------------------------------------------- # Attach actual file -- path from fixture is not correct # -------------------------------------------- elect_filepath = join(dirname(__file__), 'input', 'election_precincts2.csv') tab_file_info.dv_file.save(\ 'election_precincts2.csv', File(open(elect_filepath, 'r')), save=False) self.assertEqual(tab_file_info.id, 15) # ------------------------------------------ # Load successful info # ------------------------------------------ tab_map_info = WorldMapTabularLayerInfo.build_from_worldmap_json(\ tab_file_info,\ json.loads(self.json_join_data_string)) self.assertTrue(tab_map_info.id is not None) # ------------------------------------------ # Make sure data loading as expected # ------------------------------------------ self.assertEqual(type(tab_map_info.core_data), dict) self.assertEqual(type(tab_map_info.attribute_data), list) self.assertEqual(type(tab_map_info.download_links), dict) # ------------------------------------------ # Send message to non-existent server # ------------------------------------------ msgt('Send message to non-existent server') url_non_existent = 'https://nope.dataverse.harvard.edu' success, err_msg_or_None = MetadataUpdater.delete_dataverse_map_metadata(\ tab_map_info, url_non_existent) self.assertEqual(success, False) self.assertTrue(err_msg_or_None.startswith(\ ERROR_DV_NO_SERVER_RESPONSE)) # ------------------------------------------ # Send message to server without an endpoint # ------------------------------------------ msgt('Send message to server without an endpoint') url_no_endpoint = 'http://www.harvard.edu' success, err_msg_or_None = MetadataUpdater.delete_dataverse_map_metadata(\ tab_map_info, url_no_endpoint) self.assertEqual(success, False) self.assertTrue(err_msg_or_None.startswith(\ ERROR_DV_PAGE_NOT_FOUND)) # ------------------------------------------ # No token in request to Dataverse # ------------------------------------------ msgt(('No token in request to Dataverse' ' (requires working endpoint at https://dataverse.harvard.edu)')) url_no_endpoint = 'https://dataverse.harvard.edu' success, err_msg_or_None = MetadataUpdater.delete_dataverse_map_metadata(\ tab_map_info, url_no_endpoint) self.assertEqual(success, False) self.assertEqual(err_msg_or_None, 'Token not found in JSON request.')