- Timestamp:
- 11 Mar 2012, 18:47:20 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.kofa/trunk/src/waeup/kofa/widgets/phonewidget.py
r7811 r7840 29 29 30 30 """ 31 import grok 31 32 import re 32 33 import copy … … 76 77 extension = None 77 78 78 def __init__(self, context , number=None):79 def __init__(self, context=None, number=None): 79 80 """Set countrycode, areacode, and extension line from number. 80 81 """ … … 246 247 """See zope.formlib.interfaces.IBrowserWidget""" 247 248 return self.template() 249 250 from zope.formlib.objectwidget import ObjectWidget 251 from zope.schema import Object 252 from waeup.kofa.widgets.objectwidget import KofaObjectWidget, ObjectWidgetView 253 from zc.sourcefactory.contextual import BasicContextualSourceFactory 254 255 256 257 class KofaPhoneWidgetView(ObjectWidgetView): 258 template = ViewPageTemplateFile('phonewidget2.pt') 259 260 class PhoneWidget2(KofaObjectWidget): 261 262 _fake_value = None 263 264 def __init__(self, context, request, **kw): 265 self._fake_context = Object(IPhoneData, __name__=context.__name__) 266 super(PhoneWidget2, self).__init__( 267 self._fake_context, request, PhoneWidgetData, **kw) 268 return 269 270 def _to_fake_value(value): 271 return PhoneWidgetData(None, value.number) 272 273 def _from_fake_value(value): 274 return value.number 275 276 def _getView(self, request): 277 return KofaPhoneWidgetView(self, request) 278 279 from zope.formlib.textwidgets import TextWidget, renderElement 280 from zope.component import getUtility 281 from zope.interface import Invalid 282 from zope.formlib.interfaces import WidgetInputError, MissingInputError 283 284 class IInternationalPhonePrefixes(Interface): 285 """A dict of international phone number prefixes. 286 """ 287 288 INT_PHONE_PREFIXES = { 289 'Germany': '49', 290 'Nigeria': '234', 291 'U.S.': '1', 292 } 293 294 class PhonePrefixes(grok.GlobalUtility): 295 grok.implements(IInternationalPhonePrefixes) 296 297 _data = INT_PHONE_PREFIXES.items() 298 299 def title_value_list(self): 300 return sorted([('%s (+%s)' % (x,y), y) 301 for x,y in self._data]) 302 303 class PhoneWidget2(TextWidget): 304 305 subwidget_names = ('country', 'area', 'ext') 306 307 #_missing = '--' 308 309 def _renderPrefixWidget(self, value): 310 prefixes = getUtility( 311 IInternationalPhonePrefixes).title_value_list() 312 options = [] 313 for ptitle, pval in prefixes: 314 selected = '' 315 if value == pval: 316 selected = ' selected="selected" ' 317 options.append( 318 '<option value="%s"%s>%s</option>' % (pval, selected, ptitle)) 319 options = '\n'.join(options) 320 return '<select id="%s" name="%s" size="1" class="span4">\n%s\n</select>' % ( 321 '%s.%s' % (self.name, 'country'), 322 '%s.%s' % (self.name, 'country'), 323 options) 324 325 def __call__(self): 326 value = self._getFormValue() 327 if value is None or value == self.context.missing_value: 328 value = '' 329 if len(value.split('-')) < 2: 330 value = '--' + value 331 subvalues = value.split('-', 2) 332 333 kwargs = {'type': self.type, 334 'name': self.name, 335 'id': self.name, 336 'value': value, 337 'cssClass': self.cssClass, 338 'style': self.style, 339 'size': self.displayWidth, 340 'extra': self.extra} 341 if self.displayMaxWidth: 342 kwargs['maxlength'] = self.displayMaxWidth # TODO This is untested. 343 fields = [] 344 for num, subname in enumerate(self.subwidget_names): 345 if num == 0: 346 select = self._renderPrefixWidget(subvalues[num]) 347 fields.append(select) 348 continue 349 print select 350 kwargs.update(name = '%s.%s' % (self.name, subname)) 351 kwargs.update(id=kwargs['name']) 352 # kwargs.update(cssClass = '%s %s' % (self.cssClass, 'span1')) 353 kwargs.update(cssClass = '%s %s' % ('', 'span2')) 354 kwargs.update(value = subvalues[num]) 355 fields.append(renderElement(self.tag, **kwargs)) 356 return '-'.join(fields) 357 358 def _getFormInput(self): 359 """Returns current form input. 360 361 The value returned must be in a format that can be used as the 'input' 362 argument to `_toFieldValue`. 363 364 The default implementation returns the form value that corresponds to 365 the widget's name. Subclasses may override this method if their form 366 input consists of more than one form element or use an alternative 367 naming convention. 368 """ 369 result = '-'.join( 370 [self.request.get('%s.%s' % (self.name, name)) 371 for name in self.subwidget_names]) 372 return result 373 374 def hasInput(self): 375 for name in self.subwidget_names: 376 if '%s.%s' % (self.name, name) not in self.request.form: 377 return False 378 return True
Note: See TracChangeset for help on using the changeset viewer.