Changeset 8175 for main/waeup.kofa/trunk


Ignore:
Timestamp:
16 Apr 2012, 09:16:13 (13 years ago)
Author:
uli
Message:

Add schema field converter for phone numbers.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/utils/converters.py

    r7932 r8175  
    3131    IObjectConverter, IResultEntryField, IFieldConverter, SubjectSource,
    3232    GradeSource)
     33from waeup.kofa.schema.interfaces import IPhoneNumber
    3334from waeup.kofa.schoolgrades import ResultEntry
    3435
     
    134135                unicode(num), unicode(item),
    135136                value_type, "%s.%s." % (prefix, num)))
     137        return result
     138
     139class PhoneNumberFieldConverter(grok.Adapter):
     140    """Convert strings into dict as expected from forms feeding PhoneWidget.
     141
     142    If you want strings without extra-checks imported, you can use
     143    schema.TextLine in your interface instead of PhoneNumber.
     144    """
     145    grok.context(IPhoneNumber)
     146    grok.provides(IFieldConverter)
     147
     148    def request_data(self, name, value, schema_field, prefix=''):
     149        parts = value.split('-', 2)
     150        country = ''
     151        area = ''
     152        ext = ''
     153        if len(parts) == 3:
     154            country = parts[0]
     155            area = parts[1]
     156            ext = parts[2]
     157        elif len(parts) == 2:
     158            country = parts[0]
     159            ext = parts[1]
     160        else:
     161            ext = value
     162        result = {
     163            u'%s.country' % prefix: country,
     164            u'%s.area' % prefix: area,
     165            u'%s.ext' % prefix: ext}
    136166        return result
    137167
Note: See TracChangeset for help on using the changeset viewer.