def handle(self, replacement_rd3):
        # assign doc_id if need.
        ResourceDataModelValidator.assign_id(replacement_rd3)

        # validate the rd3 to be published.
        ResourceDataModelValidator.validate_model(replacement_rd3)

        # check to see if it's a replacement
        if replacement_rd3 is not None and _REPLACES in replacement_rd3:
            replacement_rd3_info = get_verification_info(replacement_rd3)
            if replacement_rd3_info is not None:
                graveyard = []
                existing_gravestones = []
                for orig_doc_id in replacement_rd3[_REPLACES]:
                    try:
                        orig_doc = self.db[orig_doc_id]
                        orig_doc_info = get_verification_info(orig_doc)
                    except couchdb.http.ResourceNotFound as rne:
                        orig_doc = None
                        orig_doc_info = None

                    if orig_doc is not None and orig_doc["doc_type"] == "tombstone":
                        permitted = True
                        existing_gravestones.append(orig_doc)
                    else:
                        # verify that tombstones
                        permitted = self._check_if_permitted(replacement_rd3, replacement_rd3_info, orig_doc, orig_doc_info)
                    
                    log.debug("handle: permitted? {0}".format(permitted))
                    if permitted:
                        tombstone = self._make_tombstone(replacement_rd3, replacement_rd3_info, orig_doc_id, orig_doc)               
                        graveyard.append(tombstone)
                    else:
                        log.debug("Replacement resource not permitted to be saved.")

                # this should save unless a permit plugin has thrown an exception to disallow saving
                result = ResourceDataModelValidator.save(replacement_rd3, skip_validation=True)

                if self._has_graveyard_permit(replacement_rd3, replacement_rd3_info, graveyard, existing_gravestones):
                    for tombstone in graveyard:
                        try:
                            self.db[tombstone["_id"]] = tombstone
                        except:
                            # if this is already a tombstone, then it's okay - first wins
                            TombstoneValidator.validate_model(self.db[tombstone["_id"]])
                # else:
                #     # import pdb; pdb.set_trace()
                #     raise SpecValidationException("Node policy does not permit this resource published.")

                return result

        # we get here because it's not a replacement.
        return ResourceDataModelValidator.save(replacement_rd3, skip_validation=True)
Example #2
0
def checkTombstone(doc):
    TombstoneValidator.validate_model(doc)
    def handle(self, replacement_rd3):
        # assign doc_id if need.
        ResourceDataModelValidator.assign_id(replacement_rd3)

        # validate the rd3 to be published.
        ResourceDataModelValidator.validate_model(replacement_rd3)

        # check to see if it's a replacement
        if replacement_rd3 is not None and _REPLACES in replacement_rd3:
            replacement_rd3_info = get_verification_info(replacement_rd3)
            if replacement_rd3_info is not None:
                graveyard = []
                existing_gravestones = []
                for orig_doc_id in replacement_rd3[_REPLACES]:
                    try:
                        orig_doc = self.db[orig_doc_id]
                        orig_doc_info = get_verification_info(dict(orig_doc))
                    except couchdb.http.ResourceNotFound as rne:
                        orig_doc = None
                        orig_doc_info = None

                    if orig_doc is not None and orig_doc[
                            "doc_type"] == "tombstone":
                        permitted = True
                        existing_gravestones.append(orig_doc)
                    else:
                        # verify that tombstones
                        permitted = self._check_if_permitted(
                            replacement_rd3, replacement_rd3_info, orig_doc,
                            orig_doc_info)

                    log.debug("handle: permitted? {0}".format(permitted))
                    if permitted:
                        tombstone = self._make_tombstone(
                            replacement_rd3, replacement_rd3_info, orig_doc_id,
                            orig_doc)
                        graveyard.append(tombstone)
                    else:
                        log.debug(
                            "Replacement resource not permitted to be saved.")

                # this should save unless a permit plugin has thrown an exception to disallow saving
                result = ResourceDataModelValidator.save(replacement_rd3,
                                                         skip_validation=True)

                if self._has_graveyard_permit(replacement_rd3,
                                              replacement_rd3_info, graveyard,
                                              existing_gravestones):
                    for tombstone in graveyard:
                        try:
                            self.db[tombstone["_id"]] = tombstone
                        except:
                            # if this is already a tombstone, then it's okay - first wins
                            TombstoneValidator.validate_model(
                                self.db[tombstone["_id"]])
                # else:
                #     # import pdb; pdb.set_trace()
                #     raise SpecValidationException("Node policy does not permit this resource published.")

                return result

        # we get here because it's not a replacement.
        return ResourceDataModelValidator.save(replacement_rd3,
                                               skip_validation=True)
def checkTombstone(doc):
    TombstoneValidator.validate_model(doc)