Esempio n. 1
0
        def testInterface(self):
            self.assert_(IPersistent.implementedBy(Persistent),
                         "%s does not implement IPersistent" % Persistent)
            p = Persistent()
            self.assert_(IPersistent.providedBy(p),
                         "%s does not implement IPersistent" % p)

            self.assert_(IPersistent.implementedBy(P),
                         "%s does not implement IPersistent" % P)
            p = self.klass()
            self.assert_(IPersistent.providedBy(p),
                         "%s does not implement IPersistent" % p)
Esempio n. 2
0
        def testInterface(self):
            self.assert_(IPersistent.implementedBy(Persistent),
                         "%s does not implement IPersistent" % Persistent)
            p = Persistent()
            self.assert_(IPersistent.providedBy(p),
                         "%s does not implement IPersistent" % p)

            self.assert_(IPersistent.implementedBy(P),
                         "%s does not implement IPersistent" % P)
            p = self.klass()
            self.assert_(IPersistent.providedBy(p),
                         "%s does not implement IPersistent" % p)
def _children_all(tree, debug=False):
    """returns a list of every child"""

    objects = [o for o in tree.objectValues() if IPersistent.providedBy(o)]

    child_list = []
    to_crawl = deque(objects)

    i = 0
    while to_crawl:
        current = to_crawl.popleft()
        child_list.append(current)
        #print "Looking at ", current.absolute_url()
        print ".",

        if IFolder.providedBy(current):
            node_children = [o for o in current.objectValues() if \
                    IAttributeAnnotatable.providedBy(o) and is_ok(o.getId())]
            to_crawl.extend(node_children)

        i += 1
        if debug and (i > 1000):
            break

    return child_list
Esempio n. 4
0
def exportSolrSettings(context):
    """ export settings for solr integration as an XML file """
    site = context.getSite()
    utility = queryUtility(ISolrConnectionConfig, context=site)
    if utility is None:
        logger = context.getLogger('collective.solr')
        logger.info('Nothing to export.')
        return
    if IPersistent.providedBy(utility):
        exportObjects(utility, '', context)
Esempio n. 5
0
def exportSolrSettings(context):
    """ export settings for solr integration as an XML file """
    site = context.getSite()
    utility = queryUtility(ISolrConnectionConfig, context=site)
    if utility is None:
        logger = context.getLogger('collective.solr')
        logger.info('Nothing to export.')
        return
    if IPersistent.providedBy(utility):
        exportObjects(utility, '', context)
def importWebSocketClientSettings(context):
    """ import settings for websocket client integration from an XML file """
    site = context.getSite()
    utility = queryUtility(IWebSocketConnectionConfig, context=site)
    if utility is None:
        logger = context.getLogger('collective.websocketclient')
        logger.info('Nothing to import.')
        return
    if IPersistent.providedBy(utility):
        importObjects(utility, '', context)
Esempio n. 7
0
def getKeyReference(object):
    if not IPersistent.providedBy(object):
        return

    try:
        return KeyReferenceToPersistent(removeSecurityProxy(object))
    except NotYet:
        while not IDraftContent.providedBy(object):
            object = getattr(object, '__parent__', None)
            if object is None:
                raise
Esempio n. 8
0
 def storeExceptionText(self, text):
     """
     Store an exception text, usually as created by
     :func:`nti.webhooks._util.print_exception_to_text`
     """
     if self.exception_history == ():
         if IPersistent.providedBy(self.__parent__):
             self.__parent__._p_changed = True  # pylint:disable=protected-access
             self.exception_history = PersistentList()
         else:
             self.exception_history = []
     assert isinstance(text, text_type)
     self.exception_history.append(text)
Esempio n. 9
0
 def cached_property(self):
     utility = getUtility(ICacheManager)
     if region is not None:
         cache = utility.get_cache_from_region(cache_ns, region)
     else:
         cache = utility.get_cache(cache_ns, **cache_options)
     cache_key = 'property'
     if IPersistent.providedBy(self):
         cache_key = str(self._p_oid)
     def solves():
         return func(self)
     return cache.get_value(_verify_key(cache_key), createfunc=solves,
         expiretime=expire)
Esempio n. 10
0
    def __init__(self, _context, class_):
        if not ILocation.implementedBy(class_) and \
               not hasattr(class_, '__parent__'):
            raise ConfigurationError('Class `%s` does not implement '
                                     '`ILocation`.' % class_.__name__)

        if not IPersistent.implementedBy(class_):
            raise ConfigurationError('Class `%s` does not implement '
                                     '`IPersistent`.' % class_.__name__)

        classImplements(class_, IAttributeAnnotatable)
        classImplements(class_, ILocalUtility)

        super(LocalUtilityDirective, self).__init__(_context, class_)
Esempio n. 11
0
 def __init__(self, subscriptions_and_attempts):
     # Sort them by URL so that requests to the same host go together;
     # this may help with HTTP keepalive/pipeline?
     self._sub_and_attempts = sorted(
         subscriptions_and_attempts,
         key=lambda sub_attempt: sub_attempt[0].to)
     self._results = []
     self._had_persistent = False
     for sub, attempt in self._sub_and_attempts:
         if IPersistent.providedBy(attempt):
             self._had_persistent = True
             getter = _PersistentAttemptGetter(sub, attempt)
         else:
             getter = _TrivialAttemptGetter(sub, attempt)
         result = _AttemptResult(getter)
         self._results.append(result)
Esempio n. 12
0
    def _getOid(self, object):
        """ returns oid for persistent object """
        if not IPersistent.providedBy(object):
            return

        try:
            return hash(KeyReferenceToPersistent(object))
            #oid = hash(KeyReferenceToPersistent(object))

            ## negative hash means file was not uploaded
            #if oid <= 0:
            #    return

            #return oid
        except NotYet:
            return
        def changedField(field, value, context=None):
            """Figure if a field's value changed

            Comparing the value of the context attribute and the given value"""
            if context is None:
                context = field.context
            if context is None:
                # IObjectWidget madness
                return True
            if zope.schema.interfaces.IObject.providedBy(field):
                return True

            if not IPersistent.providedBy(context):
                # Field is not persisted, delegate to original implementation
                # Could be a z3c.formwidget QueryContex or an AQ wrapped dict
                # instance from Plone's TTW registry editor.
                assert any((
                    isinstance(context, QueryContext),
                    isinstance(context, RecordsProxy),
                    isinstance(context, ImplicitAcquisitionWrapper)
                    and isinstance(aq_base(context), dict),
                ))
                return original_changedField(field, value, context)

            dm = zope.component.getMultiAdapter((context, field),
                                                interfaces.IDataManager)

            if not dm.canAccess():
                # Can't get the original value, assume it changed
                return True

            # Determine the original value
            # Use a helper method that actually returns the persisted value,
            # *without* triggering any fallbacks to default values or
            # missing values.
            try:
                stored_value = get_persisted_value_for_field(context, field)
            except AttributeError:
                return True

            if stored_value != value:
                return True

            return False
Esempio n. 14
0
        def changedField(field, value, context=None):
            """Figure if a field's value changed

            Comparing the value of the context attribute and the given value"""
            if context is None:
                context = field.context
            if context is None:
                # IObjectWidget madness
                return True
            if zope.schema.interfaces.IObject.providedBy(field):
                return True

            if not IPersistent.providedBy(context):
                # Field is not persisted, delegate to original implementation
                # Could be a z3c.formwidget QueryContex or an AQ wrapped dict
                # instance from Plone's TTW registry editor.
                assert any((
                    isinstance(context, QueryContext),
                    isinstance(context, RecordsProxy),
                    isinstance(context, ImplicitAcquisitionWrapper) and
                    isinstance(aq_base(context), dict),
                ))
                return original_changedField(field, value, context)

            dm = zope.component.getMultiAdapter(
                (context, field), interfaces.IDataManager)

            if not dm.canAccess():
                # Can't get the original value, assume it changed
                return True

            # Determine the original value
            # Use a helper method that actually returns the persisted value,
            # *without* triggering any fallbacks to default values or
            # missing values.
            try:
                stored_value = get_persisted_value_for_field(context, field)
            except AttributeError:
                return True

            if stored_value != value:
                return True

            return False
Esempio n. 15
0
 def cached_method(self, *args, **kwargs):
     utility = getUtility(ICacheManager)
     if region is not None:
         cache = utility.get_cache_from_region(cache_ns, region)
     else:
         cache = utility.get_cache(cache_ns, **cache_options)
     cache_key = None
     if key is not None:
         cache_key = key(self, *args, **kwargs)
     if cache_key is None:
         cache_key = tuple()
         if IPersistent.providedBy(self):
             cache_key += tuple([str(self._p_oid)])
         cache_key += tuple(map(str, args))
         cache_key += tuple(map(
                 lambda kwarg: "=".join(map(str, kwarg)),
                 sorted(kwargs.items(), key=operator.itemgetter(0))))
     def solves():
         return func(self, *args, **kwargs)
     return cache.get_value(_verify_key(cache_key), createfunc=solves,
         expiretime=expire)
Esempio n. 16
0
    def add_subscriptions(self, data, event, subscriptions):
        """
        Add more subscriptions to the set managed by this object.

        Once two-phase-commit begins, this method is forbidden.

        TODO We may need a mechanism to say, if we got multiple event types for a single
        object, when and how to coalesce them. For example, given ObjectCreated and ObjectAdded
        events, we probably only want to broadcast one of them. This may be dialect specific?
        Or part of the subscription registration? A priority or 'supercedes' or something?
        For now, we just wind up discarding the event type and assume it's not important to the
        delivery.
        """
        # We don't enforce that you can't call this after TPC has begun.
        self._subscriptions[(data, event)].update(subscriptions)
        for subscription in subscriptions:
            # pylint:disable=protected-access
            if IPersistent.providedBy(
                    subscription
            ) and subscription._p_jar and subscription._p_oid:
                # See comment below for why we must do this.
                subscription._p_jar.register(subscription)
Esempio n. 17
0
def _local_settings(context):
    """Get local MLS settings."""
    settings = None
    if not context or not IPersistent.providedBy(context):
        request = getRequest()
        parents = request.get('PARENTS')
        if len(parents) > 0:
            context = parents[0]
        else:
            context = api.portal.get()
    obj = context
    while (
            not IPloneSiteRoot.providedBy(obj) and
            not ILocalMLSSettings.providedBy(obj)):
        parent = aq_parent(aq_inner(obj))
        if parent is None:
            return
        obj = parent
    if ILocalMLSSettings.providedBy(obj):
        annotations = IAnnotations(obj)
        settings = annotations.get(
            CONFIGURATION_KEY, annotations.setdefault(CONFIGURATION_KEY, {}))
    return settings
Esempio n. 18
0
def _local_settings(context):
    """Get local MLS settings."""
    settings = None
    if not context or not IPersistent.providedBy(context):
        request = getRequest()
        parents = request.get('PARENTS')
        if len(parents) > 0:
            context = parents[0]
        else:
            context = api.portal.get()
    obj = context
    while (
            not IPloneSiteRoot.providedBy(obj) and
            not ILocalMLSSettings.providedBy(obj)):
        parent = aq_parent(aq_inner(obj))
        if parent is None:
            return
        obj = parent
    if ILocalMLSSettings.providedBy(obj):
        annotations = IAnnotations(obj)
        settings = annotations.get(
            CONFIGURATION_KEY, annotations.setdefault(CONFIGURATION_KEY, {}))
    return settings
Esempio n. 19
0
def get_persisted_value_for_field(context, field):
    """Return the *real* stored value for a field, avoiding any fallbacks.

    In particular, this circumvents the __getattr__ fallbacks in
    DexterityContent and AnnotationsFactoryImpl that return the field's
    default / missing_value.

    Raises an AttributeError if there is no stored value for the field.

    What we need to do here is basically to decide whether the storage
    implementation used for the given field on this context has any
    fallbacks (to default or missing value) in place. If it does, we need to
    bypass them and only return the actually persisted value, or raise an
    AttributeError.

    We also deal with fields that are implemented as properties (which are
    to be found in the class dict instead of the instance dict).
    """
    name = field.getName()
    if not IPersistent.providedBy(context):
        raise Exception(
            "Attempt to get persisted field value for a non-persistent object "
            "for field %r on obj %r" % (name, context))

    # AQ unwrap object to avoid finding an attribute via acquisition
    context = aq_base(context)
    storage_impl = field.interface(context)

    def classname(obj):
        """Stringy alternative to instance checks to avoid circular imports.
        """
        return getattr(getattr(obj, '__class__', object), '__name__', '')

    if isinstance(storage_impl, AnnotationsFactoryImpl):
        # AnnotationStorage
        if name not in storage_impl.__dict__['schema']:
            raise AttributeError(name)

        annotations = storage_impl.__dict__['annotations']
        key_name = storage_impl.__dict__['prefix'] + name
        try:
            value = annotations[key_name]
        except KeyError:
            # Don't do the fallback to field.missing_value that
            # AnnotationsFactoryImpl.__getattr__ does
            raise AttributeError(name)
        return value
    elif storage_impl is context:
        # Assume attribute storage
        try:
            # Two possible cases here: Field in base schema, or field in
            # behavior with attribute storage. Either way, we look up the
            # attribute in the object's __dict__ in order to circumvent the
            # fallback in DexterityContent.__getattr__
            value = storage_impl.__dict__[name]
        except KeyError:
            # Check whether the field is a property
            descriptor = storage_impl.__class__.__dict__.get(name)
            if isinstance(descriptor, property):
                # Invoke the property's getter
                return descriptor.fget(storage_impl)
            raise AttributeError(name)
        return value
    elif classname(storage_impl) in ('TranslatedTitle', 'OGMailBase'):
        # These factories wrap a context that inherits from DexterityContent,
        # accessed via storage_impl.context.
        # So we need to apply the same strategy as for direct attribute
        # storage to accessing attributes on that context in order to bypass
        # the __getattr__ fallback.
        try:
            value = storage_impl.context.__dict__[name]
        except KeyError:
            # Check whether the field is a property
            descriptor = storage_impl.__class__.__dict__.get(name)
            if isinstance(descriptor, property):
                # Invoke the property's getter
                return descriptor.fget(storage_impl)
            raise AttributeError(name)
        return value
    elif classname(storage_impl) == 'Versionable':
        # Versionable.changeNote is a property that isn't persisted, but
        # written to request annotations instead
        raise AttributeError(field.getName())
    elif isinstance(storage_impl, metadata.MetadataBase):
        # MetadataBase has its own default value fallback - bypass it
        try:
            value = context.__dict__[name]
        except KeyError:
            raise AttributeError
        return value
    else:
        # Unknown storage - bail
        raise Exception(
            "Unknown storage %r for field %r" % (storage_impl, name))
Esempio n. 20
0
 def test_persistence(self):
     field = IContent2['rich']
     value = field.from_string("A plain text string")
     self.assertFalse(IPersistent.providedBy(value))
     self.assertTrue(IPersistent.providedBy(value._raw_holder))
Esempio n. 21
0
def get_persisted_value_for_field(context, field):
    """Return the *real* stored value for a field, avoiding any fallbacks.

    In particular, this circumvents the __getattr__ fallbacks in
    DexterityContent and AnnotationsFactoryImpl that return the field's
    default / missing_value.

    Raises an AttributeError if there is no stored value for the field.

    What we need to do here is basically to decide whether the storage
    implementation used for the given field on this context has any
    fallbacks (to default or missing value) in place. If it does, we need to
    bypass them and only return the actually persisted value, or raise an
    AttributeError.
    """
    name = field.getName()
    if not IPersistent.providedBy(context):
        raise Exception(
            "Attempt to get persisted field value for a non-persistent object "
            "for field %r on obj %r" % (name, context))

    # AQ unwrap object to avoid finding an attribute via acquisition
    context = aq_base(context)
    storage_impl = field.interface(context)

    def classname(obj):
        """Stringy alternative to instance checks to avoid circular imports.
        """
        return getattr(getattr(obj, '__class__', object), '__name__', '')

    if isinstance(storage_impl, AnnotationsFactoryImpl):
        # AnnotationStorage
        if name not in storage_impl.__dict__['schema']:
            raise AttributeError(name)

        annotations = storage_impl.__dict__['annotations']
        key_name = storage_impl.__dict__['prefix'] + name
        try:
            value = annotations[key_name]
        except KeyError:
            # Don't do the fallback to field.missing_value that
            # AnnotationsFactoryImpl.__getattr__ does
            raise AttributeError(name)
        return value
    elif storage_impl is context:
        # Assume attribute storage
        try:
            # Two possible cases here: Field in base schema, or field in
            # behavior with attribute storage. Either way, we look up the
            # attribute in the object's __dict__ in order to circumvent the
            # fallback in DexterityContent.__getattr__
            value = storage_impl.__dict__[name]
        except KeyError:
            raise AttributeError(name)
        return value
    elif classname(storage_impl) in ('TranslatedTitle', 'OGMailBase'):
        # These factories wrap a context that inherits from DexterityContent,
        # accessed via storage_impl.context.
        # So we need to apply the same strategy as for direct attribute
        # storage to accessing attributes on that context in order to bypass
        # the __getattr__ fallback.
        try:
            value = storage_impl.context.__dict__[name]
        except KeyError:
            raise AttributeError(name)
        return value
    elif classname(storage_impl) == 'Versionable':
        # Versionable.changeNote is a property that isn't persisted, but
        # written to request annotations instead
        raise AttributeError(field.getName())
    elif isinstance(storage_impl, metadata.MetadataBase):
        # MetadataBase has its own default value fallback - bypass it
        try:
            value = context.__dict__[name]
        except KeyError:
            raise AttributeError
        return value
    else:
        # Unknown storage - bail
        raise Exception("Unknown storage %r for field %r" %
                        (storage_impl, name))
Esempio n. 22
0
    def __setattr(self, object, name, value):
        removeSecurityProxy(object).__dict__[name] = value

        if IPersistent.providedBy(object):
            object._p_changed = True
Esempio n. 23
0
 def test_persistence(self):
     field = IContent2['rich']
     value = field.from_string("A plain text string")
     self.assertFalse(IPersistent.providedBy(value))
     self.assertTrue(IPersistent.providedBy(value._raw_holder))
Esempio n. 24
0
def getWeakRef(ob):
    """Get either a persistent or non-presistent weakref"""
    if IPersistent.providedBy(ob):
        return WeakRef(ob)
    else:
        return wref(ob)