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 | The code was mainly taken from usphone implementation in z3c.widget |
---|
11 | (which was designed for outdated zope.app.form (instead of |
---|
12 | zope.formlib or newer form libs). Therefore some modifications were |
---|
13 | neccessary and it might look a bit overloaded. |
---|
14 | |
---|
15 | If you use the PhoneWidget for rendering regular TextLine attributes |
---|
16 | (preferably in edit forms or add forms), the phone number is displayed |
---|
17 | by three input fields representing the international code, the area |
---|
18 | code and the extension line. All three fields require pure numbers as |
---|
19 | input and do not accept other chars. |
---|
20 | |
---|
21 | When the entered input is stored with a context object, it is stored |
---|
22 | as a single unicode string with the numbers divided by single hyphen. |
---|
23 | |
---|
24 | So, input <12>, <111>, <444> becomes the string ``'12-111-444'`` for |
---|
25 | the context object. |
---|
26 | |
---|
27 | Large parts of this module are copied from z3c.widget and may be |
---|
28 | covered by ZPL. |
---|
29 | |
---|
30 | """ |
---|
31 | import re |
---|
32 | import copy |
---|
33 | from waeup.sirp import MessageFactory as _ |
---|
34 | from zope import schema |
---|
35 | from zope.browserpage import ViewPageTemplateFile |
---|
36 | from zope.component import getMultiAdapter |
---|
37 | from zope.formlib import form |
---|
38 | from zope.formlib.interfaces import ( |
---|
39 | IBrowserWidget, IWidgetInputErrorView, IInputWidget, WidgetInputError, |
---|
40 | MissingInputError) |
---|
41 | from zope.formlib.widget import SimpleInputWidget |
---|
42 | from zope.interface import Interface, implements |
---|
43 | from zope.schema.interfaces import RequiredMissing |
---|
44 | |
---|
45 | class IPhoneData(Interface): |
---|
46 | """A schema used to generate a Phone widget.""" |
---|
47 | |
---|
48 | country = schema.TextLine( |
---|
49 | title=_('Country Code'), |
---|
50 | description=_('The country code of the phone number.'), |
---|
51 | min_length=1, |
---|
52 | constraint=re.compile(r'^[0-9]+$').search, |
---|
53 | default=u'234', # int. code for nigeria |
---|
54 | required=True) |
---|
55 | |
---|
56 | area = schema.TextLine( |
---|
57 | title=_('Area Code'), |
---|
58 | description=_('The area code of the phone number.'), |
---|
59 | min_length=1, |
---|
60 | constraint=re.compile(r'^[0-9]+$').search, |
---|
61 | required=True) |
---|
62 | |
---|
63 | extension = schema.TextLine( |
---|
64 | title=_('Direct Line'), |
---|
65 | description=_('The direct line of the phone number.'), |
---|
66 | min_length=3, |
---|
67 | constraint=re.compile(r'^[0-9]{3,}$').search, |
---|
68 | required=True) |
---|
69 | |
---|
70 | class 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 | |
---|
108 | class 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 | # we have to modify the adapter according to our own |
---|
132 | # 'required' state. Some ugly workaround |
---|
133 | adapter = copy.deepcopy(IPhoneData) |
---|
134 | for name in 'country', 'area', 'extension': |
---|
135 | adapter[name].required = self.required |
---|
136 | adapters = {adapter: PhoneWidgetData(self, value)} |
---|
137 | self.widgets = form.setUpEditWidgets( |
---|
138 | form.FormFields(IPhoneData), |
---|
139 | self.name, value, self.request, adapters=adapters) |
---|
140 | self.widgets['country'].displayWidth = 3 |
---|
141 | self.widgets['area'].displayWidth = 5 |
---|
142 | self.widgets['extension'].displayWidth = 10 |
---|
143 | return |
---|
144 | |
---|
145 | def setRenderedValue(self, value): |
---|
146 | """See zope.formlib.interfaces.IWidget""" |
---|
147 | if isinstance(value, unicode) and '-' in value: |
---|
148 | country, area, extension = value.split('-', 2) |
---|
149 | self.widgets['country'].setRenderedValue(country) |
---|
150 | self.widgets['area'].setRenderedValue(area) |
---|
151 | self.widgets['extension'].setRenderedValue(extension) |
---|
152 | return |
---|
153 | return |
---|
154 | |
---|
155 | def setPrefix(self, prefix): |
---|
156 | """See zope.formlib.interfaces.IWidget""" |
---|
157 | # Set the prefix locally |
---|
158 | if not prefix.endswith("."): |
---|
159 | prefix += '.' |
---|
160 | self._prefix = prefix |
---|
161 | self.name = prefix + self.context.__name__ |
---|
162 | # Now distribute it to the sub-widgets |
---|
163 | self._generateSubWidgets() |
---|
164 | return |
---|
165 | |
---|
166 | def getInputValue(self): |
---|
167 | """See zope.formlib.interfaces.IInputWidget""" |
---|
168 | self._error = None |
---|
169 | try: |
---|
170 | result = u'-'.join(( |
---|
171 | self.widgets['country'].getInputValue(), |
---|
172 | self.widgets['area'].getInputValue(), |
---|
173 | self.widgets['extension'].getInputValue() )) |
---|
174 | return result |
---|
175 | except TypeError: |
---|
176 | if self.required: |
---|
177 | return self._missing |
---|
178 | return None |
---|
179 | except ValueError, v: |
---|
180 | self._error = WidgetInputError( |
---|
181 | self.context.__name__, self.label, _(v)) |
---|
182 | raise self._error |
---|
183 | except WidgetInputError, e: |
---|
184 | self._error = e |
---|
185 | raise e |
---|
186 | |
---|
187 | def applyChanges(self, content): |
---|
188 | """See zope.formlib.interfaces.IInputWidget""" |
---|
189 | field = self.context |
---|
190 | new_value = self.getInputValue() |
---|
191 | old_value = field.query(content, self) |
---|
192 | # The selection has not changed |
---|
193 | if new_value == old_value: |
---|
194 | return False |
---|
195 | field.set(content, new_value) |
---|
196 | return True |
---|
197 | |
---|
198 | def hasInput(self): |
---|
199 | """See zope.formlib.interfaces.IInputWidget""" |
---|
200 | return (self.widgets['country'].hasInput() and |
---|
201 | (self.widgets['area'].hasInput() and |
---|
202 | self.widgets['extension'].hasInput())) |
---|
203 | |
---|
204 | |
---|
205 | def hasValidInput(self): |
---|
206 | """See zope.formlib.interfaces.IInputWidget""" |
---|
207 | return (self.widgets['country'].hasValidInput() and |
---|
208 | self.widgets['area'].hasValidInput() and |
---|
209 | self.widgets['extension'].hasValidInput()) |
---|
210 | |
---|
211 | |
---|
212 | def hidden(self): |
---|
213 | """See zope.formlib.interfaces.IBrowserWidget""" |
---|
214 | output = [] |
---|
215 | output.append(self.widgets['country'].hidden()) |
---|
216 | output.append(self.widgets['area'].hidden()) |
---|
217 | output.append(self.widgets['extension'].hidden()) |
---|
218 | return '\n'.join(output) |
---|
219 | |
---|
220 | |
---|
221 | def error(self): |
---|
222 | """See zope.formlib.interfaces.IBrowserWidget""" |
---|
223 | if self._error: |
---|
224 | return getMultiAdapter( |
---|
225 | (self._error, self.request), |
---|
226 | IWidgetInputErrorView).snippet() |
---|
227 | country_error = self.widgets['country'].error() |
---|
228 | if country_error: |
---|
229 | return country_error |
---|
230 | area_error = self.widgets['area'].error() |
---|
231 | if area_error: |
---|
232 | return area_error |
---|
233 | extension_error = self.widgets['extension'].error() |
---|
234 | if extension_error: |
---|
235 | return extension_error |
---|
236 | return "" |
---|
237 | |
---|
238 | def __call__(self): |
---|
239 | """See zope.formlib.interfaces.IBrowserWidget""" |
---|
240 | return self.template() |
---|