def modifiedArchetype(obj, event):
    """ an archetype based object was modified """
    pu = getToolByName(obj, 'portal_url', None)
    if pu is None:
        # `getObjectFromLinks` is not possible without access
        # to `portal_url`
        return
    rc = getToolByName(obj, 'reference_catalog', None)
    if rc is None:
        # `updateReferences` is not possible without access
        # to `reference_catalog`
        return
    refs = set()
    for field in obj.Schema().fields():
        if isinstance(field, TextField):
            accessor = field.getAccessor(obj)
            if accessor is not None:
                value = accessor()
            else:
                # Fields that have been added via schema extension do
                # not have an accessor method.
                value = field.get(obj)
            links = extractLinks(value)
            refs |= getObjectsFromLinks(obj, links)
    updateReferences(obj, referencedRelationship, refs)
def modifiedArchetype(obj, event):
    """ an archetype based object was modified """
    refs = set()
    for field in obj.Schema().fields():
        if isinstance(field, TextField):
            accessor = field.getAccessor(obj)
            links = extractLinks(accessor())
            refs |= getObjectsFromLinks(obj, links)
    updateReferences(obj, referencedRelationship, refs)
Exemple #3
0
def modifiedArchetype(obj, event):
    """ an archetype based object was modified """
    try:  # TODO: is this a bug or a needed workaround?
        existing = set(obj.getReferences(relationship=referencedRelationship))
    except AttributeError:
        return
    refs = set()
    for field in obj.Schema().fields():
        if isinstance(field, TextField):
            accessor = field.getAccessor(obj)
            links = extractLinks(accessor())
            refs = refs.union(getObjectsFromLinks(obj, links))
    for ref in refs.difference(existing):  # add new references and...
        try:
            obj.addReference(ref, relationship=referencedRelationship)
        except ReferenceException:
            pass
    for ref in existing.difference(refs):  # removed leftovers
        try:
            obj.deleteReference(ref, relationship=referencedRelationship)
        except ReferenceException:
            try:
                # try to get rid of the dangling reference, but let's not
                # have this attempt to clean up break things otherwise...
                # iow, the `try..except` is there, because internal methods
                # of the reference catalog are being used directly here.  any
                # changes regarding these shouldn't break things over here,
                # though...
                refcat = getToolByName(obj, "reference_catalog")
                uid, dummy = refcat._uidFor(obj)
                brains = refcat._queryFor(uid, None, relationship=referencedRelationship)
                objs = refcat._resolveBrains(brains)
                for obj in objs:
                    refcat._deleteReference(obj)
            except ConflictError:
                raise
            except:
                getLogger(__name__).warning('dangling "linkintegrity" ' "reference to %r could not be removed.", obj)