[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Formulator/Fields/Generic - DateTimeField.py:1.2 EmailField.py:1.2 FileField.py:1.2 FloatField.py:1.2 IntegerField.py:1.2 LinkField.py:1.2 ListField.py:1.2 MethodField.py:1.2 PasswordField.py:1.2 PatternField.py:1.2 StringField.py:1.2 TALESField.py:1.2 __init__.py:1.2

Jim Fulton jim@zope.com
Mon, 10 Jun 2002 19:28:50 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/App/Formulator/Fields/Generic
In directory cvs.zope.org:/tmp/cvs-serv17445/lib/python/Zope/App/Formulator/Fields/Generic

Added Files:
	DateTimeField.py EmailField.py FileField.py FloatField.py 
	IntegerField.py LinkField.py ListField.py MethodField.py 
	PasswordField.py PatternField.py StringField.py TALESField.py 
	__init__.py 
Log Message:
Merged Zope-3x-branch into newly forked Zope3 CVS Tree.


=== Zope3/lib/python/Zope/App/Formulator/Fields/Generic/DateTimeField.py 1.1 => 1.2 ===
+#
+# Copyright (c) 2001, 2002 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.
+# 
+##############################################################################
+"""
+
+$Id$
+"""
+
+from Zope.App.Formulator.Field import Field
+from Zope.App.Formulator.Validators import DateTimeValidator 
+import time
+
+
+class DateTimeField(Field):
+
+    __implements__ = Field.__implements__
+
+    id = None
+    default = ''
+    title = 'DateTime Field'
+    description = 'DateTime Field'
+    validator = DateTimeValidator.DateTimeValidator()
+
+            
+    def on_value_input_style_changed(self, value):
+        if value == 'text':
+            self.sub_form = create_datetime_text_sub_form()
+        elif value == 'list':
+            self.sub_form = create_datetime_list_sub_form()
+            year_field = self.sub_form.get_field('year')
+            year_field.overrides['items'] = BoundMethod(self,
+                                                        'override_year_items')
+        else:
+            assert 0, "Unknown input_style."
+    
+    def override_year_items(self):
+        """The method gets called to get the right amount of years.
+        """
+        start_datetime = self.get_value('start_datetime')
+        end_datetime = self.get_value('end_datetime')
+        current_year = time.gmtime(time.time())[0]
+        if start_datetime:
+            first_year = start_datetime.year()
+        else:
+            first_year = current_year
+        if end_datetime:
+            last_year = end_datetime.year() + 1
+        else:
+            last_year = first_year + 11
+        return create_items(first_year, last_year, digits=4)
+
+            
+def create_datetime_text_sub_form():
+    sub_form = BasicForm()
+        
+    year = IntegerField('year',
+                        title="Year",
+                        required=0,
+                        display_width=4,
+                        display_maxwidth=4,
+                        max_length=4)
+    
+    month = IntegerField('month',
+                         title="Month",
+                         required=0,
+                         display_width=2,
+                         display_maxwidth=2,
+                         max_length=2)
+    
+    day = IntegerField('day',
+                       title="Day",
+                       required=0,
+                       display_width=2,
+                       display_maxwidth=2,
+                       max_length=2)
+    
+    sub_form.add_group("date")
+    sub_form.add_fields([year, month, day], "date")
+    
+    hour = IntegerField('hour',
+                        title="Hour",
+                        required=0,
+                        display_width=2,
+                        display_maxwidth=2,
+                        max_length=2)
+    
+    minute = IntegerField('minute',
+                          title="Minute",
+                          required=0,
+                          display_width=2,
+                          display_maxwidth=2,
+                          max_length=2)
+
+    sub_form.add_group("time")
+    sub_form.add_fields([hour, minute], "time")
+    return sub_form
+
+
+def create_datetime_list_sub_form():
+    sub_form = BasicForm()
+
+    year = ListField('year',
+                     title="Year",
+                     required=0,
+                     default="",
+                     items=create_items(2000, 2010, digits=4),
+                     size=1)
+    
+    month = ListField('month',
+                      title="Month",
+                      required=0,
+                      default="",
+                      items=create_items(1, 13, digits=2),
+                      size=1)
+    
+    day = ListField('day',
+                    title="Day",
+                    required=0,
+                    default="",
+                    items=create_items(1, 32, digits=2),
+                    size=1)
+
+    sub_form.add_group("date")
+    sub_form.add_fields([year, month, day], "date")
+    
+    hour = IntegerField('hour',
+                        title="Hour",
+                        required=0,
+                        display_width=2,
+                        display_maxwidth=2,
+                        max_length=2)
+    
+    minute = IntegerField('minute',
+                          title="Minute",
+                          required=0,
+                          display_width=2,
+                          display_maxwidth=2,
+                          max_length=2)
+
+    sub_form.add_group("time")
+    sub_form.add_fields([hour, minute], "time")
+    return sub_form
+
+def create_items(start, end, digits=0):
+    result = [("-", "")]
+    if digits:
+        format_string = "%0" + str(digits) + "d"
+    else:
+        format_string = "%s"
+        
+    for i in range(start, end):
+        s = format_string % i
+        result.append((s, s))
+    return result


=== Zope3/lib/python/Zope/App/Formulator/Fields/Generic/EmailField.py 1.1 => 1.2 ===
+#
+# Copyright (c) 2001, 2002 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.
+# 
+##############################################################################
+"""
+
+$Id$
+"""
+
+from Zope.App.Formulator.Field import Field
+from Zope.App.Formulator.Validators import EmailValidator 
+
+
+class EmailField(Field):
+
+    __implements__ = Field.__implements__
+
+    id = None
+    default = ''
+    title = 'Email Field'
+    description = 'Email Field'
+    validator = EmailValidator.EmailValidator()


=== Zope3/lib/python/Zope/App/Formulator/Fields/Generic/FileField.py 1.1 => 1.2 ===
+#
+# Copyright (c) 2001, 2002 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.
+# 
+##############################################################################
+"""
+
+$Id$
+"""
+
+from Zope.App.Formulator.Field import Field
+from Zope.App.Formulator.Validators import FileValidator 
+
+
+class FileField(Field):
+
+    __implements__ = Field.__implements__
+
+    id = None
+    default = ''
+    title = 'File Field'
+    description = 'File Field'
+    validator = FileValidator.FileValidator()


=== Zope3/lib/python/Zope/App/Formulator/Fields/Generic/FloatField.py 1.1 => 1.2 ===
+#
+# Copyright (c) 2001, 2002 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.
+# 
+##############################################################################
+"""
+
+$Id$
+"""
+
+from Zope.App.Formulator.Field import Field
+from Zope.App.Formulator.Validators import FloatValidator
+
+
+class FloatField(Field):
+
+    __implements__ = Field.__implements__
+
+    id = None
+    default = ''
+    title = 'Float Field'
+    description = 'Float Field'
+    validator = FloatValidator.FloatValidator()


=== Zope3/lib/python/Zope/App/Formulator/Fields/Generic/IntegerField.py 1.1 => 1.2 ===
+#
+# Copyright (c) 2001, 2002 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.
+# 
+##############################################################################
+"""
+
+$Id$
+"""
+
+from Zope.App.Formulator.Field import Field
+from Zope.App.Formulator.Validators import IntegerValidator
+
+
+class IntegerField(Field):
+
+    __implements__ = Field.__implements__
+
+    id = None
+    default = ''
+    title = 'Integer Field'
+    description = 'Integer Field'
+    validator = IntegerValidator.IntegerValidator()


=== Zope3/lib/python/Zope/App/Formulator/Fields/Generic/LinkField.py 1.1 => 1.2 ===
+#
+# Copyright (c) 2001, 2002 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.
+# 
+##############################################################################
+"""
+
+$Id$
+"""
+
+from Zope.App.Formulator.Fields.Field import Field
+from Zope.App.Formulator.Validators import LinkValidator
+
+
+class LinkField(Field):
+
+    __implements__ = Field.__implements__
+
+    id = None
+    default = ''
+    title = 'Link Field'
+    description = 'Link Field'
+    validator = LinkValidator.LinkValidator()


=== Zope3/lib/python/Zope/App/Formulator/Fields/Generic/ListField.py 1.1 => 1.2 ===
+#
+# Copyright (c) 2001, 2002 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.
+# 
+##############################################################################
+"""
+
+$Id$
+"""
+
+from Zope.App.Formulator.Field import Field
+from Zope.App.Formulator.Validators import SelectionValidator
+
+
+class ListField(Field):
+
+    __implements__ = Field.__implements__
+
+    id = None
+    default = ''
+    title = 'List Field'
+    description = 'List Field'
+    validator = SelectionValidator.SelectionValidator()


=== Zope3/lib/python/Zope/App/Formulator/Fields/Generic/MethodField.py 1.1 => 1.2 ===
+#
+# Copyright (c) 2001, 2002 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.
+# 
+##############################################################################
+from DummyField import fields
+import Widget, Validator
+from Globals import Persistent
+import Acquisition
+from Field import ZMIField
+from AccessControl import getSecurityManager
+
+class MethodWidget(Widget.TextWidget):
+    default = fields.MethodField('default',
+                                 title='Default',
+                                 default="",
+                                 required=0)
+    
+    def render(self, field, key, value, REQUEST):
+        if value == None:
+            method_name = field.get_value('default')
+        else:
+            if value != "":
+                method_name = value.method_name
+            else:
+                method_name = ""
+        return Widget.TextWidget.render(self, field, key, method_name, REQUEST)
+
+MethodWidgetInstance = MethodWidget()
+
+class Method(Persistent, Acquisition.Implicit):
+    """A method object; calls method name in acquisition context.
+    """
+    def __init__(self, method_name):
+        self.method_name = method_name
+        
+    def __call__(self, *arg, **kw):
+        # get method from acquisition path
+        method = getattr(self, self.method_name)
+        # check if we have 'View' permission for this method
+        # (raises error if not)
+        getSecurityManager().checkPermission('View', method)
+        # okay, execute it with supplied arguments
+        return apply(method, arg, kw)
+
+class BoundMethod(Method):
+    """A bound method calls a method on a particular object.
+    Should be used internally only.
+    """
+    def __init__(self, object, method_name):
+        BoundMethod.inheritedAttribute('__init__')(self, method_name)
+        self.object = object
+          
+    def __call__(self, *arg, **kw):
+        method = getattr(self.object, self.method_name)
+        return apply(method, arg, kw)
+    
+class MethodValidator(Validator.StringBaseValidator):
+
+    def validate(self, field, key, REQUEST):
+        value = Validator.StringBaseValidator.validate(self, field, key,
+                                                       REQUEST)
+
+        if value == "" and not field.get_value('required'):
+            return value
+
+        return Method(value)
+    
+MethodValidatorInstance = MethodValidator()
+
+class MethodField(ZMIField):
+    meta_type = 'MethodField'
+
+    internal_field = 1
+
+    widget = MethodWidgetInstance
+    validator = MethodValidatorInstance
+    
+    


=== Zope3/lib/python/Zope/App/Formulator/Fields/Generic/PasswordField.py 1.1 => 1.2 ===
+#
+# Copyright (c) 2001, 2002 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.
+# 
+##############################################################################
+"""
+
+$Id$
+"""
+
+from Zope.App.Formulator.Field import Field
+from Zope.App.Formulator.Validators import StringValidator
+
+
+class PasswordField(Field):
+
+    __implements__ = Field.__implements__
+
+    id = None
+    default = ''
+    title = 'Password Field'
+    description = 'Password Field'
+    validator = StringValidator.StringValidator()


=== Zope3/lib/python/Zope/App/Formulator/Fields/Generic/PatternField.py 1.1 => 1.2 ===
+#
+# Copyright (c) 2001, 2002 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.
+# 
+##############################################################################
+"""
+
+$Id$
+"""
+
+from Zope.App.Formulator.Field import Field
+from Zope.App.Formulator.Validators import PatternValidator
+
+
+class PatternField(Field):
+
+    __implements__ = Field.__implements__
+
+    id = None
+    default = ''
+    title = 'Pattern Field'
+    description = 'Pattern Field'
+    validator = PatternValidator.PatternValidator()


=== Zope3/lib/python/Zope/App/Formulator/Fields/Generic/StringField.py 1.1 => 1.2 ===
+#
+# Copyright (c) 2001, 2002 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.
+# 
+##############################################################################
+"""
+
+$Id$
+"""
+
+from Zope.App.Formulator.Field import Field
+from Zope.App.Formulator.Validators import StringValidator
+
+
+class StringField(Field):
+
+    __implements__ = Field.__implements__
+
+    id = None
+    default = ''
+    title = 'String Field'
+    description = 'String Field'
+    validator = StringValidator.StringValidator()


=== Zope3/lib/python/Zope/App/Formulator/Fields/Generic/TALESField.py 1.1 => 1.2 ===
+#
+# Copyright (c) 2001, 2002 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.
+# 
+##############################################################################
+from DummyField import fields
+import Widget, Validator
+from Globals import Persistent
+import Acquisition
+from Field import ZMIField
+from AccessControl import getSecurityManager
+   
+class TALESWidget(Widget.TextWidget):
+    default = fields.MethodField('default',
+                                 title='Default',
+                                 default="",
+                                 required=0)
+    
+    def render(self, field, key, value, REQUEST):
+        if value == None:
+            text = field.get_value('default')
+        else:
+            if value != "":
+                text = value._text
+            else:
+                text = ""
+        return Widget.TextWidget.render(self, field, key, text, REQUEST)
+
+TALESWidgetInstance = TALESWidget()
+
+class TALESNotAvailable(Exception):
+    pass
+
+try:
+    # try to import getEngine from TALES
+    from Products.PageTemplates.EngineConfig import getEngine
+    
+    class TALESMethod(Persistent, Acquisition.Implicit):
+        """A method object; calls method name in acquisition context.
+        """
+        def __init__(self, text):
+            self._text = text
+            #self._expr = getEngine().compile(text)
+
+        def __call__(self, **kw):
+            expr = getEngine().compile(self._text)
+            return getEngine().getContext(kw).evaluate(expr)
+
+            # check if we have 'View' permission for this method
+            # (raises error if not)
+            # getSecurityManager().checkPermission('View', method)
+
+    TALES_AVAILABLE = 1
+    
+except ImportError:
+    # cannot import TALES, so supply dummy TALESMethod
+    class TALESMethod(Persistent, Acquisition.Implicit):
+        """A dummy method in case TALES is not available.
+        """
+        def __init__(self, text):
+            self._text = text
+
+        def __call__(self, **kw):
+            raise TALESNotAvailable
+    TALES_AVAILABLE = 0
+    
+class TALESValidator(Validator.StringBaseValidator):
+
+    def validate(self, field, key, REQUEST):
+        value = Validator.StringBaseValidator.validate(self, field, key,
+                                                       REQUEST)
+
+        if value == "" and not field.get_value('required'):
+            return value
+
+        return TALESMethod(value)
+    
+TALESValidatorInstance = TALESValidator()
+
+class TALESField(ZMIField):
+    meta_type = 'TALESField'
+
+    internal_field = 1
+
+    widget = TALESWidgetInstance
+    validator = TALESValidatorInstance
+    
+    


=== Zope3/lib/python/Zope/App/Formulator/Fields/Generic/__init__.py 1.1 => 1.2 ===
+#
+# Copyright (c) 2001, 2002 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.
+# 
+##############################################################################