Changeset 7916 for main


Ignore:
Timestamp:
19 Mar 2012, 05:23:01 (13 years ago)
Author:
uli
Message:

Clean up and avoid repetitive code.

File:
1 edited

Legend:

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

    r7915 r7916  
    2121from zc.sourcefactory.contextual import BasicContextualSourceFactory
    2222from zope.catalog.interfaces import ICatalog
    23 from zope.component import getUtility
     23from zope.component import getUtility, queryUtility
    2424from waeup.kofa.interfaces import SimpleKofaVocabulary, IKofaUtils
    2525from waeup.kofa.interfaces import MessageFactory as _
     26from waeup.kofa.utils.utils import KofaUtils
    2627
    2728course_levels = SimpleKofaVocabulary(
     
    3738    )
    3839
    39 class SemesterSource(BasicSourceFactory):
     40#: An instance of :class:`waeup.kofa.utils.utils.KofaUtils` for fallback.
     41KOFA_UTILS = KofaUtils()
     42
     43class ContextualDictSourceFactoryBase(BasicContextualSourceFactory):
     44    """A base for contextual sources based on KofaUtils dicts.
     45
     46    To create a real source, you have to set the `DICT_NAME` attribute
     47    which should be the name of a dictionary in KofaUtils.
     48    """
     49    def getValues(self, context):
     50        utils = queryUtility(IKofaUtils, default=KOFA_UTILS)
     51        return sorted(getattr(utils, self.DICT_NAME).keys())
     52
     53    def getToken(self, context, value):
     54        return str(value)
     55
     56    def getTitle(self, context, value):
     57        utils = queryUtility(IKofaUtils, default=KOFA_UTILS)
     58        return getattr(utils, self.DICT_NAME)[value]
     59
     60class SemesterSource(ContextualDictSourceFactoryBase):
    4061    """An institution type source delivers semester or term descriptors.
    4162    """
    42     def getValues(self):
    43         try:
    44             # We have to 'try', don't know why (henrik)
    45             # Alternatively, we can use BasicContextualSourceFactory,
    46             # see sources below
    47             from waeup.kofa.interfaces import IKofaUtils
    48             return getUtility(IKofaUtils).SEMESTER_DICT.keys()
    49         except:
    50             return [9]
    51        
    52     def getToken(self, value):
    53         return str(value)
     63    #: name of dict to deliver from kofa utils.
     64    DICT_NAME = 'SEMESTER_DICT'
    5465
    55     def getTitle(self, value):
    56         try:
    57             from waeup.kofa.interfaces import IKofaUtils
    58             semesters_dict = getUtility(IKofaUtils).SEMESTER_DICT
    59             return semesters_dict[value]
    60         except:
    61             value
    62 
    63 class InstTypeSource(BasicContextualSourceFactory):
     66class InstTypeSource(ContextualDictSourceFactoryBase):
    6467    """An institution type source delivers types of institutions
    6568    in the portal.
    6669    """
    67     def getValues(self, context):
    68         insttypes_dict = getUtility(IKofaUtils).INST_TYPES_DICT
    69         return sorted(insttypes_dict.keys())
     70    #: name of dict to deliver from kofa utils.
     71    DICT_NAME = 'INST_TYPES_DICT'
    7072
    71     def getToken(self, context, value):
    72         return value
    73 
    74     def getTitle(self, context, value):
    75         insttypes_dict = getUtility(IKofaUtils).INST_TYPES_DICT
    76         return insttypes_dict[value]
    77 
    78 class AppCatSource(BasicContextualSourceFactory):
     73class AppCatSource(ContextualDictSourceFactoryBase):
    7974    """A application category source delivers all application categories
    8075    provided in the portal.
    8176    """
    82     def getValues(self, context):
    83         appcats_dict = getUtility(IKofaUtils).APP_CATS_DICT
    84         return sorted(appcats_dict.keys())
     77    #: name of dict to deliver from kofa utils.
     78    DICT_NAME = 'APP_CATS_DICT'
    8579
    86     def getToken(self, context, value):
    87         return value
    88 
    89     def getTitle(self, context, value):
    90         appcats_dict = getUtility(IKofaUtils).APP_CATS_DICT
    91         return appcats_dict[value]
    92 
    93 class StudyModeSource(BasicContextualSourceFactory):
     80class StudyModeSource(ContextualDictSourceFactoryBase):
    9481    """A study modes source delivers all study modes provided
    9582    in the portal.
    9683    """
    97     def getValues(self, context):
    98         studymodes_dict = getUtility(IKofaUtils).STUDY_MODES_DICT
    99         return sorted(studymodes_dict.keys())
    100 
    101     def getToken(self, context, value):
    102         return value
    103 
    104     def getTitle(self, context, value):
    105         studymodes_dict = getUtility(IKofaUtils).STUDY_MODES_DICT
    106         return studymodes_dict[value]
     84    #: name of dict to deliver from kofa utils.
     85    DICT_NAME = 'STUDY_MODES_DICT'
    10786
    10887class CourseSource(BasicSourceFactory):
     
    11493        return sorted(list(
    11594                catalog.searchResults(
    116                     code=('', 'z*'))),key=lambda value: value.code)
     95                    code=(None, None))),key=lambda value: value.code)
    11796
    11897    def getToken(self, value):
     
    129108        catalog = getUtility(ICatalog, name='certificates_catalog')
    130109        return sorted(list(
    131                 catalog.searchResults(
    132                     code=('', 'z*'))),
    133                     key=lambda value: value.code)
     110            catalog.searchResults(
     111                code=(None, None))),
     112                      key=lambda value: value.code)
    134113
    135114    def getToken(self, context, value):
Note: See TracChangeset for help on using the changeset viewer.