Skip to content
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
31 changes: 8 additions & 23 deletions qiita_pet/handlers/api_proxy/artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
from qiita_core.qiita_settings import qiita_config
from qiita_pet.handlers.api_proxy.util import check_access, check_fp
from qiita_ware.context import safe_submit
from qiita_ware.dispatchable import delete_artifact
from qiita_ware.dispatchable import (create_raw_data, copy_raw_data,
delete_artifact)
from qiita_db.artifact import Artifact
from qiita_db.user import User
from qiita_db.metadata_template.prep_template import PrepTemplate
Expand Down Expand Up @@ -293,16 +294,7 @@ def artifact_post_req(user_id, filepaths, artifact_type, name,

if artifact_id:
# if the artifact id has been provided, import the artifact
try:
artifact = Artifact.copy(Artifact(artifact_id), prep)
except Exception as e:
# We should hit this exception rarely (that's why it is an
# exception) since at this point we have done multiple checks.
# However, it can occur in weird cases, so better let the GUI know
# that this failed
return {'status': 'error',
'message': "Error creating artifact: %s" % str(e)}

job_id = safe_submit(user_id, copy_raw_data, prep, artifact_id)
else:
uploads_path = get_mountpoint('uploads')[0][1]
path_builder = partial(join, uploads_path, str(study_id))
Expand Down Expand Up @@ -330,20 +322,13 @@ def artifact_post_req(user_id, filepaths, artifact_type, name,
return {'status': 'error',
'message': "Can't create artifact, no files provided."}

try:
artifact = Artifact.create(cleaned_filepaths, artifact_type,
name=name, prep_template=prep)
except Exception as e:
# We should hit this exception rarely (that's why it is an
# exception) since at this point we have done multiple checks.
# However, it can occur in weird cases, so better let the GUI know
# that this failed
return {'status': 'error',
'message': "Error creating artifact: %s" % str(e)}
job_id = safe_submit(user_id, create_raw_data, artifact_type, prep,
cleaned_filepaths, name=name)

r_client.set(PREP_TEMPLATE_KEY_FORMAT % prep.id, job_id)

return {'status': 'success',
'message': '',
'artifact': artifact.id}
'message': ''}


def artifact_patch_request(user_id, req_op, req_path, req_value=None,
Expand Down
31 changes: 16 additions & 15 deletions qiita_pet/handlers/api_proxy/prep_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,22 @@ def prep_template_ajax_get_req(user_id, prep_id):
# Currently there is no name attribute, but it will be soon
name = "Prep information %d" % prep_id
pt = PrepTemplate(prep_id)

job_id = r_client.get(PREP_TEMPLATE_KEY_FORMAT % prep_id)
if job_id:
redis_info = loads(r_client.get(job_id))
processing = redis_info['status_msg'] == 'Running'
if processing:
alert_type = 'info'
alert_msg = 'This prep template is currently being updated'
else:
alert_type = redis_info['return']['status']
alert_msg = redis_info['return']['message'].replace('\n', '</br>')
else:
processing = False
alert_type = ''
alert_msg = ''

artifact_attached = pt.artifact is not None
study_id = pt.study_id
files = [f for _, f in get_files_from_uploads_folders(study_id)
Expand All @@ -130,21 +146,6 @@ def prep_template_ajax_get_req(user_id, prep_id):

ontology = _get_ENA_ontology()

job_id = r_client.get(PREP_TEMPLATE_KEY_FORMAT % prep_id)
if job_id:
redis_info = loads(r_client.get(job_id))
processing = redis_info['status_msg'] == 'Running'
if processing:
alert_type = 'info'
alert_msg = 'This prep template is currently being updated'
else:
alert_type = redis_info['return']['status']
alert_msg = redis_info['return']['message'].replace('\n', '</br>')
else:
processing = False
alert_type = ''
alert_msg = ''

editable = Study(study_id).can_edit(User(user_id)) and not processing

return {'status': 'success',
Expand Down
37 changes: 18 additions & 19 deletions qiita_pet/handlers/api_proxy/tests/test_artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,16 @@ def test_artifact_post_req(self):
obs = artifact_post_req(
'test@foo.bar', filepaths, 'FASTQ', 'New Test Artifact', pt.id)
exp = {'status': 'success',
'message': '',
'artifact': new_artifact_id}
'message': ''}
self.assertEqual(obs, exp)

obs = r_client.get('prep_template_%d' % pt.id)
self.assertIsNotNone(obs)
redis_info = loads(r_client.get(obs))
while redis_info['status_msg'] == 'Running':
sleep(0.05)
redis_info = loads(r_client.get(obs))

# Instantiate the artifact to make sure it was made and
# to clean the environment
a = Artifact(new_artifact_id)
Expand All @@ -377,12 +384,18 @@ def test_artifact_post_req(self):
'test@foo.bar', {}, 'FASTQ', 'New Test Artifact 2', pt.id,
new_artifact_id)
exp = {'status': 'success',
'message': '',
'artifact': new_artifact_id_2}
'message': ''}
self.assertEqual(obs, exp)

obs = r_client.get('prep_template_%d' % pt.id)
self.assertIsNotNone(obs)
redis_info = loads(r_client.get(obs))
while redis_info['status_msg'] == 'Running':
sleep(0.05)
redis_info = loads(r_client.get(obs))
# Instantiate the artifact to make sure it was made and
# to clean the environment
a = Artifact(new_artifact_id)
a = Artifact(new_artifact_id_2)
self._files_to_remove.extend([fp for _, fp, _ in a.filepaths])

def test_artifact_post_req_error(self):
Expand Down Expand Up @@ -420,20 +433,6 @@ def test_artifact_post_req_error(self):
'message': "Can't create artifact, no files provided."}
self.assertEqual(obs, exp)

# Exception
obs = artifact_post_req(user_id, filepaths, artifact_type, name, 1)
exp = {'status': 'error',
'message': "Error creating artifact: Prep template 1 already "
"has an artifact associated"}
self.assertEqual(obs, exp)

# Exception
obs = artifact_post_req(user_id, {}, artifact_type, name, 1, 1)
exp = {'status': 'error',
'message': "Error creating artifact: Prep template 1 already "
"has an artifact associated"}
self.assertEqual(obs, exp)

def test_artifact_status_put_req(self):
obs = artifact_status_put_req(1, 'test@foo.bar', 'sandbox')
exp = {'status': 'success',
Expand Down
2 changes: 2 additions & 0 deletions qiita_pet/templates/study_ajax/prep_summary.html
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,11 @@
if ("{{artifact_attached}}" == "True") {
load_template_graph();
}
{% if editable %}
else {
load_new_artifact();
}
{% end %}
// If the file-selector select changes, check if we need to show the update button
$("#file-selector").change(function ( event ) {
if ( $("#file-selector").val() === "" ) {
Expand Down
68 changes: 63 additions & 5 deletions qiita_ware/dispatchable.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from .analysis_pipeline import RunAnalysis
from qiita_ware.commands import submit_EBI, submit_VAMPS
from qiita_db.analysis import Analysis
from qiita_db.artifact import Artifact


def submit_to_ebi(preprocessed_data_id, submission_type):
Expand All @@ -31,17 +30,76 @@ def run_analysis(analysis_id, commands, comm_opts=None,
merge_duplicated_sample_ids)


def create_raw_data(filetype, prep_template, filepaths):
def create_raw_data(artifact_type, prep_template, filepaths, name=None):
"""Creates a new raw data

Needs to be dispachable because it moves large files

Parameters
----------
artifact_type: str
The artifact type
prep_template : qiita_db.metadata_template.prep_template.PrepTemplate
The template to attach the artifact
filepaths : list of (str, str)
The list with filepaths and their filepath types
name : str, optional
The name of the new artifact

Returns
-------
dict of {str: str}
A dict of the form {'status': str, 'message': str}
"""
Artifact.create(filepaths, filetype, prep_template=prep_template)
from qiita_db.artifact import Artifact

status = 'success'
msg = ''
try:
Artifact.create(filepaths, artifact_type, name=name,
prep_template=prep_template)
except Exception as e:
# We should hit this exception rarely (that's why it is an
# exception) since at this point we have done multiple checks.
# However, it can occur in weird cases, so better let the GUI know
# that this failed
return {'status': 'danger',
'message': "Error creating artifact: %s" % str(e)}

return {'status': status, 'message': msg}


def copy_raw_data(prep_template, artifact_id):
"""Creates a new raw data by copying from artifact_id"""
Artifact.copy(Artifact(artifact_id), prep_template)
"""Creates a new raw data by copying from artifact_id

Parameters
----------
prep_template : qiita_db.metadata_template.prep_template.PrepTemplate
The template to attach the artifact
artifact_id : int
The id of the artifact to duplicate

Returns
-------
dict of {str: str}
A dict of the form {'status': str, 'message': str}
"""
from qiita_db.artifact import Artifact

status = 'success'
msg = ''

try:
Artifact.copy(Artifact(artifact_id), prep_template)
except Exception as e:
# We should hit this exception rarely (that's why it is an
# exception) since at this point we have done multiple checks.
# However, it can occur in weird cases, so better let the GUI know
# that this failed
return {'status': 'danger',
'message': "Error creating artifact: %s" % str(e)}

return {'status': status, 'message': msg}


def delete_artifact(artifact_id):
Expand Down
19 changes: 18 additions & 1 deletion qiita_ware/test/test_dispatchable.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
from qiita_core.util import qiita_test_checker
from qiita_ware.dispatchable import (
create_sample_template, update_sample_template, delete_sample_template,
update_prep_template, delete_artifact)
update_prep_template, delete_artifact, copy_raw_data, create_raw_data)
from qiita_db.study import Study
from qiita_db.artifact import Artifact
from qiita_db.exceptions import QiitaDBUnknownIDError
from qiita_db.metadata_template.prep_template import PrepTemplate


@qiita_test_checker()
Expand All @@ -36,6 +37,22 @@ def tearDown(self):
if exists(fp):
remove(fp)

def test_create_raw_data(self):
fps = {'raw_barcodes': 'uploaded_file.txt',
'raw_forward_seqs': 'update.txt'}
obs = create_raw_data("FASTQ", PrepTemplate(1), fps, name="New name")
exp = {'status': 'danger',
'message': "Error creating artifact: Prep template 1 already "
"has an artifact associated"}
self.assertEqual(obs, exp)

def test_copy_raw_data(self):
obs = copy_raw_data(PrepTemplate(1), 1)
exp = {'status': 'danger',
'message': "Error creating artifact: Prep template 1 already "
"has an artifact associated"}
self.assertEqual(obs, exp)

def test_delete_artifact(self):
obs = delete_artifact(1)
exp = {'status': 'danger',
Expand Down