Esempio n. 1
0
def copy_raw_data(prep_template, artifact_id):
    """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}
Esempio n. 2
0
def artifact_post_req(user_id, filepaths, artifact_type, name,
                      prep_template_id, artifact_id=None):
    """Creates the initial artifact for the prep template

    Parameters
    ----------
    user_id : str
        User adding the atrifact
    filepaths : dict of str
        Comma-separated list of files to attach to the artifact,
        keyed by file type
    artifact_type : str
        The type of the artifact
    name : str
        Name to give the artifact
    prep_template_id : int or str castable to int
        Prep template to attach the artifact to
    artifact_id : int or str castable to int, optional
        The id of the imported artifact

    Returns
    -------
    dict of objects
        A dictionary containing the new artifact ID
        {'status': status,
         'message': message,
         'artifact': id}
    """
    prep = PrepTemplate(int(prep_template_id))
    study_id = prep.study_id

    # First check if the user has access to the study
    access_error = check_access(study_id, user_id)
    if access_error:
        return access_error

    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)}

    else:
        uploads_path = get_mountpoint('uploads')[0][1]
        path_builder = partial(join, uploads_path, str(study_id))
        cleaned_filepaths = []
        for ftype, file_list in viewitems(filepaths):
            # JavaScript sends us this list as a comma-separated list
            for fp in file_list.split(','):
                # JavaScript will send this value as an empty string if the
                # list of files was empty. In such case, the split will
                # generate a single element containing the empty string. Check
                # for that case here and, if fp is not the empty string,
                # proceed to check if the file exists
                if fp:
                    # Check if filepath being passed exists for study
                    full_fp = path_builder(fp)
                    exists = check_fp(study_id, full_fp)
                    if exists['status'] != 'success':
                        return {'status': 'error',
                                'message': 'File does not exist: %s' % fp}
                    cleaned_filepaths.append((full_fp, ftype))

        # This should never happen, but it doesn't hurt to actually have
        # a explicit check, in case there is something odd with the JS
        if not cleaned_filepaths:
            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)}

    return {'status': 'success',
            'message': '',
            'artifact': artifact.id}
Esempio n. 3
0
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)
Esempio n. 4
0
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)