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

Last change on this file since 9931 was 8606, checked in by uli, 13 years ago

Start using the smarter contextual sources.

  • Property svn:keywords set to Id
File size: 4.2 KB
Line 
1## $Id: vocabularies.py 8606 2012-06-02 17:52:56Z 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 zope.catalog.interfaces import ICatalog
22from zope.component import getUtility, queryUtility
23from waeup.kofa.interfaces import SimpleKofaVocabulary, IKofaUtils
24from waeup.kofa.interfaces import MessageFactory as _
25from waeup.kofa.sourcefactory import SmartBasicContextualSourceFactory
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    (_('900 (Year 9)'),900),
39    (_('Postgraduate Level'),999),
40    )
41
42#: An instance of :class:`waeup.kofa.utils.utils.KofaUtils` for fallback.
43KOFA_UTILS = KofaUtils()
44
45class ContextualDictSourceFactoryBase(SmartBasicContextualSourceFactory):
46    """A base for contextual sources based on KofaUtils dicts.
47
48    To create a real source, you have to set the `DICT_NAME` attribute
49    which should be the name of a dictionary in KofaUtils.
50    """
51    def getValues(self, context):
52        utils = queryUtility(IKofaUtils, default=KOFA_UTILS)
53        return sorted(getattr(utils, self.DICT_NAME).keys())
54
55    def getToken(self, context, value):
56        return str(value)
57
58    def getTitle(self, context, value):
59        utils = queryUtility(IKofaUtils, default=KOFA_UTILS)
60        return getattr(utils, self.DICT_NAME)[value]
61
62class SemesterSource(ContextualDictSourceFactoryBase):
63    """An institution type source delivers semester or term descriptors.
64    """
65    #: name of dict to deliver from kofa utils.
66    DICT_NAME = 'SEMESTER_DICT'
67
68class InstTypeSource(ContextualDictSourceFactoryBase):
69    """An institution type source delivers types of institutions
70    in the portal.
71    """
72    #: name of dict to deliver from kofa utils.
73    DICT_NAME = 'INST_TYPES_DICT'
74
75class AppCatSource(ContextualDictSourceFactoryBase):
76    """A application category source delivers all application categories
77    provided in the portal.
78    """
79    #: name of dict to deliver from kofa utils.
80    DICT_NAME = 'APP_CATS_DICT'
81
82class StudyModeSource(ContextualDictSourceFactoryBase):
83    """A study modes source delivers all study modes provided
84    in the portal.
85    """
86    #: name of dict to deliver from kofa utils.
87    DICT_NAME = 'STUDY_MODES_DICT'
88
89class CourseSource(BasicSourceFactory):
90    """A course source delivers all courses inside the portal by looking
91       up a catalog.
92    """
93    def getValues(self):
94        catalog = getUtility(ICatalog, name='courses_catalog')
95        return sorted(list(
96                catalog.searchResults(
97                    code=(None, None))),key=lambda value: value.code)
98
99    def getToken(self, value):
100        return value.code
101
102    def getTitle(self, value):
103        return "%s - %s" % (value.code, value.title[:64])
104
105
106
107class CertificateSource(SmartBasicContextualSourceFactory):
108    """A certificate source delivers all certificates provided
109    in the portal.
110    """
111    def getValues(self, context):
112        catalog = getUtility(ICatalog, name='certificates_catalog')
113        return sorted(list(
114            catalog.searchResults(
115                code=(None, None))),
116                      key=lambda value: value.code)
117
118    def getToken(self, context, value):
119        return value.code
120
121    def getTitle(self, context, value):
122        return "%s - %s" % (value.code, value.title)
Note: See TracBrowser for help on using the repository browser.