From dff6c490c305a847868efc7b3a59c905f65d8586 Mon Sep 17 00:00:00 2001 From: Antonio Gonzalez Date: Sun, 3 May 2015 16:56:01 -0600 Subject: [PATCH 1/6] GUI modifications --- qiita_pet/handlers/analysis_handlers.py | 39 +++++++++++++++++++++-- qiita_pet/templates/analysis_results.html | 34 +++++++++++++++++++- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/qiita_pet/handlers/analysis_handlers.py b/qiita_pet/handlers/analysis_handlers.py index fa377366e..2204d87bf 100644 --- a/qiita_pet/handlers/analysis_handlers.py +++ b/qiita_pet/handlers/analysis_handlers.py @@ -156,21 +156,54 @@ def get(self, analysis_id): dropped[data_type].append({'study': Study(study).title, 'samples': samples}) - self.render("analysis_results.html", + self.render("analysis_results.html", analysis_id=analysis_id, jobres=jobres, aname=analysis.name, dropped=dropped, basefolder=get_db_files_base_dir()) + @authenticated + def post(self, analysis_id): + analysis_id = int(analysis_id.split("/")[0]) + analysis_id_sent = int(self.get_argument('analysis_id')) + action = self.get_argument('action') + + if analysis_id != analysis_id_sent or action != 'delete_analysis': + raise QiitaPetAuthorizationError( + self.current_user.id, + 'analysis/results/%d-delete' % analysis_id) + + analysis = Analysis(analysis_id) + check_analysis_access(self.current_user, analysis) + + try: + Analysis.delete(analysis_id) + msg = ("Analysis %s has been deleted." % ( + analysis.title)) + msg_level = "success" + except Exception as e: + print e + msg = ("Couldn't remove %s analysis: %s" % ( + analysis.name, str(e))) + msg_level = "danger" + + # redirecting to list of analysis but also passing messages and + # we need to change the request.method to GET + self.request.method = 'GET' + ShowAnalysesHandler(self.application, self.request)._execute( + [t(self.request) for t in self.application.transforms], + message=msg, msg_level=msg_level) + class ShowAnalysesHandler(BaseHandler): """Shows the user's analyses""" @authenticated - def get(self): + def get(self, message='', msg_level=''): user = self.current_user analyses = [Analysis(a) for a in user.shared_analyses | user.private_analyses] - self.render("show_analyses.html", analyses=analyses) + self.render("show_analyses.html", analyses=analyses, message=message, + msg_level=msg_level) class ResultsHandler(StaticFileHandler, BaseHandler): diff --git a/qiita_pet/templates/analysis_results.html b/qiita_pet/templates/analysis_results.html index 7fe20187f..006031734 100644 --- a/qiita_pet/templates/analysis_results.html +++ b/qiita_pet/templates/analysis_results.html @@ -11,7 +11,16 @@
-

 Analysis: {{aname}}

+ + + + + + +

 Analysis: {{aname}}

+

+
+
@@ -74,4 +83,27 @@

ERROR

+ + + {%end%} From b78e50a005bc2cffaca5b12b35c2f2323932d825 Mon Sep 17 00:00:00 2001 From: Antonio Gonzalez Date: Sun, 10 May 2015 15:27:53 -0600 Subject: [PATCH 2/6] fix #1091 --- qiita_db/analysis.py | 71 +++++++++++++++++++++++++++++++++- qiita_db/test/test_analysis.py | 18 ++++++++- 2 files changed, 87 insertions(+), 2 deletions(-) diff --git a/qiita_db/analysis.py b/qiita_db/analysis.py index 37fb276df..b6a10141f 100644 --- a/qiita_db/analysis.py +++ b/qiita_db/analysis.py @@ -32,7 +32,7 @@ from .base import QiitaStatusObject from .data import ProcessedData from .study import Study -from .exceptions import QiitaDBStatusError, QiitaDBError +from .exceptions import QiitaDBStatusError, QiitaDBError, QiitaDBUnknownIDError from .util import (convert_to_id, get_work_base_dir, get_mountpoint, insert_filepaths) @@ -66,9 +66,13 @@ class Analysis(QiitaStatusObject): unshare build_files summary_data + exists + create + delete """ _table = "analysis" + _analysis_id_column = 'analysis_id' def _lock_check(self, conn_handler): """Raises QiitaDBStatusError if analysis is not in_progress""" @@ -165,6 +169,71 @@ def create(cls, owner, name, description, parent=None, from_default=False): a_id = conn_handler.execute_queue(queue)[0] return cls(a_id) + @classmethod + def delete(cls, _id): + """Deletes an analysis + + Parameters + ---------- + _id : int + The analysis id + + Raises + ------ + QiitaDBUnknownIDError + If the analysis id doesn't exist + """ + # check if the analysis exist + if not cls.exists(_id): + raise QiitaDBUnknownIDError(_id, "analysis") + + queue = "delete_analysis_%d" % _id + conn_handler = SQLConnectionHandler() + conn_handler.create_queue(queue) + + sql = ("DELETE FROM qiita.analysis_filepath WHERE " + "{0} = {1}".format(cls._analysis_id_column, _id)) + conn_handler.add_to_queue(queue, sql) + + sql = ("DELETE FROM qiita.analysis_workflow WHERE " + "{0} = {1}".format(cls._analysis_id_column, _id)) + conn_handler.add_to_queue(queue, sql) + + sql = ("DELETE FROM qiita.analysis_sample WHERE " + "{0} = {1}".format(cls._analysis_id_column, _id)) + conn_handler.add_to_queue(queue, sql) + + sql = ("DELETE FROM qiita.collection_analysis WHERE " + "{0} = {1}".format(cls._analysis_id_column, _id)) + conn_handler.add_to_queue(queue, sql) + + sql = ("DELETE FROM qiita.{0} WHERE " + "{1} = {2}".format(cls._table, cls._analysis_id_column, _id)) + conn_handler.add_to_queue(queue, sql) + + conn_handler.execute_queue(queue) + + @classmethod + def exists(cls, analysis_id): + r"""Checks if the given analysis _id exists + + Parameters + ---------- + analysis_id : int + The id of the analysis we are searching for + + Returns + ------- + bool + True if exists, false otherwise. + """ + conn_handler = SQLConnectionHandler() + + return conn_handler.execute_fetchone( + "SELECT EXISTS(SELECT * FROM qiita.{0} WHERE " + "{1}=%s)".format(cls._table, cls._analysis_id_column), + (analysis_id, ))[0] + # ---- Properties ---- @property def owner(self): diff --git a/qiita_db/test/test_analysis.py b/qiita_db/test/test_analysis.py index f923c98c6..4db760aa8 100644 --- a/qiita_db/test/test_analysis.py +++ b/qiita_db/test/test_analysis.py @@ -12,7 +12,8 @@ from qiita_db.analysis import Analysis, Collection from qiita_db.job import Job from qiita_db.user import User -from qiita_db.exceptions import QiitaDBStatusError, QiitaDBError +from qiita_db.exceptions import (QiitaDBStatusError, QiitaDBError, + QiitaDBUnknownIDError) from qiita_db.util import get_mountpoint, get_count from qiita_db.study import Study, StudyPerson from qiita_db.data import ProcessedData @@ -145,6 +146,21 @@ def test_create_from_default(self): [new_id, 1, '1.SKM4.640180']] self.assertEqual(obs, exp) + def test_exists(self): + self.assertTrue(Analysis.exists(1)) + new_id = get_count("qiita.analysis") + 1 + self.assertFalse(Analysis.exists(new_id)) + + def test_delete(self): + # successful delete + total_analyses = get_count("qiita.analysis") + Analysis.delete(1) + self.assertEqual(total_analyses - 1, get_count("qiita.analysis")) + + # no possible to delete + with self.assertRaises(QiitaDBUnknownIDError): + Analysis.delete(total_analyses + 1) + def test_retrieve_owner(self): self.assertEqual(self.analysis.owner, "test@foo.bar") From 742f9f18ba17c14c7df2c8964441bb1afd52c0c3 Mon Sep 17 00:00:00 2001 From: Antonio Gonzalez Date: Sun, 10 May 2015 15:37:16 -0600 Subject: [PATCH 3/6] fix --- qiita_pet/handlers/analysis_handlers.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/qiita_pet/handlers/analysis_handlers.py b/qiita_pet/handlers/analysis_handlers.py index efd2b5d51..800f15ef0 100644 --- a/qiita_pet/handlers/analysis_handlers.py +++ b/qiita_pet/handlers/analysis_handlers.py @@ -170,17 +170,17 @@ def post(self, analysis_id): 'analysis/results/%d-delete' % analysis_id) analysis = Analysis(analysis_id) + analysis_name = analysis.name check_analysis_access(self.current_user, analysis) try: Analysis.delete(analysis_id) msg = ("Analysis %s has been deleted." % ( - analysis.title)) + analysis_name)) msg_level = "success" except Exception as e: - print e msg = ("Couldn't remove %s analysis: %s" % ( - analysis.name, str(e))) + analysis_name, str(e))) msg_level = "danger" # redirecting to list of analysis but also passing messages and From dc938f77eb4560bd582095e8e4c1cbd1e46ce344 Mon Sep 17 00:00:00 2001 From: Antonio Gonzalez Date: Wed, 13 May 2015 06:45:06 -0600 Subject: [PATCH 4/6] addressing suggestions from @josenavas --- qiita_pet/handlers/analysis_handlers.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/qiita_pet/handlers/analysis_handlers.py b/qiita_pet/handlers/analysis_handlers.py index 5734ce55f..3a5d13c90 100644 --- a/qiita_pet/handlers/analysis_handlers.py +++ b/qiita_pet/handlers/analysis_handlers.py @@ -177,31 +177,28 @@ def post(self, analysis_id): Analysis.delete(analysis_id) msg = ("Analysis %s has been deleted." % ( analysis_name)) - msg_level = "success" + level = "success" except Exception as e: msg = ("Couldn't remove %s analysis: %s" % ( analysis_name, str(e))) - msg_level = "danger" + level = "danger" - # redirecting to list of analysis but also passing messages and - # we need to change the request.method to GET - self.request.method = 'GET' - ShowAnalysesHandler(self.application, self.request)._execute( - [t(self.request) for t in self.application.transforms], - message=msg, msg_level=msg_level) + self.redirect(u"/analysis/show/?level=%s&message=%s" % (level, msg)) class ShowAnalysesHandler(BaseHandler): """Shows the user's analyses""" @authenticated - def get(self, message='', msg_level=''): + def get(self): + message = self.get_argument('message', '') + level = self.get_argument('level', '') user = self.current_user analyses = [Analysis(a) for a in user.shared_analyses | user.private_analyses] self.render("show_analyses.html", analyses=analyses, message=message, - msg_level=msg_level) + level=level) class ResultsHandler(StaticFileHandler, BaseHandler): From 368722e1afdca914351f8469bba893f24e5e8b7d Mon Sep 17 00:00:00 2001 From: Antonio Gonzalez Date: Wed, 13 May 2015 13:41:08 -0600 Subject: [PATCH 5/6] addressing comments --- qiita_db/analysis.py | 2 ++ qiita_db/test/test_analysis.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/qiita_db/analysis.py b/qiita_db/analysis.py index df9a6d233..890c814eb 100644 --- a/qiita_db/analysis.py +++ b/qiita_db/analysis.py @@ -207,6 +207,8 @@ def delete(cls, _id): "{0} = {1}".format(cls._analysis_id_column, _id)) conn_handler.add_to_queue(queue, sql) + # TODO: issue #1176 + sql = ("DELETE FROM qiita.{0} WHERE " "{1} = {2}".format(cls._table, cls._analysis_id_column, _id)) conn_handler.add_to_queue(queue, sql) diff --git a/qiita_db/test/test_analysis.py b/qiita_db/test/test_analysis.py index a7a2a081b..b7925519a 100644 --- a/qiita_db/test/test_analysis.py +++ b/qiita_db/test/test_analysis.py @@ -423,7 +423,7 @@ def test_build_mapping_file(self): exp = [[15, '1_analysis_mapping.txt', 9, '852952723', 1, 1], [new_id, '1_analysis_mapping.txt', 9, '2349935429', 1, 1]] - self.assertEqual(obs, exp) + self.assertItemsEqual(obs, exp) sql = """SELECT * FROM qiita.analysis_filepath WHERE analysis_id=%s ORDER BY filepath_id""" From 7e22a62cd1433d2f542062961a9b1cf2a32c0025 Mon Sep 17 00:00:00 2001 From: Antonio Gonzalez Date: Mon, 18 May 2015 11:35:04 -0600 Subject: [PATCH 6/6] addressing @adamrp comment --- qiita_pet/handlers/analysis_handlers.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/qiita_pet/handlers/analysis_handlers.py b/qiita_pet/handlers/analysis_handlers.py index cc10e40b2..b08fba82f 100644 --- a/qiita_pet/handlers/analysis_handlers.py +++ b/qiita_pet/handlers/analysis_handlers.py @@ -31,6 +31,7 @@ filepath_ids_to_rel_paths) from qiita_db.exceptions import QiitaDBUnknownIDError from qiita_db.study import Study +from qiita_db.logger import LogEntry SELECT_SAMPLES = 2 SELECT_COMMANDS = 3 @@ -178,9 +179,12 @@ def post(self, analysis_id): analysis_name)) level = "success" except Exception as e: + e = str(e) msg = ("Couldn't remove %s analysis: %s" % ( - analysis_name, str(e))) + analysis_name, e)) level = "danger" + LogEntry.create('Runtime', "Couldn't remove analysis ID %d: %s" % + (analysis_id, e)) self.redirect(u"/analysis/show/?level=%s&message=%s" % (level, msg))