Example #1
0
    def setUp(self):
        registry_fixtures = fixtures.make_registry_base_objects()
        for data in registry_fixtures:
            obj = RegistryEntry(**data)
            obj.save()

        for name in DPN_NODE_LIST:
            nd = Node(name=name)
            nd.save()

        self.good_body = fixtures.REG_ENTRIES[0]
Example #2
0
def create_registry_entry(correlation_id):
    """
    Creates an entry in the local registry when transfer
    process is completed

    :param correlation_id: String of the correlation_id used in IngestAction

    """
    try:
        ingest = IngestAction.objects.get(
            correlation_id=correlation_id
        )
    except IngestAction.DoesNotExist as err:
        raise err

    replicating_nodes = ingest.sendfileaction_set.filter(
        chosen_to_transfer=True,
        step=COMPLETE,
        state=SUCCESS
    )

    if replicating_nodes.count() > 0:
        local_bag_path = os.path.join(DPN_INGEST_DIR_OUT, "%s.%s" % (
        ingest.object_id, DPN_BAGS_FILE_EXT))
        fixity_value = generate_fixity(local_bag_path)
        now = datetime.now()

        # attributes to update in registry entry
        attributes = dict(
            first_node_name=DPN_NODE_NAME,
            version_number=1,
            fixity_algorithm=DPN_FIXITY_CHOICES[0],
            fixity_value=fixity_value,
            last_fixity_date=now,
            creation_date=now,
            last_modified_date=now,
            bag_size=os.path.getsize(local_bag_path)
        )

        try:
            registry_entry = RegistryEntry.objects.get(
                dpn_object_id=ingest.object_id)

            # in case entry already exists, update its attributes
            for attr, value in attributes.iteritems():
                # this is to prevent modifying creation_date or last_fixity_date
                # just update last_modified_date
                if attr not in ['creation_date', 'last_fixity_date']:
                    setattr(registry_entry, attr, value)

            # set flag created to false
            created = False
        except RegistryEntry.DoesNotExist as err:
            attributes['dpn_object_id'] = ingest.object_id
            registry_entry = RegistryEntry(**attributes)
            created = True

        # validate and save
        registry_entry.full_clean()
        registry_entry.save()

        # now save replication nodes and own node
        for node in [a.node for a in replicating_nodes] + [DPN_NODE_NAME]:
            node, created = Node.objects.get_or_create(name=node)
            registry_entry.replicating_nodes.add(node)

        _status = "created" if created else "updated"
        logger.info(
            "Registry entry successfully %s for transaction with correlation_id: %s" %
            (_status, correlation_id))
        return registry_entry
    else:
        logger.info(
            "Registry entry not created. The bag was not transferred by any node.")
        return None