From 53cae70ddad87a73448d8dd0457d0426c85fde3d Mon Sep 17 00:00:00 2001 From: Jon Mease Date: Tue, 18 Jun 2019 08:16:44 -0400 Subject: [PATCH 1/2] chart_studio: Delete existing file before creating rather than update --- .../chart_studio/plotly/plotly.py | 47 +++++++++---------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/packages/python/chart-studio/chart_studio/plotly/plotly.py b/packages/python/chart-studio/chart_studio/plotly/plotly.py index 476b4081700..fffd21872cd 100644 --- a/packages/python/chart-studio/chart_studio/plotly/plotly.py +++ b/packages/python/chart-studio/chart_studio/plotly/plotly.py @@ -281,7 +281,7 @@ def plot(figure_or_data, validate=True, **plot_options): _set_grid_column_references(figure, grid) payload['figure'] = figure - file_info = _create_or_update(payload, 'plot') + file_info = _create_or_overwrite(payload, 'plot') # Compute viewing URL if sharing == 'secret': @@ -1081,7 +1081,7 @@ def upload(cls, grid, filename=None, if parent_path != '': payload['parent_path'] = parent_path - file_info = _create_or_update(payload, 'grid') + file_info = _create_or_overwrite(payload, 'grid') cols = file_info['cols'] fid = file_info['fid'] @@ -1431,10 +1431,10 @@ def get_grid(grid_url, raw=False): return Grid(parsed_content, fid) -def _create_or_update(data, filetype): +def _create_or_overwrite(data, filetype): """ - Create or update (if file exists) and grid, plot, spectacle, or dashboard - object + Create or overwrite (if file exists) and grid, plot, spectacle, + or dashboard object Parameters ---------- @@ -1466,27 +1466,22 @@ def _create_or_update(data, filetype): matching_file = json.loads(content) - if matching_file['filetype'] == filetype: - fid = matching_file['fid'] - res = api_module.update(fid, data) - else: - raise _plotly_utils.exceptions.PlotlyError(""" -'{filename}' is already a {other_filetype} in your account. -While you can overwrite {filetype}s with the same name, you can't overwrite -files with a different type. Try deleting '{filename}' in your account or -changing the filename.""".format( - filename=filename, - filetype=filetype, - other_filetype=matching_file['filetype'] - ) - ) + fid = matching_file['fid'] - except exceptions.PlotlyRequestError: - res = api_module.create(data) - else: - res = api_module.create(data) + # Delete fid + # This requires sending file to trash and then deleting it + res = api_module.trash(fid) + res.raise_for_status() + + res = api_module.permanent_delete(fid) + res.raise_for_status() + except exceptions.PlotlyRequestError as e: + # Raise on trash or permanent delete + # Pass through to try creating the file anyway + pass - # Check response + # Create file + res = api_module.create(data) res.raise_for_status() # Get resulting file content @@ -1576,7 +1571,7 @@ def upload(cls, dashboard, filename, sharing='public', auto_open=True): 'world_readable': world_readable } - file_info = _create_or_update(data, 'dashboard') + file_info = _create_or_overwrite(data, 'dashboard') url = file_info['web_url'] @@ -1674,7 +1669,7 @@ def upload(cls, presentation, filename, sharing='public', auto_open=True): 'world_readable': world_readable } - file_info = _create_or_update(data, 'spectacle_presentation') + file_info = _create_or_overwrite(data, 'spectacle_presentation') url = file_info['web_url'] From daa5bfcf3c79f3489c3f7f42efbe67b5b4180adf Mon Sep 17 00:00:00 2001 From: Jon Mease Date: Tue, 18 Jun 2019 08:35:05 -0400 Subject: [PATCH 2/2] Fix test URL expectation --- .../chart_studio/tests/test_plot_ly/test_stream/test_stream.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/python/chart-studio/chart_studio/tests/test_plot_ly/test_stream/test_stream.py b/packages/python/chart-studio/chart_studio/tests/test_plot_ly/test_stream/test_stream.py index 1e544b81185..2260fdd04d7 100644 --- a/packages/python/chart-studio/chart_studio/tests/test_plot_ly/test_stream/test_stream.py +++ b/packages/python/chart-studio/chart_studio/tests/test_plot_ly/test_stream/test_stream.py @@ -35,7 +35,7 @@ def test_initialize_stream_plot(self): auto_open=False, world_readable=True, filename='stream-test') - self.assertEqual('https://plot.ly/~PythonAPI/461/', url) + self.assertTrue(url.startswith('https://plot.ly/~PythonAPI/')) time.sleep(.5) @attr('slow')