[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Schema - DC.py:1.1.2.1 DCBrowserView.py:1.1.2.1 Field.py:1.1.2.1 IDC.py:1.1.2.1 IField.py:1.1.2.1 PropertySet.py:1.1.2.1 Widget.py:1.1.2.1 __init__.py:1.1.2.1 index.pt:1.1.2.1 metaConfigure.py:1.1.2.1

Martijn Faassen m.faassen@vet.uu.nl
Wed, 10 Apr 2002 12:10:32 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/App/Schema
In directory cvs.zope.org:/tmp/cvs-serv6926/Zope/App/Schema

Added Files:
      Tag: Zope3-property-branch
	DC.py DCBrowserView.py Field.py IDC.py IField.py 
	PropertySet.py Widget.py __init__.py index.pt metaConfigure.py 
Log Message:
Added experimental schema stuff. Incidentally removed windows newlines from
Formulator


=== Added File Zope3/lib/python/Zope/App/Schema/DC.py ===
from IDC import IDC
from PropertySet import PropertySetFactoryFactory

DC = PropertySetFactoryFactory('MementoBagDCAdapter',
                               IDC)



    


=== Added File Zope3/lib/python/Zope/App/Schema/DCBrowserView.py ===
from Zope.Publisher.Browser.AttributePublisher import AttributePublisher
from Zope.ComponentArchitecture.ContextDependent import ContextDependent
from Zope.PageTemplate.PageTemplateFile import PageTemplateFile
from IDC import IDC

class DCBrowserView(AttributePublisher, ContextDependent):

    index = PageTemplateFile('index.pt')

    __used_for__ = IDC

    def setViewRequest(self, request):
        self._request = request
        
    def titleView(self):
        field = IDC.getDescriptionFor('title')
        view_name = getRequestDefaultViewName(field, self._request)
        widget = getRequestView(field, view_name, self._request)
        widget.setDefault(self.getContext().title)
        return widget()
    
    def descriptionView(self):
        field = IDC.getDescriptionFor('description')
        view_name = getRequestDefaultViewName(field, self._request)
        widget = getRequestView(field, view_name, self._request)
        widget.setDefault(self.getContext().description)
        return widget()

    def submit(self, REQUEST):
        context = self.getContext()

        context.title = REQUEST['title']
        context.description = REQUEST['description']
        
        return self.index(REQUEST)


=== Added File Zope3/lib/python/Zope/App/Schema/Field.py ===
from Interface.Attribute import Attribute

class Field(Attribute):

    title = None
    description = None
    
    def __init__(self, title, description):
        super(Field, self).__init__('%s\n\n%s' % (title, description))
        self.title = title
        self.description = description

    
class StringField(Field):

    default = None
    
    def __init__(self, title, description, default=""):
        super(StringField, self).__init__(title, description)
        self.default = default

class TextField(Field):
    default = None
    
    def __init__(self, title, description, default=""):
        super(TextField, self).__init__(title, description)
        self.default = default

from IField import IField, IStringField, ITextField
from Interface.Implements import implements

implements(Field, IField)
implements(StringField, IStringField)
implements(TextField, ITextField)



=== Added File Zope3/lib/python/Zope/App/Schema/IDC.py ===
from Interface import Interface
from Field import StringField
from Field import TextField

class IDC(Interface):

    title = StringField(
        title="Title",
        description="The title.",
        )

    description = TextField(
        title="Description",
        description="The description.",
        )

    # class __tagged_values__:
    # 
    #         def validate(data):
    #         "validate some data"

    #         order = 'title', 'description'



=== Added File Zope3/lib/python/Zope/App/Schema/IField.py ===
from Interface import Interface
from Field import StringField, TextField

class IField(Interface):
    title = StringField(
        title="Title",
        description="The title.",
        )

    description = TextField(
        title="Description",
        description="The description.",
        )

class IStringField(IField):
    default = StringField(
        title="Default",
        description="Default value.",
        )

class ITextField(IField):
    default = TextField(
        title="Default",
        description="Default value.",
        )
    


=== Added File Zope3/lib/python/Zope/App/Schema/PropertySet.py ===
from IField import IField
from Zope.ComponentArchitecture.ContextDependent import ContextDependent

class MementoAdapterProperty(object):
    def __init__(self, schema, name):
        self._schema = schema
        self._name = name
        field = schema.getDescriptionFor(name)
        self._default = field.default
        
    def __get__(self, instance, klass=None):
        if instance is None:
            return self
        memento = instance.getContext().get(self._schema.__name__, {})
        return memento.get(self._name, self._default)

    def __set__(self, instance, value):
        memento = instance.getContext().get(self._schema.__name__, None)
        if memento is None:
            memento = {}
            instance.getContext()[self._schema.__name__] = memento
        memento[self._name] = value
   
def PropertySetFactoryFactory(klassname, schema):
    """Create PropertySetFactories out of the schema.
    """
    klass = type(klassname, (ContextDependent,), {})
    for name, field in schema.namesAndDescriptions(1):
        if not IField.isImplementedBy(field):
            raise TypeError, "Schema contains non-field attributes."
        # XXX we should check whether we overwrite getContext & co
        setattr(klass, name, MementoAdapterProperty(schema, name))
    klass.__implements__ = schema
    return klass




        


=== Added File Zope3/lib/python/Zope/App/Schema/Widget.py ===
from Zope.ComponentArchitecture.ContextDependent import ContextDependent

class StringWidget(ContextDependent):

    def setViewRequest(self, request):
        self._request = request
        
    def setDefault(self, default):
        self._default = default
        
    def __call__(self):
        field_name = self.getContext().getName()
        return '<input type="text" name="%s" value="%s" />' % (
            field_name, self._default)
    
class TextWidget(object):

    def setViewRequest(self, request):
        self._request = request
        
    def setDefault(self, default):
        self._default = default

    def __call__(self):
        field_name = self.getContext().getName()
        return '<textarea name="%s:text">%s</textarea>' % (
            field_name, self._default)


=== Added File Zope3/lib/python/Zope/App/Schema/__init__.py ===
# 


=== Added File Zope3/lib/python/Zope/App/Schema/index.pt ===
<html>
<body>
<form action="submit" method="POST">
<p tal:replace="structure view/titleView">titleView</p><br />
<p tal:replace="structure view/descriptionView">descriptionView</p><br />
<input type="submit" value=" OK " />
</form>
</body>
</html>

=== Added File Zope3/lib/python/Zope/App/Schema/metaConfigure.py ===
from Zope.ComponentArchitecture.IToIRegistry import TypeRegistry

_reg = {}

def propertySetDirective(_context, provides, name, for_):
    provides = _context.resolve(provides)
    for_ = _context.resolve(for_)
    return [Action(discriminator=('propertySet', name, for_),
                   callable=propertySet,
                   args=(provides, name, for_))]

def propertySet(provides, name, for_):
    treg = _reg.get(name)
    if treg is None:
        treg = TypeRegistry()
        _reg[name] = treg
    treg.register(for_, provides)