source: main/waeup.kofa/trunk/src/waeup/kofa/widgets/phonewidget.py @ 8171

Last change on this file since 8171 was 8169, checked in by uli, 13 years ago

Shorten code.

  • Property svn:keywords set to Id
File size: 6.6 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
10If you use the PhoneWidget for rendering regular TextLine attributes
11(preferably in edit forms or add forms), the phone number is displayed
12by three input fields representing the international code, the area
13code and the extension line.
14
15When the entered input is stored with a context object, it is stored
16as a single unicode string with the numbers divided by single hyphen.
17
18So, input <+12>, <111>, <444> becomes the string ``'+12-111-444'`` for
19the context object.
20"""
21import grok
22import re
23from zope.component import queryUtility
24from zope.formlib.interfaces import MissingInputError, InputErrors
25from zope.interface import Interface
26from zope.formlib.textwidgets import (
27    TextWidget, renderElement, ConversionError)
28from waeup.kofa.interfaces import IKofaUtils
29from waeup.kofa.interfaces import MessageFactory as _
30from waeup.kofa.utils.utils import KofaUtils
31
32RE_INT_PREFIX = re.compile('^\+\d+')
33RE_NUMBERS = re.compile('^\d+$')
34RE_NUMBERS_AND_HYPHENS = re.compile('^[\d\-]+$')
35
36class PhoneWidget(TextWidget):
37
38    subwidget_names = ('country', 'area', 'ext')
39    bootstrap_span = 'span2'
40
41    def _renderPrefixWidget(self, value):
42        prefix_func = getattr(
43            queryUtility(IKofaUtils), 'sorted_phone_prefixes',
44            KofaUtils.sorted_phone_prefixes)
45        options = []
46        for ptitle, pval in prefix_func(request=self.request):
47            selected = ''
48            if value == pval:
49                selected = ' selected="selected" '
50            options.append(
51                '<option value="%s"%s>%s</option>' % (pval, selected, ptitle))
52        options = '\n'.join(options)
53        return '<select id="%s" name="%s" size="1" class="span4">\n%s\n</select>' % (
54            '%s.%s' % (self.name, 'country'),
55            '%s.%s' % (self.name, 'country'),
56            options)
57
58    def __call__(self):
59        value = self._getFormValue()
60        if value is None or value == self.context.missing_value:
61            value = ''
62        if len(value.split('-')) < 2:
63            value = '--' + value
64        subvalues = value.split('-', 2)
65
66        kwargs = {'type': self.type,
67                  'name': self.name,
68                  'id': self.name,
69                  'value': value,
70                  'cssClass': self.cssClass,
71                  'style': self.style,
72                  'size': self.displayWidth,
73                  'extra': self.extra}
74        if self.displayMaxWidth:
75            kwargs['maxlength'] = self.displayMaxWidth # TODO This is untested.
76        fields = []
77        for num, subname in enumerate(self.subwidget_names):
78            if num == 0:
79                select = self._renderPrefixWidget(subvalues[num])
80                fields.append(select)
81                continue
82                print select
83            kwargs.update(name = '%s.%s' % (self.name, subname))
84            kwargs.update(id=kwargs['name'])
85            # kwargs.update(cssClass = '%s %s' % (self.cssClass, 'span1'))
86            kwargs.update(cssClass = '%s %s' % ('', self.bootstrap_span))
87            kwargs.update(value = subvalues[num])
88            fields.append(renderElement(self.tag, **kwargs))
89        return '-'.join(fields)
90
91    def _getFormInput(self):
92        """Returns current form input.
93
94        The value returned must be in a format that can be used as the 'input'
95        argument to `_toFieldValue`.
96
97        The default implementation returns the form value that corresponds to
98        the widget's name. Subclasses may override this method if their form
99        input consists of more than one form element or use an alternative
100        naming convention.
101        """
102        result = '-'.join(
103            [self.request.get('%s.%s' % (self.name, name), '')
104             for name in self.subwidget_names])
105        return result
106
107    def _toFieldValue(self, input):
108        """Check value entered in form further.
109
110        Raises ConversionError if values entered contain non-numbers.
111
112        For the extension line we silently allow slashes as well.
113        """
114        result = super(PhoneWidget, self)._toFieldValue(input)
115        parts = input.split('-', 2)
116        if '' in parts and self.context.required:
117            raise ConversionError(
118                _("Empty phone field(s)."), MissingInputError(
119                    self.name, self.label, None))
120        if parts[0] != '' and not RE_INT_PREFIX.match(parts[0]):
121            raise ConversionError(
122                _("Int. prefix requires format '+NNN'"),
123                ValueError('invalid international prefix'))
124        # Make sure there are only numbers in parts 1..N. We do not allow
125        # dashes in last field any more.
126        errors = [(x != '' and not RE_NUMBERS.match(x)) for x in parts[1:]]
127        error = True in errors
128        if error:
129            raise ConversionError(
130                _("Phone numbers may contain numbers only."),
131                ValueError('non numbers in phone number'))
132        # We consider also values ending with empty ending as missing
133        # values.  An 'empty ending' is a form where the fields
134        # following the prefix are all empty.  This means that any
135        # prefix setting in the form will switch back to default upon
136        # submit if no further phone fields are filled.  As advantage
137        # we get only valid phone numbers or missing value.
138        empty_ending = '-'*(len(parts) - 1)
139        if result in ('', None) or result.endswith(empty_ending):
140            result = self.context.missing_value
141        return result
142
143    def _getFormValue(self):
144        """Returns a value suitable for use in an HTML form.
145
146        Detects the status of the widget and selects either the input value
147        that came from the request, the value from the _data attribute or the
148        default value.
149        """
150        try:
151            input_value = self._getCurrentValueHelper()
152        except InputErrors:
153            form_value = '-'.join(
154                [self.request.form.get('%s.%s' % (self.name, name), '')
155                 for name in self.subwidget_names]
156                )
157        else:
158            form_value = self._toFormValue(input_value)
159        return form_value
160
161    def hasInput(self):
162        """A phone widget has input if all three subfields have input.
163        """
164        for name in self.subwidget_names:
165            if '%s.%s' % (self.name, name) not in self.request.form:
166                return False
167        return True
Note: See TracBrowser for help on using the repository browser.