Skip to content

Commit

Permalink
Code addressing #623
Browse files Browse the repository at this point in the history
  • Loading branch information
adkinsrs committed Aug 7, 2024
1 parent 852b41e commit ec0a1ca
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
32 changes: 32 additions & 0 deletions lib/geardb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1728,6 +1728,38 @@ def save(self):
cursor.close()
conn.close()

def remove(self):
"""
Deletes the current display from the database.
"""
conn = Connection()
cursor = conn.get_cursor()

# first remove any layout_display entries
qry = """
DELETE FROM layout_displays
WHERE display_id = %s
"""
cursor.execute(qry, (self.id,))

# Then remove from anly dataset_preference entries
qry = """
DELETE FROM dataset_preference
WHERE display_id = %s
"""
cursor.execute(qry, (self.id,))

# Then remove the display itself
qry = """
DELETE FROM dataset_display
WHERE id = %s
"""
cursor.execute(qry, (self.id,))

cursor.close()
conn.commit()
conn.close()

@dataclass
class Dataset:
id: str
Expand Down
45 changes: 43 additions & 2 deletions www/cgi/delete_dataset_analysis.cgi
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,59 @@ def main():
session_id = form.getvalue('session_id')
user = geardb.get_user_from_session_id(session_id)

if not user:
print('Content-Type: application/json\n\n')
print(json.dumps({'success': 0, 'error': 'User not found'}))
return

source_ana = geardb.Analysis(id=analysis_id, type=analysis_type,
dataset_id=dataset_id, session_id=session_id, user_id=user.id)
source_pipeline_base = source_ana.base_path()

result = {'success': 0}

# verify this analysis belongs to the user
if not os.path.exists(source_pipeline_base):
result = {'success': 0, 'error': 'Analysis not found'}
sys.stdout = original_stdout
print('Content-Type: application/json\n\n')
print(json.dumps(result))
return
if not source_ana.user_id == user.id:
result = {'success': 0, 'error': 'Analysis does not belong to user'}
sys.stdout = original_stdout
print('Content-Type: application/json\n\n')
print(json.dumps(result))
return

try:
# TODO: need to verify ownership here in the future before deleting
#print("DEBUG: would rmtree this:{0}".format(source_pipeline_base), file=sys.stderr)
# move directory to a backup in case of failures
#shutil.move(source_pipeline_base, source_pipeline_base + '.deleted')

# find any dataset displays with this analysis id in the plotly_config
"""
conn = Connection()
cursor = conn.get_cursor()
qry = "SELECT * from dataset_display where plotly_config like '%\"id\":\"{}\"%'".format(analysis_id)
cursor.execute(qry, (self.id,))
for (display_id, user_id, label, plot_type, plotly_config) in cursor:
display = DatasetDisplay(
id=display_id,
dataset_id=self.id,
user_id=user_id,
label=label,
plot_type=plot_type,
plotly_config=plotly_config
)
display.remove()
"""

#shutil.rmtree(source_pipeline_base + '.deleted')
shutil.rmtree(source_pipeline_base)
result = {'success': 1}
except:
# restore the backup
#shutil.move(source_pipeline_base + '.deleted', source_pipeline_base)
result = {'success': 0, 'error': 'Unable to delete dataset'}

sys.stdout = original_stdout
Expand Down

0 comments on commit ec0a1ca

Please sign in to comment.