Beispiel #1
0
    def loadRelations(self, *relations):
        """
        Preload a a list of relationships.  For instance, if you have an instance of an
        object C{User} (named C{user}) that has many C{Address}es and has one C{Avatar},
        you could call C{user.loadRelations('addresses', 'avatar').addCallback('handleUser')}
        instead of having to call C{user.addresses.get()} and C{user.avatar.get()} and assign
        callbacks to the results of those calls.  In the first case, the function C{handleUser}
        would accept one argument, which will be a dictionary whose keys are the property names
        and whose values are the results of the C{get()} calls.  This just makes it easier to
        load multiple properties at once, without having to create a long list of callbacks.

        If the method is called without any arguments, then all relations will loaded.

        @return: A C{Deferred}.
        """
        if len(relations) == 0:
            klass = object.__getattribute__(self, "__class__")
            allrelations = klass.RELATIONSHIP_CACHE.keys()
            if len(allrelations) == 0:
                return defer.succeed({})
            return self.loadRelations(*allrelations)

        ds = {}
        for relation in relations:
            ds[relation] = getattr(self, relation).get()
        return deferredDict(ds)
Beispiel #2
0
def uniquenessOf(obj, names, kwargs):
    """
    A validator to test whether or not some named properties are unique.
    For those named properties that are not unique, an error will
    be recorded in C{obj.errors}.

    @param obj: The object whose properties need to be tested.
    @param names: The names of the properties to test.
    @param kwargs: Keyword arguments.  Right now, all but a
    C{message} value are ignored.
    """
    message = kwargs.get('message', "is not unique.")

    def handle(results):
        for propname, value in results.items():
            if value is not None:
                obj.errors.add(propname, message)
    ds = {}
    for name in names:
        where = ["%s = ?" % name, getattr(obj, name, "")]
        if obj.id is not None:
            where = joinWheres(where, ["id != ?", obj.id])
        d = obj.__class__.find(where=where, limit=1)
        ds[name] = d
    return deferredDict(ds).addCallback(handle)
Beispiel #3
0
    def loadRelations(self, *relations):
        """
        Preload a a list of relationships.  For instance, if you have an instance of an
        object C{User} (named C{user}) that has many C{Address}es and has one C{Avatar},
        you could call C{user.loadRelations('addresses', 'avatar').addCallback('handleUser')}
        instead of having to call C{user.addresses.get()} and C{user.avatar.get()} and assign
        callbacks to the results of those calls.  In the first case, the function C{handleUser}
        would accept one argument, which will be a dictionary whose keys are the property names
        and whose values are the results of the C{get()} calls.  This just makes it easier to
        load multiple properties at once, without having to create a long list of callbacks.

        If the method is called without any arguments, then all relations will loaded.

        @return: A C{Deferred}.
        """
        if len(relations) == 0:
            klass = object.__getattribute__(self, "__class__")
            allrelations = klass.RELATIONSHIP_CACHE.keys()
            if len(allrelations) == 0:
                return defer.succeed({})
            return self.loadRelations(*allrelations)

        ds = {}
        for relation in relations:
            ds[relation] = getattr(self, relation).get()
        return deferredDict(ds)
Beispiel #4
0
def uniquenessOf(obj, names, kwargs):
    """
    A validator to test whether or not some named properties are unique.
    For those named properties that are not unique, an error will
    be recorded in C{obj.errors}.

    @param obj: The object whose properties need to be tested.
    @param names: The names of the properties to test.
    @param kwargs: Keyword arguments.  Right now, all but a
    C{message} value are ignored.
    """
    message = kwargs.get('message', "is not unique.")

    def handle(results):
        for propname, value in results.items():
            if value is not None:
                obj.errors.add(propname, message)

    ds = {}
    for name in names:
        where = ["%s = ?" % name, getattr(obj, name, "")]
        if obj.id is not None:
            where = joinWheres(where, ["id != ?", obj.id])
        d = obj.__class__.find(where=where, limit=1)
        ds[name] = d
    return deferredDict(ds).addCallback(handle)