~hardware-certification/zope3/certify-staging-2.5

« back to all changes in this revision

Viewing changes to src/zope/app/form/__init__.py

  • Committer: Marc Tardif
  • Date: 2008-04-26 19:03:34 UTC
  • Revision ID: cr3@lime-20080426190334-u16xo4llz56vliqf
Initial import.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
 
4
# All Rights Reserved.
 
5
#
 
6
# This software is subject to the provisions of the Zope Public License,
 
7
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
 
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
 
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 
11
# FOR A PARTICULAR PURPOSE.
 
12
#
 
13
##############################################################################
 
14
"""Generic Widget base classes
 
15
 
 
16
$Id: __init__.py 71638 2006-12-20 23:34:35Z jacobholm $
 
17
"""
 
18
__docformat__ = 'restructuredtext'
 
19
 
 
20
from zope.app.form.interfaces import IWidget, InputErrors
 
21
from zope.cachedescriptors.property import readproperty
 
22
from zope.component.interfaces import IViewFactory
 
23
from zope.deprecation import deprecated
 
24
from zope.interface import implements
 
25
from zope.i18n import translate
 
26
from zope.schema.interfaces import IChoice, ICollection
 
27
 
 
28
class Widget(object):
 
29
    """Mixin class providing functionality common across widget types."""
 
30
 
 
31
    implements(IWidget)
 
32
 
 
33
    _prefix = 'field.'
 
34
    _data_marker = object()
 
35
    _data = _data_marker
 
36
 
 
37
    visible = True
 
38
 
 
39
    def __init__(self, context, request):
 
40
        self.context = context
 
41
        self.request = request
 
42
        self.name = self._prefix + context.__name__
 
43
 
 
44
    @readproperty
 
45
    def label(self):
 
46
        """The widget label.
 
47
 
 
48
        This read-write attribute defaults to the title of the context.
 
49
        """
 
50
        return self.context.title
 
51
 
 
52
    @readproperty
 
53
    def hint(self):
 
54
        """A hint regarding the use of the widget.
 
55
 
 
56
        This read-write attribute defaults to the description of the context.
 
57
        """
 
58
        return self.context.description
 
59
 
 
60
    def _translate(self, text):
 
61
        return translate(text, context=self.request, default=text)
 
62
 
 
63
    def _renderedValueSet(self):
 
64
        """Returns ``True`` if the the widget's rendered value has been set.
 
65
 
 
66
        This is a convenience method that widgets can use to check whether
 
67
        or not `setRenderedValue` was called.
 
68
        """
 
69
        return self._data is not self._data_marker
 
70
 
 
71
    def setPrefix(self, prefix):
 
72
        if prefix and not prefix.endswith("."):
 
73
            prefix += '.'
 
74
        self._prefix = prefix
 
75
        self.name = prefix + self.context.__name__
 
76
 
 
77
    def setRenderedValue(self, value):
 
78
        self._data = value
 
79
 
 
80
 
 
81
class InputWidget(Widget):
 
82
    """Mixin class providing some default input widget methods."""
 
83
 
 
84
    def hasValidInput(self):
 
85
        try:
 
86
            self.getInputValue()
 
87
            return True
 
88
        except InputErrors:
 
89
            return False
 
90
 
 
91
    def applyChanges(self, content):
 
92
        field = self.context
 
93
        value = self.getInputValue()
 
94
        if field.query(content, self) != value:
 
95
            field.set(content, value)
 
96
            return True
 
97
        else:
 
98
            return False
 
99
 
 
100
 
 
101
class CustomWidgetFactory(object):
 
102
    """Custom Widget Factory."""
 
103
    implements(IViewFactory)
 
104
 
 
105
    def __init__(self, widget_factory, *args, **kw):
 
106
        self._widget_factory = widget_factory
 
107
        self.args = args
 
108
        self.kw = kw
 
109
 
 
110
    def _create(self, args):
 
111
        instance = self._widget_factory(*args)
 
112
        for name, value in self.kw.items():
 
113
            setattr(instance, name, value)
 
114
        return instance
 
115
 
 
116
    def __call__(self, context, request):
 
117
        # Sequence widget factory
 
118
        if ICollection.providedBy(context):
 
119
            args = (context, context.value_type, request) + self.args
 
120
 
 
121
        # Vocabulary widget factory
 
122
        elif IChoice.providedBy(context):
 
123
            args = (context, context.vocabulary, request) + self.args
 
124
 
 
125
        # Regular widget factory
 
126
        else:
 
127
            args = (context, request) + self.args
 
128
 
 
129
        return self._create(args)