Ignore:
Timestamp:
15 Dec 2011, 12:19:43 (13 years ago)
Author:
uli
Message:

Make the phonewidget handling non-standardized input more gracefully.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/widgets/phonewidget.py

    r7342 r7352  
    1616zope.formlib or newer form libs). Therefore some modifications were
    1717neccessary and it might look a bit overloaded.
     18
     19If you use the PhoneWidget for rendering regular TextLine attributes
     20(preferably in edit forms or add forms), the phone number is displayed
     21by three input fields representing the international code, the area
     22code and the extension line. All three fields require pure numbers as
     23input and do not accept other chars.
     24
     25When the entered input is stored with a context object, it is stored
     26as a single unicode string with the numbers divided by single hyphen.
     27
     28So, input <12>, <111>, <444> becomes the string ``'12-111-444'`` for
     29the context object.
     30
    1831"""
    1932import re
     
    6275
    6376    def __init__(self, context, number=None):
     77        """Set countrycode, areacode, and extension line from number.
     78        """
    6479        self.context = context
    65         if number is not None:
    66             # XXX: We have to provide something more tolerant
    67             self.country, self.area, self.extension = number.split('-')
     80        if number is None:
    6881            return
    69             try:
    70                 self.country, self.area, self.extension = number.split('-')
    71             except ValueError:
    72                 replacement = '1-1-%s' % (number.replace('-', ''),)
    73                 self.country, self.area, self.extension = replacement.split(
    74                     '-', 2)
     82        # handle other types than strings.
     83        if not isinstance(number, basestring):
     84            number = str(number)
     85        parts = number.split('-', 2)
     86        if len(parts) == 2:
     87            parts = [None,] + parts
     88        elif len(parts) == 0:
     89            return
     90        elif len(parts) == 1:
     91            parts = [None, None] + parts
     92        self.country, self.area, self.extension = parts
     93        return
    7594
    7695    @property
    7796    def number(self):
    78         return u'-'.join((self.country, self.area, self.extension))
     97        """Return a valid phone number string of format ``<IC>-<AC>-<EL>``.
     98
     99        where <IC> is the country code (digits only), <AC> is the area
     100        code and <EL> is the extension line.
     101        """
     102        return u'-'.join(
     103            (self.country or '', self.area or '', self.extension))
    79104
    80105
Note: See TracChangeset for help on using the changeset viewer.