[Zope3-checkins] CVS: Zope3/src/zope/app/utilities - instance.py:1.1.2.1

Stephan Richter srichter at cosmos.phy.tufts.edu
Wed Aug 13 14:27:50 EDT 2003


Update of /cvs-repository/Zope3/src/zope/app/utilities
In directory cvs.zope.org:/tmp/cvs-serv3138/utilities

Added Files:
      Tag: dreamcatcher-ttwschema-branch
	instance.py 
Log Message:
Added theoretical excercise of a ContentObjectInstance. This was mainly
taken from the workflow, but it seems to work in general, so that we should
be able to merge the workflow version and this one eventually.


=== Added File Zope3/src/zope/app/utilities/instance.py ===
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Content Object Instance

$Id: instance.py,v 1.1.2.1 2003/08/13 17:27:46 srichter Exp $
"""
__metaclass__ = type

from persistence import Persistent
from persistence.dict import PersistentDict
from zope.context import ContextMethod
from zope.interface import directlyProvides, implements
from zope.schema import getFields
from zope.security.checker import CheckerPublic, Checker, defineChecker

class ContentObjectInstance(Persistent):

    def __init__(self, schema=None, schemaPermissions=None):
        super(ContentObjectInstance, self).__init__()
        self.__schema = schema
        # Add the new attributes, if there was a schema passed in
        if schema is not None:
            for name, field in getFields(schema).items():
                setattr(self, name, field.default)
            directlyProvides(self, schema)

            # Build up a Checker rules and store it for later
            self.__checker_getattr = PersistentDict()
            self.__checker_setattr = PersistentDict()
            for name in getFields(schema):
                get_perm, set_perm = schemaPermissions.get(name, (None, None))
                self.__checker_getattr[name] = get_perm or CheckerPublic
                self.__checker_setattr[name] = set_perm or CheckerPublic

            # Always permit our class's public methods
            self.__checker_getattr['getSchema'] = CheckerPublic


    def __setattr__(self, key, value):
        if key in ('_ContentObjectInstance__schema',
                   '_ContentObjectInstance__checker_getattr',
                   '_ContentObjectInstance__checker_setattr',
                   'getChecker', 'getSchema') or \
               key.startswith('_p_'):
            return super(ContentObjectInstance, self).__setattr__(key, value)

        is_schema_field = self.__schema is not None and \
                          key in getFields(self.__schema).keys()

        if is_schema_field:
            super(ContentObjectInstance, self).__setattr__(key, value)
        else:
            raise AttributeError, 'Attribute not available'

    __setattr__ = ContextMethod(__setattr__)


    def getSchema(self):
        return self.__schema


def ContentObjectInstanceChecker(instance):
    """A function that can be registered as a Checker in defineChecker()"""
    return Checker(instance.__checker_getattr.get,
                   instance.__checker_setattr.get)

defineChecker(ContentObjectInstance, ContentObjectInstanceChecker)


class EditContentObject:
    pass
    




More information about the Zope3-Checkins mailing list