예제 #1
0
def create_dataset(investigation_uuid, username, identifier=None, title=None,
                   dataset_name=None, slug=None, public=False):
    """creates (or updates) a dataset with the given investigation and user and
    returns the dataset UUID or None if something went wrong
    Parameters:
    investigation_uuid: UUID of the investigation that's being assigned to the
    dataset
    username: username of the user this dataset will belong to
    identifier: If not None, this will be used as the identifier of the data
    set.
    title: If not None, this will be
    public: boolean value that determines if the dataset is public or not
    """
    # get User for assigning DataSets
    try:
        user = User.objects.get(username__exact=username)
    except:
        logger.info(
            "create_dataset: User %s doesn't exist, so creating User %s with "
            "password 'test'", username, username)
        # user doesn't exist
        user = User.objects.create_user(username, "", "test")
    if investigation_uuid is not None:
        # TODO: make sure this is used everywhere
        annotate_nodes(investigation_uuid)
        dataset = None
        investigation = Investigation.objects.get(uuid=investigation_uuid)
        if identifier is None:
            identifier = investigation.get_identifier()
        if title is None:
            title = investigation.get_title()
        if dataset_name is None:
            dataset_name = "%s: %s" % (identifier, title)

        logger.info(
            "create_dataset: title = %s, identifer = %s, dataset_name = '%s'",
            title, identifier, dataset_name)

        datasets = DataSet.objects.filter(name=dataset_name)
        # check if the investigation already exists
        if len(datasets):  # if not 0, update dataset with new investigation
            """go through datasets until you find one with the correct owner"""
            for ds in datasets:
                own = ds.get_owner()
                if own == user:
                    ds.update_investigation(investigation,
                                            "updated %s" % date.today())
                    logger.info("create_dataset: Updated data set %s", ds.name)
                    dataset = ds
                    break
        # create a new dataset if doesn't exist already for this user
        if not dataset:
            dataset = DataSet.objects.create(name=dataset_name)
            dataset.set_investigation(investigation)
            dataset.set_owner(user)
            dataset.accession = identifier
            dataset.title = title
            logger.info("create_dataset: Created data set '%s'", dataset_name)
        if public:
            public_group = ExtendedGroup.objects.public_group()
            dataset.share(public_group)
        # set dataset slug
        dataset.slug = slug
        # calculate total number of files and total number of bytes
        dataset.file_size = dataset.get_file_size()
        dataset.file_count = dataset.get_file_count()
        dataset.save()
        # Finally index data set
        update_data_set_index(dataset)
        add_data_set_to_neo4j(dataset.uuid, user.id)
        return dataset.uuid
    return None
def create_dataset(investigation_uuid,
                   username,
                   identifier=None,
                   title=None,
                   dataset_name=None,
                   slug=None,
                   public=False):
    """creates (or updates) a dataset with the given investigation and user and
    returns the dataset UUID or None if something went wrong
    Parameters:
    investigation_uuid: UUID of the investigation that's being assigned to the
    dataset
    username: username of the user this dataset will belong to
    identifier: If not None, this will be used as the identifier of the data
    set.
    title: If not None, this will be
    public: boolean value that determines if the dataset is public or not
    """
    # get User for assigning DataSets
    try:
        user = User.objects.get(username__exact=username)
    except:
        logger.info(
            "create_dataset: User %s doesn't exist, so creating User %s with "
            "password 'test'", username, username)
        # user doesn't exist
        user = User.objects.create_user(username, "", "test")
    if investigation_uuid is None:
        return None  # TODO: make sure this is never happens
    annotate_nodes(investigation_uuid)
    dataset = None
    investigation = Investigation.objects.get(uuid=investigation_uuid)
    if identifier is None:
        identifier = investigation.get_identifier()
    if title is None:
        title = investigation.get_title()
    if dataset_name is None:
        dataset_name = "%s: %s" % (identifier, title)

    logger.info(
        "create_dataset: title = %s, identifer = %s, dataset_name = '%s'",
        title, identifier, dataset_name)

    datasets = DataSet.objects.filter(name=dataset_name)
    # check if the investigation already exists
    if len(datasets):  # if not 0, update dataset with new investigation
        """go through datasets until you find one with the correct owner"""
        for ds in datasets:
            own = ds.get_owner()
            if own == user:
                ds.update_investigation(investigation,
                                        "updated %s" % date.today())
                logger.info("create_dataset: Updated data set %s", ds.name)
                dataset = ds
                break
    # create a new dataset if doesn't exist already for this user
    if not dataset:
        dataset = DataSet.objects.create(name=dataset_name)
        dataset.set_investigation(investigation)
        dataset.set_owner(user)
        dataset.accession = identifier
        dataset.title = title
        logger.info("create_dataset: Created data set '%s'", dataset_name)
    if public:
        public_group = ExtendedGroup.objects.public_group()
        dataset.share(public_group)
    # set dataset slug
    dataset.slug = slug
    # calculate total number of files and total number of bytes
    dataset.file_size = dataset.get_file_size()
    dataset.file_count = dataset.get_file_count()
    dataset.save()
    # Finally index data set
    update_data_set_index(dataset)
    add_data_set_to_neo4j(dataset, user.id)
    async_update_annotation_sets_neo4j()
    return dataset.uuid