source: main/waeup.sirp/trunk/src/waeup/sirp/widgets/phonewidget.py @ 7430

Last change on this file since 7430 was 7428, checked in by uli, 13 years ago

Do not complain if value to set is empty string.

  • Property svn:keywords set to Id
File size: 8.0 KB
Line 
1"""A phone number widget.
2
3This widget is an input widget (not made for display forms but for
4edit and add forms).
5
6It can be used for :class:`zope.schema.TextLine` fields but has to be
7requested by the form manually (otherwise the regular TextLine widget
8will be used for rendering).
9
10The code was mainly taken from usphone implementation in z3c.widget
11(which was designed for outdated zope.app.form (instead of
12zope.formlib or newer form libs). Therefore some modifications were
13neccessary and it might look a bit overloaded.
14
15If you use the PhoneWidget for rendering regular TextLine attributes
16(preferably in edit forms or add forms), the phone number is displayed
17by three input fields representing the international code, the area
18code and the extension line. All three fields require pure numbers as
19input and do not accept other chars.
20
21When the entered input is stored with a context object, it is stored
22as a single unicode string with the numbers divided by single hyphen.
23
24So, input <12>, <111>, <444> becomes the string ``'12-111-444'`` for
25the context object.
26
27Large parts of this module are copied from z3c.widget and may be
28covered by ZPL.
29
30"""
31import re
32from waeup.sirp import MessageFactory as _
33from zope import schema
34from zope.browserpage import ViewPageTemplateFile
35from zope.component import getMultiAdapter
36from zope.formlib import form
37from zope.formlib.interfaces import (
38    IBrowserWidget, IWidgetInputErrorView, IInputWidget, WidgetInputError,
39    MissingInputError)
40from zope.formlib.widget import SimpleInputWidget
41from zope.interface import Interface, implements
42from zope.schema.interfaces import RequiredMissing
43
44class IPhoneData(Interface):
45    """A schema used to generate a Phone widget."""
46
47    country = schema.TextLine(
48        title=_('Country Code'),
49        description=_('The country code of the phone number.'),
50        min_length=1,
51        constraint=re.compile(r'^[0-9]+$').search,
52        default=u'234', # int. code for nigeria
53        required=True)
54
55    area = schema.TextLine(
56        title=_('Area Code'),
57        description=_('The area code of the phone number.'),
58        min_length=1,
59        constraint=re.compile(r'^[0-9]+$').search,
60        required=True)
61
62    extension = schema.TextLine(
63        title=_('Direct Line'),
64        description=_('The direct line of the phone number.'),
65        min_length=3,
66        constraint=re.compile(r'^[0-9]{3,}$').search,
67        required=True)
68
69
70class PhoneWidgetData(object):
71    """Phone number data"""
72    implements(IPhoneData)
73
74    country = None
75    area = None
76    extension = None
77
78    def __init__(self, context, number=None):
79        """Set countrycode, areacode, and extension line from number.
80        """
81        self.context = context
82        if number is None:
83            return
84        # handle other types than strings.
85        if not isinstance(number, basestring):
86            number = str(number)
87        parts = number.split('-', 2)
88        if len(parts) == 2:
89            parts = [None,] + parts
90        elif len(parts) == 0:
91            return
92        elif len(parts) == 1:
93            parts = [None, None] + parts
94        self.country, self.area, self.extension = parts
95        return
96
97    @property
98    def number(self):
99        """Return a valid phone number string of format ``<IC>-<AC>-<EL>``.
100
101        where <IC> is the country code (digits only), <AC> is the area
102        code and <EL> is the extension line.
103        """
104        return u'-'.join(
105            (self.country or '', self.area or '', self.extension))
106
107
108class PhoneWidget(SimpleInputWidget):
109    """Phone Number Widget"""
110    implements(IBrowserWidget, IInputWidget)
111
112    template = ViewPageTemplateFile('phonewidget.pt')
113    _prefix = 'field.'
114    _error = None
115    widgets = {}
116
117    # See zope.formlib.interfaces.IWidget
118    name = None
119    visible = True
120
121    def __init__(self, field, request):
122        super(PhoneWidget, self).__init__(field, request)
123        value = field.query(field.context)
124        self._generateSubWidgets()
125        return
126
127    def _generateSubWidgets(self):
128        """Create the three subwidgets.
129        """
130        value = self.context.query(self.context.context)
131        adapters = {}
132        adapters[IPhoneData] = PhoneWidgetData(self, value)
133        self.widgets = form.setUpEditWidgets(
134            form.FormFields(IPhoneData),
135            self.name, value, self.request, adapters=adapters)
136        self.widgets['country'].displayWidth = 3
137        self.widgets['area'].displayWidth = 5
138        self.widgets['extension'].displayWidth = 10
139        return
140
141    def setRenderedValue(self, value):
142        """See zope.formlib.interfaces.IWidget"""
143        if isinstance(value, unicode) and '-' in value:
144            country, area, extension = value.split('-', 2)
145            self.widgets['country'].setRenderedValue(country)
146            self.widgets['area'].setRenderedValue(area)
147            self.widgets['extension'].setRenderedValue(extension)
148            return
149        return
150
151    def setPrefix(self, prefix):
152        """See zope.formlib.interfaces.IWidget"""
153        # Set the prefix locally
154        if not prefix.endswith("."):
155            prefix += '.'
156        self._prefix = prefix
157        self.name = prefix + self.context.__name__
158        # Now distribute it to the sub-widgets
159        self._generateSubWidgets()
160        return
161
162    def getInputValue(self):
163        """See zope.formlib.interfaces.IInputWidget"""
164        self._error = None
165        try:
166            return u'-'.join((
167                self.widgets['country'].getInputValue(),
168                self.widgets['area'].getInputValue(),
169                self.widgets['extension'].getInputValue() ))
170        except ValueError, v:
171            self._error = WidgetInputError(
172                self.context.__name__, self.label, _(v))
173            raise self._error
174        except WidgetInputError, e:
175            # Subwidgets require input. We have to work around that if
176            # the parent widget does not require input.
177            missing = isinstance(e.errors, RequiredMissing)
178            missing = missing or isinstance(e, MissingInputError)
179            if not self.required and missing:
180                return self._missing
181            self._error = e
182            raise e
183
184    def applyChanges(self, content):
185        """See zope.formlib.interfaces.IInputWidget"""
186        field = self.context
187        new_value = self.getInputValue()
188        old_value = field.query(content, self)
189        # The selection has not changed
190        if new_value == old_value:
191            return False
192        field.set(content, new_value)
193        return True
194
195    def hasInput(self):
196        """See zope.formlib.interfaces.IInputWidget"""
197        return (self.widgets['country'].hasInput() and
198                (self.widgets['area'].hasInput() and
199                 self.widgets['extension'].hasInput()))
200
201
202    def hasValidInput(self):
203        """See zope.formlib.interfaces.IInputWidget"""
204        return (self.widgets['country'].hasValidInput() and
205            self.widgets['area'].hasValidInput() and
206            self.widgets['extension'].hasValidInput())
207
208
209    def hidden(self):
210        """See zope.formlib.interfaces.IBrowserWidget"""
211        output = []
212        output.append(self.widgets['country'].hidden())
213        output.append(self.widgets['area'].hidden())
214        output.append(self.widgets['extension'].hidden())
215        return '\n'.join(output)
216
217
218    def error(self):
219        """See zope.formlib.interfaces.IBrowserWidget"""
220        if self._error:
221            return getMultiAdapter(
222                (self._error, self.request),
223                IWidgetInputErrorView).snippet()
224        country_error = self.widgets['country'].error()
225        if country_error:
226            return country_error
227        area_error = self.widgets['area'].error()
228        if area_error:
229            return area_error
230        extension_error = self.widgets['extension'].error()
231        if extension_error:
232            return extension_error
233        return ""
234
235    def __call__(self):
236        """See zope.formlib.interfaces.IBrowserWidget"""
237        return self.template()
Note: See TracBrowser for help on using the repository browser.