Changeset 7840
- Timestamp:
- 11 Mar 2012, 18:47:20 (13 years ago)
- Location:
- main/waeup.kofa/trunk/src/waeup/kofa/widgets
- Files:
-
- 2 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 -
main/waeup.kofa/trunk/src/waeup/kofa/widgets/tests/test_phonewidget.py
r7811 r7840 26 26 # XXX: could we possibly get rid of zope.app.testing? 27 27 from zope.app.testing.setup import placefulSetUp, placefulTearDown 28 from zope.component import provideAdapter 28 from zope.component import provideAdapter, getGlobalSiteManager 29 from zope.formlib import form 29 30 from zope.formlib.exception import WidgetInputErrorView 30 31 from zope.formlib.interfaces import ( … … 48 49 title = u'Phone', 49 50 description = u'Phone number (not required)', 50 required = False,) 51 required = False, 52 missing_value = u'') 51 53 52 54 baz = schema.TextLine( … … 54 56 description = u'Required phone with a default', 55 57 required = True, 56 default=u'234--') 58 #default=u'234--' 59 ) 57 60 58 61 class SampleContent: 59 62 implements(ISampleContent) 63 64 foo = None 65 bar = 'bar' 66 baz = None 60 67 61 68 class PhoneWidgetTests(unittest.TestCase): … … 497 504 u' value="" />\n')) 498 505 return 506 507 import zope.formlib 508 from zope import schema 509 from zope.formlib.tests.test_functional_objectwidget import ( 510 Test, PlacelessSetup, VerifyResults, traversingSetUp, xmlconfig, 511 PageTemplate, os, DefaultTraversable 512 ) 513 from zope.formlib.tests.test_functional_textwidget import( 514 FunctionalWidgetTestCase, patternExists) 515 from zope.formlib.widgets import TextWidget 516 from zope.component import adapts 517 from zope.interface import implements 518 from zope.traversing.interfaces import ITraversable 519 from waeup.kofa.widgets.phonewidget import PhoneWidget2, PhonePrefixes 520 521 class SampleForm(form.EditForm): 522 form_fields = form.fields(ISampleContent) 523 form_fields['foo'].custom_widget = PhoneWidget2 524 form_fields['bar'].custom_widget = PhoneWidget2 525 form_fields['baz'].custom_widget = PhoneWidget2 526 527 class PhoneWidget2Tests(FunctionalWidgetTestCase): 528 529 widgets = [ 530 (ITextLine, TextWidget), 531 ] 532 533 def setUp(self): 534 super(PhoneWidget2Tests, self).setUp() 535 self.gsm = getGlobalSiteManager() 536 self.reg_prefixes = PhonePrefixes() 537 self.gsm.registerUtility(self.reg_prefixes) 538 return 539 540 def tearDown(self): 541 self.gsm.unregisterUtility(self.reg_prefixes) 542 return 543 544 def test_display_editform(self): 545 content = SampleContent() 546 request = TestRequest() 547 html = SampleForm(content, request)() 548 # foo.country, foo.area and foo.ext exist 549 self.assert_(patternExists( 550 '<select .* name="form.foo.country".*>', html)) 551 self.assert_(patternExists( 552 '<input .* name="form.foo.area".* value="".*>', html)) 553 self.assert_(patternExists( 554 '<input .* name="form.foo.ext".* value="".*>', html)) 555 return 556 557 def test_submit_editform(self): 558 # we can submit an edit form 559 content = SampleContent() 560 request = TestRequest() 561 562 # submit edit view 563 request.form['form.foo.country'] = u'123' 564 request.form['form.foo.area'] = u'456' 565 request.form['form.foo.ext'] = u'7890' 566 request.form['form.actions.apply'] = u'' 567 SampleForm(content, request)() 568 569 # check new values in object 570 self.assertEqual(content.foo, u'123-456-7890') 571 return 572 573 def test_invalid_type(self): 574 # there is no invalid type for textline-based input 575 content = SampleContent() 576 request = TestRequest() 577 578 # submit invalid type for text line 579 request.form['form.foo.country'] = '123' 580 request.form['form.foo.area'] = '456' 581 request.form['form.foo.ext'] = '7890' 582 request.form['form.actions.apply'] = u'' 583 html = SampleForm(content, request)() 584 585 # We don't have a invalid field value 586 # since we convert the value to unicode 587 self.assert_('Object is of wrong type.' not in html) 588 return 589 590 def test_missing_value(self): 591 content = SampleContent() 592 request = TestRequest() 593 594 request.form['form.foo.country'] = u'123' 595 request.form['form.foo.area'] = u'456' 596 request.form['form.foo.ext'] = u'7890' 597 request.form['form.bar.country'] = u'' 598 request.form['form.bar.area'] = u'' 599 request.form['form.bar.ext'] = u'' 600 request.form['form.baz.country'] = u'' 601 request.form['form.baz.area'] = u'' 602 request.form['form.baz.ext'] = u'' 603 request.form['form.actions.apply'] = u'' 604 SampleForm(content, request)() 605 606 # check new values in object 607 self.assertEqual(content.foo, u'123-456-7890') 608 self.assertEqual(content.bar, u'--') # default missing value 609 self.assertEqual(content.baz, u'--') 610 return
Note: See TracChangeset for help on using the changeset viewer.