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

Last change on this file since 7916 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
Line 
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##
18"""Vocabularies and sources for the academics section.
19"""
20from zc.sourcefactory.basic import BasicSourceFactory
21from zc.sourcefactory.contextual import BasicContextualSourceFactory
22from zope.catalog.interfaces import ICatalog
23from zope.component import getUtility, queryUtility
24from waeup.kofa.interfaces import SimpleKofaVocabulary, IKofaUtils
25from waeup.kofa.interfaces import MessageFactory as _
26from waeup.kofa.utils.utils import KofaUtils
27
28course_levels = SimpleKofaVocabulary(
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),
38    )
39
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):
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):
74    """A application category source delivers all application categories
75    provided in the portal.
76    """
77    #: name of dict to deliver from kofa utils.
78    DICT_NAME = 'APP_CATS_DICT'
79
80class StudyModeSource(ContextualDictSourceFactoryBase):
81    """A study modes source delivers all study modes provided
82    in the portal.
83    """
84    #: name of dict to deliver from kofa utils.
85    DICT_NAME = 'STUDY_MODES_DICT'
86
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')
93        return sorted(list(
94                catalog.searchResults(
95                    code=(None, None))),key=lambda value: value.code)
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])
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(
110            catalog.searchResults(
111                code=(None, None))),
112                      key=lambda value: value.code)
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.