Ignore:
Timestamp:
14 Mar 2012, 03:19:19 (13 years ago)
Author:
uli
Message:

A (slightly) different setup to register phone prefixes in KofaUtils?.

Location:
main/waeup.kofa/trunk/src/waeup/kofa
Files:
3 edited

Legend:

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

    r7871 r7874  
    3838    return True
    3939
     40#: A list of phone prefixes (order num, country, prefix).
     41#: Items with same order num will be sorted alphabetically.
     42#: The lower the order num, the higher the precedence.
     43INT_PHONE_PREFIXES = [
     44    (99, _('Germany'), '49'),
     45    ( 1, _('Nigeria'), '234'),
     46    (99, _('U.S.'), '1'),
     47    ]
     48
     49def sorted_phone_prefixes(data = INT_PHONE_PREFIXES, request=None):
     50    """Sorted tuples of phone prefixes.
     51
     52    Ordered as shown above and formatted for use in select boxes.
     53
     54    If request is given, we'll try to translate all country names in
     55    order to sort alphabetically correctly.
     56
     57    XXX: This is a function (and not a constant) as different
     58    languages might give different orders. This is not tested yet.
     59
     60    XXX: If we really want to use alphabetic ordering here, we might
     61    think about caching results of translations.
     62    """
     63    if request is not None:
     64        data = [
     65            (x, translate(y, context=request), z)
     66            for x, y, z in data]
     67    return tuple([
     68        ('%s (+%s)' % (x[1],x[2]), '+%s' % x[2])
     69        for x in sorted(data)
     70        ])
     71
    4072class KofaUtils(grok.GlobalUtility):
    4173    """A collection of parameters and methods subject to customization.
     
    5789        }
    5890
    59     INT_PHONE_PREFIXES = {
    60             _('Germany'): (2, '49'),
    61             _('Nigeria'): (1, '234'),
    62             _('U.S.'): (3, '1'),
    63             }
     91    #: A function to return
     92    @classmethod
     93    def sorted_phone_prefixes(cls, data=INT_PHONE_PREFIXES, request=None):
     94        return sorted_phone_prefixes(data, request)
    6495
    6596    EXAM_SUBJECTS_DICT = {
  • main/waeup.kofa/trunk/src/waeup/kofa/widgets/phonewidget.py

    r7871 r7874  
    2121import grok
    2222import re
    23 from zope.component import getUtility
     23from zope.component import queryUtility
    2424from zope.formlib.interfaces import MissingInputError, InputErrors
    2525from zope.interface import Interface
    2626from zope.formlib.textwidgets import (
    2727    TextWidget, renderElement, ConversionError)
     28from waeup.kofa.interfaces import IKofaUtils
    2829from waeup.kofa.interfaces import MessageFactory as _
    29 
    30 class IInternationalPhonePrefixes(Interface):
    31     """A dict of international phone number prefixes.
    32     """
    33 
    34 class PhonePrefixes(grok.GlobalUtility):
    35     grok.implements(IInternationalPhonePrefixes)
    36 
    37     def title_value_list(self):
    38         try:
    39             from waeup.kofa.interfaces import IKofaUtils
    40             prefixes = getUtility(IKofaUtils).INT_PHONE_PREFIXES
    41             data = sorted(prefixes.items(), key=lambda value: value[1][0])
    42         except:
    43             data = [('Country of Nowhere',(1, '234'))]
    44         return [('%s (+%s)' % (x,y[1]), '+%s' % y[1]) for x,y in data]
     30from waeup.kofa.utils.utils import KofaUtils
    4531
    4632RE_INT_PREFIX = re.compile('^\+\d+')
     
    5339
    5440    def _renderPrefixWidget(self, value):
    55         prefixes = getUtility(
    56             IInternationalPhonePrefixes).title_value_list()
     41        prefix_func = getattr(
     42            queryUtility(IKofaUtils), 'sorted_phone_prefixes',
     43            KofaUtils.sorted_phone_prefixes)
    5744        options = []
    58         for ptitle, pval in prefixes:
     45        for ptitle, pval in prefix_func(request=self.request):
    5946            selected = ''
    6047            if value == pval:
  • main/waeup.kofa/trunk/src/waeup/kofa/widgets/tests/test_phonewidget.py

    r7855 r7874  
    3030from zope.publisher.browser import TestRequest
    3131from zope.schema.interfaces import ITextLine
    32 from waeup.kofa.widgets.phonewidget import PhoneWidget, PhonePrefixes
     32from waeup.kofa.widgets.phonewidget import PhoneWidget
    3333
    3434# Dummy content
     
    7474        (ITextLine, TextWidget),
    7575        ]
    76 
    77     def setUp(self):
    78         super(PhoneWidgetTests, self).setUp()
    79         # register the phone prefixes utility
    80         self.gsm = getGlobalSiteManager()
    81         self.reg_prefixes = PhonePrefixes()
    82         self.gsm.registerUtility(self.reg_prefixes)
    83         return
    84 
    85     def tearDown(self):
    86         self.gsm.unregisterUtility(self.reg_prefixes)
    87         return
    8876
    8977    def test_display_editform(self):
Note: See TracChangeset for help on using the changeset viewer.