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

Last change on this file since 7839 was 7838, checked in by Henrik Bettermann, 13 years ago

Uli, what's better: catching exceptions or using BasicContextualSourceFactory? instead of BasicSourceFactory?? This revision is meant to demonstrate the two alternative ways constructing sources. The first one does always work.

  • Property svn:keywords set to Id
File size: 4.2 KB
RevLine 
[7195]1## $Id: vocabularies.py 7838 2012-03-11 16:03:54Z henrik $
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
[6091]23from zope.component import getUtility
[7819]24from waeup.kofa.interfaces import SimpleKofaVocabulary, IKofaUtils
[7811]25from waeup.kofa.interfaces import MessageFactory as _
[6091]26
[7819]27course_levels = SimpleKofaVocabulary(
[7681]28    (_('Pre-Studies'),10),
29    (_('100 (Year 1)'),100),
30    (_('200 (Year 2)'),200),
31    (_('300 (Year 3)'),300),
32    (_('400 (Year 4)'),400),
33    (_('500 (Year 5)'),500),
34    (_('600 (Year 6)'),600),
35    (_('700 (Year 7)'),700),
36    (_('800 (Year 8)'),800),
[6091]37    )
38
[7838]39class SemesterSource(BasicSourceFactory):
[7681]40    """An institution type source delivers semester or term descriptors.
41    """
[7838]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).getSemesterDict().keys()
49        except:
50            return [9]
51       
52    def getToken(self, value):
[7681]53        return str(value)
[6091]54
[7838]55    def getTitle(self, value):
56        try:
57            from waeup.kofa.interfaces import IKofaUtils
58            semesters_dict = getUtility(IKofaUtils).getSemesterDict()
59            return semesters_dict[value]
60        except:
61            value
[6091]62
[7681]63class InstTypeSource(BasicContextualSourceFactory):
64    """An institution type source delivers types of institutions
65    in the portal.
66    """
67    def getValues(self, context):
[7819]68        insttypes_dict = getUtility(IKofaUtils).getInstTypeDict()
[7688]69        return sorted(insttypes_dict.keys())
[7681]70
71    def getToken(self, context, value):
72        return value
73
74    def getTitle(self, context, value):
[7819]75        insttypes_dict = getUtility(IKofaUtils).getInstTypeDict()
[7688]76        return insttypes_dict[value]
[7681]77
78class AppCatSource(BasicContextualSourceFactory):
79    """A application category source delivers all application categories
80    provided in the portal.
81    """
82    def getValues(self, context):
[7819]83        appcats_dict = getUtility(IKofaUtils).getAppCatDict()
[7688]84        return sorted(appcats_dict.keys())
[7681]85
86    def getToken(self, context, value):
87        return value
88
89    def getTitle(self, context, value):
[7819]90        appcats_dict = getUtility(IKofaUtils).getAppCatDict()
[7688]91        return appcats_dict[value]
[7681]92
93class StudyModeSource(BasicContextualSourceFactory):
94    """A study modes source delivers all study modes provided
95    in the portal.
96    """
97    def getValues(self, context):
[7819]98        studymodes_dict = getUtility(IKofaUtils).getStudyModesDict()
[7688]99        return sorted(studymodes_dict.keys())
[7681]100
101    def getToken(self, context, value):
102        return value
103
104    def getTitle(self, context, value):
[7819]105        studymodes_dict = getUtility(IKofaUtils).getStudyModesDict()
[7688]106        return studymodes_dict[value]
[7681]107
[6091]108class CourseSource(BasicSourceFactory):
109    """A course source delivers all courses inside the portal by looking
110       up a catalog.
111    """
112    def getValues(self):
113        catalog = getUtility(ICatalog, name='courses_catalog')
[6092]114        return sorted(list(
115                catalog.searchResults(
116                    code=('', 'z*'))),key=lambda value: value.code)
[6091]117
118    def getToken(self, value):
119        return value.code
120
121    def getTitle(self, value):
122        return "%s - %s" % (value.code, value.title[:64])
Note: See TracBrowser for help on using the repository browser.