Skip to content

Commit

Permalink
Merge pull request #1034 from antgonza/delete-process-data
Browse files Browse the repository at this point in the history
Delete process data
  • Loading branch information
squirrelo committed Apr 3, 2015
2 parents 0744eed + c195dad commit 2199bdd
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 5 deletions.
55 changes: 55 additions & 0 deletions qiita_db/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,61 @@ def create(cls, processed_params_table, processed_params_id, filepaths,
pd.add_filepaths(filepaths, conn_handler)
return cls(pd_id)

@classmethod
def delete(cls, processed_data_id):
"""Removes the processed data with id processed_data_id
Parameters
----------
processed_data_id : int
The processed data id
Raises
------
QiitaDBStatusError
If the processed data status is not sandbox
QiitaDBError
If the processed data has (meta)analyses
"""
if cls(processed_data_id).status != 'sandbox':
raise QiitaDBStatusError(
"Illegal operation on non sandbox processed data")

conn_handler = SQLConnectionHandler()

analyses = [str(n[0]) for n in conn_handler.execute_fetchall(
"SELECT DISTINCT name FROM qiita.analysis JOIN "
"qiita.analysis_sample USING (analysis_id) WHERE "
"processed_data_id = {0} ORDER BY name".format(processed_data_id))]

if analyses:
raise QiitaDBError(
"Processed data %d cannot be removed because it is linked to "
"the following (meta)analysis: %s" % (processed_data_id,
', '.join(analyses)))

# delete
queue = "delete_processed_data_%d" % processed_data_id
conn_handler.create_queue(queue)

sql = ("DELETE FROM qiita.preprocessed_processed_data WHERE "
"processed_data_id = {0}".format(processed_data_id))
conn_handler.add_to_queue(queue, sql)

sql = ("DELETE FROM qiita.processed_filepath WHERE "
"processed_data_id = {0}".format(processed_data_id))
conn_handler.add_to_queue(queue, sql)

sql = ("DELETE FROM qiita.study_processed_data WHERE "
"processed_data_id = {0}".format(processed_data_id))
conn_handler.add_to_queue(queue, sql)

sql = ("DELETE FROM qiita.processed_data WHERE "
"processed_data_id = {0}".format(processed_data_id))
conn_handler.add_to_queue(queue, sql)

conn_handler.execute_queue(queue)

@property
def preprocessed_data(self):
r"""The preprocessed data id used to generate the processed data"""
Expand Down
23 changes: 23 additions & 0 deletions qiita_db/test/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,29 @@ def test_create(self):
# preprocessed_data_id, processed_Data_id
self.assertEqual(obs, [[1, 2]])

def test_delete(self):
"""Correctly deletes a processed data"""
# testing regular delete
pd = ProcessedData.create(self.params_table, self.params_id,
self.filepaths,
preprocessed_data=self.preprocessed_data,
processed_date=self.date)
ProcessedData.delete(pd.id)

# testing that it raises an error if ID doesn't exist
with self.assertRaises(QiitaDBUnknownIDError):
ProcessedData.delete(pd.id)

# testing that we can not remove cause the processed data != sandbox
with self.assertRaises(QiitaDBStatusError):
ProcessedData.delete(1)

# testing that we can not remove cause processed data has analyses
pd = ProcessedData(1)
pd.status = 'sandbox'
with self.assertRaises(QiitaDBError):
ProcessedData.delete(1)

def test_create_no_date(self):
"""Correctly adds a processed data with no date on it"""
# All the other settings have been already tested on test_create
Expand Down
30 changes: 29 additions & 1 deletion qiita_pet/handlers/study_handlers/description_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,33 @@ def delete_prep_template(self, study, user, callback):

callback((msg, msg_level, 'raw_data_tab', prep_id, None))

def delete_processed_data(self, study, user, callback):
"""Delete the selected processed data
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
"""
pd_id = int(self.get_argument('processed_data_id'))

try:
ProcessedData.delete(pd_id)
msg = ("Processed data %d has been deleted" % pd_id)
msg_level = "success"
pd_id = None
except Exception as e:
msg = ("Couldn't remove processed data %d: %s" %
(pd_id, str(e)))
msg_level = "danger"

callback((msg, msg_level, 'processed_data_tab', pd_id, None))

@authenticated
def get(self, study_id):
study, user, full_access = self._get_study_and_check_access(study_id)
Expand Down Expand Up @@ -728,7 +755,8 @@ def post(self, study_id):
make_sandbox=self.make_sandbox,
update_investigation_type=self.update_investigation_type,
delete_raw_data=self.delete_raw_data,
delete_prep_template=self.delete_prep_template)
delete_prep_template=self.delete_prep_template,
delete_processed_data=self.delete_processed_data)

# Get the action that we need to perform
action = self.get_argument("action", None)
Expand Down
20 changes: 20 additions & 0 deletions qiita_pet/templates/study_description.html
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,26 @@
}
}

function delete_processed_data(processed_data_id) {
if (confirm('Are you sure you want to delete processed data ID: ' + processed_data_id + '?')) {
var form = $("<form>")
.attr("action", window.location.href)
.attr("method", "post")
.append($("<input>")
.attr("type", "hidden")
.attr("name", "processed_data_id")
.attr("value", processed_data_id))
.append($("<input>")
.attr("type", "hidden")
.attr("name", "action")
.attr("value", "delete_processed_data"));
$("body").append(form);
form.submit();
} else {
return false;
}
}

function make_public(pd_id) {
if (confirm("Are you sure you want to make this study public?")) {
var form = $("<form>")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
<li class="active"><a href="#add_processed_data_tab" role="tab" data-toggle="tab">Add processed data</a></li>
<!-- Create the tabs for each processed data -->
{% for pd_id, pd, (class_icon1, class_icon2, color) in available_processed_data %}
<li><a href="#processed_data_info_{{pd_id}}" role="tab" data-toggle="tab">ID: {{pd_id}}&nbsp;
<div class="{{class_icon1}}" style="color: {{color}};"></div>
<div class="{{class_icon2}}" style="color: {{color}};"></div>
</a></li>
<li>
<a href="#processed_data_info_{{pd_id}}" role="tab" data-toggle="tab">ID: {{pd_id}}&nbsp;
<div class="{{class_icon1}}" style="color: {{color}};"></div>
<div class="{{class_icon2}}" style="color: {{color}};"></div>
<button class="close" title="Remove this processed data" type="button" onclick="delete_processed_data({{pd_id}})">&nbsp; ×</button>
</a>
</li>
{% end %}
</ul>
<!-- Create the tab panes -->
Expand Down

0 comments on commit 2199bdd

Please sign in to comment.