source: main/waeup.kofa/trunk/src/waeup/kofa/university/vocabularies.py @ 8048

Last change on this file since 8048 was 7916, checked in by uli, 13 years ago

Clean up and avoid repetitive code.

  • Property svn:keywords set to Id
File size: 4.2 KB
RevLine 
[7195]1## $Id: vocabularies.py 7916 2012-03-19 05:23:01Z uli $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
[6091]18"""Vocabularies and sources for the academics section.
19"""
20from zc.sourcefactory.basic import BasicSourceFactory
[7681]21from zc.sourcefactory.contextual import BasicContextualSourceFactory
[6557]22from zope.catalog.interfaces import ICatalog
[7916]23from zope.component import getUtility, queryUtility
[7819]24from waeup.kofa.interfaces import SimpleKofaVocabulary, IKofaUtils
[7811]25from waeup.kofa.interfaces import MessageFactory as _
[7916]26from waeup.kofa.utils.utils import KofaUtils
[6091]27
[7819]28course_levels = SimpleKofaVocabulary(
[7681]29    (_('Pre-Studies'),10),
30    (_('100 (Year 1)'),100),
31    (_('200 (Year 2)'),200),
32    (_('300 (Year 3)'),300),
33    (_('400 (Year 4)'),400),
34    (_('500 (Year 5)'),500),
35    (_('600 (Year 6)'),600),
36    (_('700 (Year 7)'),700),
37    (_('800 (Year 8)'),800),
[6091]38    )
39
[7916]40#: An instance of :class:`waeup.kofa.utils.utils.KofaUtils` for fallback.
41KOFA_UTILS = KofaUtils()
[6091]42
[7916]43class ContextualDictSourceFactoryBase(BasicContextualSourceFactory):
44    """A base for contextual sources based on KofaUtils dicts.
[6091]45
[7916]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.
[7681]48    """
49    def getValues(self, context):
[7916]50        utils = queryUtility(IKofaUtils, default=KOFA_UTILS)
51        return sorted(getattr(utils, self.DICT_NAME).keys())
[7681]52
53    def getToken(self, context, value):
[7916]54        return str(value)
[7681]55
56    def getTitle(self, context, value):
[7916]57        utils = queryUtility(IKofaUtils, default=KOFA_UTILS)
58        return getattr(utils, self.DICT_NAME)[value]
[7681]59
[7916]60class SemesterSource(ContextualDictSourceFactoryBase):
61    """An institution type source delivers semester or term descriptors.
62    """
63    #: name of dict to deliver from kofa utils.
64    DICT_NAME = 'SEMESTER_DICT'
65
66class InstTypeSource(ContextualDictSourceFactoryBase):
67    """An institution type source delivers types of institutions
68    in the portal.
69    """
70    #: name of dict to deliver from kofa utils.
71    DICT_NAME = 'INST_TYPES_DICT'
72
73class AppCatSource(ContextualDictSourceFactoryBase):
[7681]74    """A application category source delivers all application categories
75    provided in the portal.
76    """
[7916]77    #: name of dict to deliver from kofa utils.
78    DICT_NAME = 'APP_CATS_DICT'
[7681]79
[7916]80class StudyModeSource(ContextualDictSourceFactoryBase):
[7681]81    """A study modes source delivers all study modes provided
82    in the portal.
83    """
[7916]84    #: name of dict to deliver from kofa utils.
85    DICT_NAME = 'STUDY_MODES_DICT'
[7681]86
[6091]87class CourseSource(BasicSourceFactory):
88    """A course source delivers all courses inside the portal by looking
89       up a catalog.
90    """
91    def getValues(self):
92        catalog = getUtility(ICatalog, name='courses_catalog')
[6092]93        return sorted(list(
94                catalog.searchResults(
[7916]95                    code=(None, None))),key=lambda value: value.code)
[6091]96
97    def getToken(self, value):
98        return value.code
99
100    def getTitle(self, value):
101        return "%s - %s" % (value.code, value.title[:64])
[7915]102
103class CertificateSource(BasicContextualSourceFactory):
104    """A certificate source delivers all certificates provided
105    in the portal.
106    """
107    def getValues(self, context):
108        catalog = getUtility(ICatalog, name='certificates_catalog')
109        return sorted(list(
[7916]110            catalog.searchResults(
111                code=(None, None))),
112                      key=lambda value: value.code)
[7915]113
114    def getToken(self, context, value):
115        return value.code
116
117    def getTitle(self, context, value):
118        return "%s - %s" % (value.code, value.title[:64])
Note: See TracBrowser for help on using the repository browser.