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