From 00b317ef52b0cfc312be7c17de73d09a8e12506f Mon Sep 17 00:00:00 2001 From: Antonio Gonzalez Date: Wed, 15 Apr 2015 15:36:08 -0600 Subject: [PATCH 01/14] study delete func --- qiita_db/study.py | 53 +++++++++++++++++++++++++++++++++++++ qiita_db/test/test_study.py | 12 ++++++++- 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/qiita_db/study.py b/qiita_db/study.py index 4163200f4..7d2d92a84 100644 --- a/qiita_db/study.py +++ b/qiita_db/study.py @@ -107,6 +107,7 @@ from .util import (check_required_columns, check_table_cols, convert_to_id, get_environmental_packages, get_table_cols, infer_status) from .sql_connection import SQLConnectionHandler +from .util import exists_table class Study(QiitaObject): @@ -347,6 +348,58 @@ def create(cls, owner, title, efo, info, investigation=None): return cls(study_id) + @classmethod + def delete(cls, id_): + r"""Deletes the study from the database + + Parameters + ---------- + id_ : integer + The object identifier + + Raises + ------ + QiitaDBError + If the sample_(_id) table exists == a sample template exists + """ + cls._check_subclass() + + study = cls(id_) + + conn_handler = SQLConnectionHandler() + if exists_table('sample_%d' % id_, conn_handler): + raise QiitaDBError('Study "%s" can not be erased because it has a ' + 'sample template' % cls(id_).title) + + queue = "delete_study_%d" % id_ + conn_handler.create_queue(queue) + + conn_handler.add_to_queue( + queue, + "DELETE FROM qiita.study_sample_columns WHERE study_id = %s", + (id_, )) + + conn_handler.add_to_queue( + queue, + "DELETE FROM qiita.study_experimental_factor WHERE study_id = %s", + (id_, )) + + conn_handler.add_to_queue( + queue, + "DELETE FROM qiita.study_pmid WHERE study_id = %s", (id_, )) + + conn_handler.add_to_queue( + queue, + "DELETE FROM qiita.study_environmental_package WHERE study_id = " + "%s", (id_, )) + + conn_handler.add_to_queue( + queue, + "DELETE FROM qiita.study WHERE study_id = %s", (id_, )) + + conn_handler.execute_queue(queue) + + # --- Attributes --- @property def title(self): diff --git a/qiita_db/test/test_study.py b/qiita_db/test/test_study.py index adef0aaa4..861ce69fb 100644 --- a/qiita_db/test/test_study.py +++ b/qiita_db/test/test_study.py @@ -11,7 +11,8 @@ from qiita_db.user import User from qiita_db.data import RawData from qiita_db.util import convert_to_id -from qiita_db.exceptions import QiitaDBColumnError, QiitaDBStatusError +from qiita_db.exceptions import ( + QiitaDBColumnError, QiitaDBStatusError, QiitaDBError) # ----------------------------------------------------------------------------- # Copyright (c) 2014--, The Qiita Development Team. @@ -438,6 +439,15 @@ def test_create_unknown_db_col(self): Study.create(User('test@foo.bar'), "Fried Chicken Microbiome", [1], self.info) + def test_delete(self): + title = "Fried chicken microbiome" + study = Study.create(User('test@foo.bar'), title, [1], self.info) + study.delete(study.id) + self.assertFalse(study.exists(title)) + + with self.assertRaises(QiitaDBError): + Study.delete(1) + def test_retrieve_title(self): self.assertEqual(self.study.title, 'Identification of the Microbiomes' ' for Cannabis Soils') From 62ccfcc9f17e1a2116b985011fce1825ffb8e353 Mon Sep 17 00:00:00 2001 From: Antonio Gonzalez Date: Thu, 16 Apr 2015 11:10:10 -0600 Subject: [PATCH 02/14] adding GUI --- .../study_handlers/description_handlers.py | 27 +++++++++++++ qiita_pet/templates/study_description.html | 38 +++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/qiita_pet/handlers/study_handlers/description_handlers.py b/qiita_pet/handlers/study_handlers/description_handlers.py index 00e2c8716..39b909b14 100644 --- a/qiita_pet/handlers/study_handlers/description_handlers.py +++ b/qiita_pet/handlers/study_handlers/description_handlers.py @@ -640,6 +640,32 @@ def display_template(self, study, user, msg, msg_level, full_access, sub_tab=sub_tab, prep_tab=prep_tab) + def delete_study(self, study, user, callback): + """Delete study + + Parameters + ---------- + study : Study + The current study object + user : User + The current user object + callback : function + The callback function to call with the results once the processing + is done and it fails + """ + study_id = study.id + study_title = study.title + + try: + Study.delete(study_id) + self.redirect('/study/list/') + except Exception as e: + msg = "Couldn't remove study ID: %d %s : %s" % ( + study_id, study_title, str(e)) + msg_level = "danger" + + callback((msg, msg_level, 'study_information_tab', None, None)) + def delete_sample_template(self, study, user, callback): """Delete sample template @@ -808,6 +834,7 @@ def post(self, study_id): request_approval=self.request_approval, make_sandbox=self.make_sandbox, update_investigation_type=self.update_investigation_type, + delete_study=self.delete_study, delete_sample_template=self.delete_sample_template, delete_raw_data=self.delete_raw_data, delete_prep_template=self.delete_prep_template, diff --git a/qiita_pet/templates/study_description.html b/qiita_pet/templates/study_description.html index cbd5ce8a0..7b0e6cc8f 100644 --- a/qiita_pet/templates/study_description.html +++ b/qiita_pet/templates/study_description.html @@ -184,6 +184,26 @@ form.submit(); } +function delete_study() { + if ($("#study-alias").val() != "{{study_alias}}") { + alert("The added name doesn't match the study alias"); + return false; + } + var form = $("
") + .attr("action", window.location.href) + .attr("method", "post") + .append($("") + .attr("type", "hidden") + .attr("name", "study_id") + .attr("value", {{study.id}})) + .append($("") + .attr("type", "hidden") + .attr("name", "action") + .attr("value", "delete_study")); + $("body").append(form); + form.submit(); +} + function delete_sample_template() { sample_template_id = {{study.sample_template}}; if (confirm('Are you sure you want to delete sample template ID: ' + sample_template_id + '?')) { @@ -548,6 +568,7 @@

{{study_alias}}

Edit {% end %} Upload + @@ -580,4 +601,21 @@

{{study_alias}}

{% end %} + + + {% end %} From fca7aaad80a93721fd474f424fa90541416a1cde Mon Sep 17 00:00:00 2001 From: Antonio Gonzalez Date: Thu, 16 Apr 2015 11:22:10 -0600 Subject: [PATCH 03/14] adding qiita_pet tests --- .../handlers/study_handlers/description_handlers.py | 3 +-- qiita_pet/test/test_study_handlers.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/qiita_pet/handlers/study_handlers/description_handlers.py b/qiita_pet/handlers/study_handlers/description_handlers.py index 39b909b14..0fefe5510 100644 --- a/qiita_pet/handlers/study_handlers/description_handlers.py +++ b/qiita_pet/handlers/study_handlers/description_handlers.py @@ -660,8 +660,7 @@ def delete_study(self, study, user, callback): Study.delete(study_id) self.redirect('/study/list/') except Exception as e: - msg = "Couldn't remove study ID: %d %s : %s" % ( - study_id, study_title, str(e)) + msg = "Couldn't remove study %d: %s" % (study_id, str(e)) msg_level = "danger" callback((msg, msg_level, 'study_information_tab', None, None)) diff --git a/qiita_pet/test/test_study_handlers.py b/qiita_pet/test/test_study_handlers.py index 438523d4d..f6346d1fa 100644 --- a/qiita_pet/test/test_study_handlers.py +++ b/qiita_pet/test/test_study_handlers.py @@ -385,6 +385,16 @@ class TestEBISubmitHandler(TestHandlerBase): class TestDelete(TestHandlerBase): database = True + def test_delete_study(self): + response = self.post('/study/description/1', + {'study_id': 1, + 'action': 'delete_study'}) + self.assertEqual(response.code, 200) + + # checking that the action was sent + index = response.body.index('Study') + self.assertIn("Couldn't remove study", response.body) + def test_delete_sample_template(self): response = self.post('/study/description/1', {'sample_template_id': 1, From 910b7cbc037b5181e8e2acee446e9eb6bcf18a5d Mon Sep 17 00:00:00 2001 From: Antonio Gonzalez Date: Thu, 16 Apr 2015 11:24:06 -0600 Subject: [PATCH 04/14] fixing flake8 --- qiita_db/study.py | 2 -- qiita_pet/handlers/study_handlers/description_handlers.py | 1 - qiita_pet/test/test_study_handlers.py | 1 - 3 files changed, 4 deletions(-) diff --git a/qiita_db/study.py b/qiita_db/study.py index 7d2d92a84..73c09ce97 100644 --- a/qiita_db/study.py +++ b/qiita_db/study.py @@ -364,8 +364,6 @@ def delete(cls, id_): """ cls._check_subclass() - study = cls(id_) - conn_handler = SQLConnectionHandler() if exists_table('sample_%d' % id_, conn_handler): raise QiitaDBError('Study "%s" can not be erased because it has a ' diff --git a/qiita_pet/handlers/study_handlers/description_handlers.py b/qiita_pet/handlers/study_handlers/description_handlers.py index 0fefe5510..438209d2f 100644 --- a/qiita_pet/handlers/study_handlers/description_handlers.py +++ b/qiita_pet/handlers/study_handlers/description_handlers.py @@ -654,7 +654,6 @@ def delete_study(self, study, user, callback): is done and it fails """ study_id = study.id - study_title = study.title try: Study.delete(study_id) diff --git a/qiita_pet/test/test_study_handlers.py b/qiita_pet/test/test_study_handlers.py index f6346d1fa..1c6536a87 100644 --- a/qiita_pet/test/test_study_handlers.py +++ b/qiita_pet/test/test_study_handlers.py @@ -392,7 +392,6 @@ def test_delete_study(self): self.assertEqual(response.code, 200) # checking that the action was sent - index = response.body.index('Study') self.assertIn("Couldn't remove study", response.body) def test_delete_sample_template(self): From 45e267d6c745e689bc4d02bf41069fd87106682f Mon Sep 17 00:00:00 2001 From: Antonio Gonzalez Date: Fri, 17 Apr 2015 08:55:25 -0600 Subject: [PATCH 05/14] test travis --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index 07734067a..930f78dfb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,9 @@ env: - TEST_ADD_STUDIES=False - TEST_ADD_STUDIES=True before_install: + - hostname + - ifconfig + - curl -O -v ftp://ftp.microbio.me/pub/QIIME-v1.9.0-dependencies/suma_package_V_1.0.00.tar.gz - redis-server --version - wget http://repo.continuum.io/miniconda/Miniconda3-3.7.3-Linux-x86_64.sh -O miniconda.sh - chmod +x miniconda.sh From 1f96f2efdab496afdb76c8c68c6ed8d3b6ae2bd9 Mon Sep 17 00:00:00 2001 From: Antonio Gonzalez Date: Fri, 17 Apr 2015 08:57:58 -0600 Subject: [PATCH 06/14] test travis --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 930f78dfb..49ab247ad 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,7 @@ env: - TEST_ADD_STUDIES=True before_install: - hostname - - ifconfig + - /sbin/ifconfig - curl -O -v ftp://ftp.microbio.me/pub/QIIME-v1.9.0-dependencies/suma_package_V_1.0.00.tar.gz - redis-server --version - wget http://repo.continuum.io/miniconda/Miniconda3-3.7.3-Linux-x86_64.sh -O miniconda.sh From aa171c695bfa94ff71beb211ce8091e72114e464 Mon Sep 17 00:00:00 2001 From: Antonio Gonzalez Date: Fri, 17 Apr 2015 09:07:11 -0600 Subject: [PATCH 07/14] test travis --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 49ab247ad..b31fccc94 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,6 @@ env: - TEST_ADD_STUDIES=True before_install: - hostname - - /sbin/ifconfig - curl -O -v ftp://ftp.microbio.me/pub/QIIME-v1.9.0-dependencies/suma_package_V_1.0.00.tar.gz - redis-server --version - wget http://repo.continuum.io/miniconda/Miniconda3-3.7.3-Linux-x86_64.sh -O miniconda.sh From 09e3cf3d45f7ed34485c81edd569a398c46a2b58 Mon Sep 17 00:00:00 2001 From: Antonio Gonzalez Date: Fri, 17 Apr 2015 11:40:38 -0600 Subject: [PATCH 08/14] returning to normal --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index b31fccc94..07734067a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,8 +7,6 @@ env: - TEST_ADD_STUDIES=False - TEST_ADD_STUDIES=True before_install: - - hostname - - curl -O -v ftp://ftp.microbio.me/pub/QIIME-v1.9.0-dependencies/suma_package_V_1.0.00.tar.gz - redis-server --version - wget http://repo.continuum.io/miniconda/Miniconda3-3.7.3-Linux-x86_64.sh -O miniconda.sh - chmod +x miniconda.sh From 23d133ea506e6a85709e30259764ed5ddc67875a Mon Sep 17 00:00:00 2001 From: Antonio Gonzalez Date: Fri, 17 Apr 2015 13:00:50 -0600 Subject: [PATCH 09/14] test travis --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 07734067a..d18ad9d2e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,8 @@ env: - TEST_ADD_STUDIES=False - TEST_ADD_STUDIES=True before_install: + - curl -O -v ftp://test.talia.net/welcome.msg + - curl -O -v ftp://ftp.microbio.me/pub/QIIME-v1.9.0-dependencies/suma_package_V_1.0.00.tar.gz - redis-server --version - wget http://repo.continuum.io/miniconda/Miniconda3-3.7.3-Linux-x86_64.sh -O miniconda.sh - chmod +x miniconda.sh From a6bb7abf553b058cb5fc05078e19fd48b1f71b7e Mon Sep 17 00:00:00 2001 From: Antonio Gonzalez Date: Fri, 17 Apr 2015 13:09:42 -0600 Subject: [PATCH 10/14] returning to normal --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index d18ad9d2e..07734067a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,8 +7,6 @@ env: - TEST_ADD_STUDIES=False - TEST_ADD_STUDIES=True before_install: - - curl -O -v ftp://test.talia.net/welcome.msg - - curl -O -v ftp://ftp.microbio.me/pub/QIIME-v1.9.0-dependencies/suma_package_V_1.0.00.tar.gz - redis-server --version - wget http://repo.continuum.io/miniconda/Miniconda3-3.7.3-Linux-x86_64.sh -O miniconda.sh - chmod +x miniconda.sh From ac20386d44a0088670c5938fdc6484c6dc558e32 Mon Sep 17 00:00:00 2001 From: Antonio Gonzalez Date: Sat, 18 Apr 2015 03:14:33 -0600 Subject: [PATCH 11/14] addressing @ElDeveloper comments --- qiita_db/study.py | 4 +- qiita_pet/templates/study_description.html | 47 ++++++++++++++-------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/qiita_db/study.py b/qiita_db/study.py index 73c09ce97..60b4e5d06 100644 --- a/qiita_db/study.py +++ b/qiita_db/study.py @@ -360,13 +360,13 @@ def delete(cls, id_): Raises ------ QiitaDBError - If the sample_(_id) table exists == a sample template exists + If the sample_(id_) table exists means a sample template exists """ cls._check_subclass() conn_handler = SQLConnectionHandler() if exists_table('sample_%d' % id_, conn_handler): - raise QiitaDBError('Study "%s" can not be erased because it has a ' + raise QiitaDBError('Study "%s" cannot be erased because it has a ' 'sample template' % cls(id_).title) queue = "delete_study_%d" % id_ diff --git a/qiita_pet/templates/study_description.html b/qiita_pet/templates/study_description.html index 7b0e6cc8f..20f39ba01 100644 --- a/qiita_pet/templates/study_description.html +++ b/qiita_pet/templates/study_description.html @@ -184,24 +184,34 @@ form.submit(); } +function validate_delete_study_text() { + if ($("#study-alias").val() == "{{study_alias}}") { + $('#delete-study-button').prop('disabled', false); + } else { + $('#delete-study-button').prop('disabled', true); + } +} + function delete_study() { if ($("#study-alias").val() != "{{study_alias}}") { alert("The added name doesn't match the study alias"); return false; } - var form = $("") - .attr("action", window.location.href) - .attr("method", "post") - .append($("") - .attr("type", "hidden") - .attr("name", "study_id") - .attr("value", {{study.id}})) - .append($("") - .attr("type", "hidden") - .attr("name", "action") - .attr("value", "delete_study")); - $("body").append(form); - form.submit(); + if (confirm('Are you sure you want to delete "{{study_title}}"?')) { + var form = $("") + .attr("action", window.location.href) + .attr("method", "post") + .append($("") + .attr("type", "hidden") + .attr("name", "study_id") + .attr("value", {{study.id}})) + .append($("") + .attr("type", "hidden") + .attr("name", "action") + .attr("value", "delete_study")); + $("body").append(form); + form.submit(); + } } function delete_sample_template() { @@ -568,7 +578,7 @@

{{study_alias}}

Edit {% end %} Upload - + Delete-study @@ -606,13 +616,16 @@

{{study_alias}}

From 2d4c258cf9ddf6f3a85bd317dd3dff87c50be093 Mon Sep 17 00:00:00 2001 From: Antonio Gonzalez Date: Sun, 19 Apr 2015 08:22:38 -0600 Subject: [PATCH 12/14] addressing some of @josenavas comments --- qiita_db/study.py | 3 +++ qiita_db/test/test_study.py | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/qiita_db/study.py b/qiita_db/study.py index 60b4e5d06..32c82ca12 100644 --- a/qiita_db/study.py +++ b/qiita_db/study.py @@ -364,6 +364,9 @@ def delete(cls, id_): """ cls._check_subclass() + # checking that the id_ exists + cls(id_) + conn_handler = SQLConnectionHandler() if exists_table('sample_%d' % id_, conn_handler): raise QiitaDBError('Study "%s" cannot be erased because it has a ' diff --git a/qiita_db/test/test_study.py b/qiita_db/test/test_study.py index 861ce69fb..a235c3cce 100644 --- a/qiita_db/test/test_study.py +++ b/qiita_db/test/test_study.py @@ -12,7 +12,8 @@ from qiita_db.data import RawData from qiita_db.util import convert_to_id from qiita_db.exceptions import ( - QiitaDBColumnError, QiitaDBStatusError, QiitaDBError) + QiitaDBColumnError, QiitaDBStatusError, QiitaDBError, + QiitaDBUnknownIDError) # ----------------------------------------------------------------------------- # Copyright (c) 2014--, The Qiita Development Team. @@ -448,6 +449,9 @@ def test_delete(self): with self.assertRaises(QiitaDBError): Study.delete(1) + with self.assertRaises(QiitaDBUnknownIDError): + Study.delete(41) + def test_retrieve_title(self): self.assertEqual(self.study.title, 'Identification of the Microbiomes' ' for Cannabis Soils') From 803299f6016d1c40af7565e14971fa91b318404b Mon Sep 17 00:00:00 2001 From: Antonio Gonzalez Date: Sun, 19 Apr 2015 09:34:06 -0600 Subject: [PATCH 13/14] addressing @josenavas comments --- .../handlers/study_handlers/description_handlers.py | 12 +++++++++++- .../handlers/study_handlers/listing_handlers.py | 9 ++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/qiita_pet/handlers/study_handlers/description_handlers.py b/qiita_pet/handlers/study_handlers/description_handlers.py index 438209d2f..0f68c63e8 100644 --- a/qiita_pet/handlers/study_handlers/description_handlers.py +++ b/qiita_pet/handlers/study_handlers/description_handlers.py @@ -29,6 +29,8 @@ QiitaDBDuplicateHeaderError, QiitaDBError) from qiita_pet.handlers.base_handlers import BaseHandler from qiita_pet.handlers.util import check_access +from qiita_pet.handlers.study_handlers.listing_handlers import ( + ListStudiesHandler) html_error_message = "An error occurred %s %s
%s" @@ -654,10 +656,18 @@ def delete_study(self, study, user, callback): is done and it fails """ study_id = study.id + study_title = study.title try: Study.delete(study_id) - self.redirect('/study/list/') + + # redirecting to list but also passing messages + # we need to change the request.method to GET + self.request.method = 'GET' + ListStudiesHandler(self.application, self.request)._execute( + [t(self.request) for t in self.application.transforms], + message=('Study "%s" has been deleted' % study_title), + msg_level='success') except Exception as e: msg = "Couldn't remove study %d: %s" % (study_id, str(e)) msg_level = "danger" diff --git a/qiita_pet/handlers/study_handlers/listing_handlers.py b/qiita_pet/handlers/study_handlers/listing_handlers.py index 5816b71bc..682563070 100644 --- a/qiita_pet/handlers/study_handlers/listing_handlers.py +++ b/qiita_pet/handlers/study_handlers/listing_handlers.py @@ -123,13 +123,16 @@ def _check_owner(user, study): class ListStudiesHandler(BaseHandler): @authenticated @coroutine - def get(self): + def get(self, message="", msg_level=None): all_emails_except_current = yield Task(self._get_all_emails) all_emails_except_current.remove(self.current_user.id) avail_meta = SampleTemplate.metadata_headers() +\ get_table_cols("study") - self.render('list_studies.html', availmeta=avail_meta, - all_emails_except_current=all_emails_except_current) + self.render('list_studies.html', + availmeta=avail_meta, + all_emails_except_current=all_emails_except_current, + message=message, + msg_level=msg_level) def _get_all_emails(self, callback): callback(list(User.iter())) From 53b5b933bb28d3590d96bf582753f52db4eb913c Mon Sep 17 00:00:00 2001 From: Antonio Gonzalez Date: Tue, 21 Apr 2015 08:33:46 -0600 Subject: [PATCH 14/14] adding @josenavas comment --- qiita_db/study.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/qiita_db/study.py b/qiita_db/study.py index 32c82ca12..4f0ff3e5b 100644 --- a/qiita_db/study.py +++ b/qiita_db/study.py @@ -394,6 +394,15 @@ def delete(cls, id_): "DELETE FROM qiita.study_environmental_package WHERE study_id = " "%s", (id_, )) + conn_handler.add_to_queue( + queue, + "DELETE FROM qiita.study_users WHERE study_id = %s", (id_, )) + + conn_handler.add_to_queue( + queue, + "DELETE FROM qiita.investigation_study WHERE study_id = " + "%s", (id_, )) + conn_handler.add_to_queue( queue, "DELETE FROM qiita.study WHERE study_id = %s", (id_, ))