Skip to content

V4 chart_studio: Delete existing file before creating rather than update #1625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 21 additions & 26 deletions packages/python/chart-studio/chart_studio/plotly/plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down Expand Up @@ -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']
Expand Down Expand Up @@ -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
----------
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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']

Expand Down Expand Up @@ -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']

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down