[7191] | 1 | ## $Id: vocabularies.py 9169 2012-09-10 11:05:07Z 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 | ## |
---|
[6648] | 18 | """Vocabularies and sources for the student section. |
---|
| 19 | """ |
---|
[6787] | 20 | from zope.component import getUtility, queryUtility |
---|
[6648] | 21 | from zope.catalog.interfaces import ICatalog |
---|
[6787] | 22 | from zope.interface import implements, directlyProvides |
---|
| 23 | from zope.schema.interfaces import ISource, IContextSourceBinder |
---|
| 24 | from zope.schema.interfaces import ValidationError |
---|
[6648] | 25 | from zc.sourcefactory.basic import BasicSourceFactory |
---|
| 26 | from zc.sourcefactory.contextual import BasicContextualSourceFactory |
---|
[7819] | 27 | from waeup.kofa.interfaces import SimpleKofaVocabulary |
---|
[7811] | 28 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
[8069] | 29 | from waeup.kofa.utils.helpers import get_sorted_preferred |
---|
| 30 | from waeup.kofa.utils.countries import COUNTRIES |
---|
[7811] | 31 | from waeup.kofa.university.vocabularies import course_levels |
---|
[6648] | 32 | |
---|
| 33 | |
---|
[8069] | 34 | #: a tuple of tuples (<COUNTRY-NAME>, <ISO-CODE>) with Nigeria first. |
---|
| 35 | COUNTRIES = get_sorted_preferred(COUNTRIES, ['NG']) |
---|
| 36 | nats_vocab = SimpleKofaVocabulary(*COUNTRIES) |
---|
[7523] | 37 | |
---|
[9169] | 38 | def study_levels(context): |
---|
| 39 | certificate = getattr(context, 'certificate', None) |
---|
| 40 | if certificate is not None: |
---|
| 41 | start_level = int(certificate.start_level) |
---|
| 42 | end_level = int(certificate.end_level) |
---|
[8471] | 43 | if start_level == 999 or end_level == 999: |
---|
| 44 | levels = [999] |
---|
| 45 | elif start_level == 10: |
---|
[7625] | 46 | levels = [10,] + [level for level in range(100,end_level+200,10) |
---|
| 47 | if level % 100 < 30] |
---|
| 48 | else: |
---|
| 49 | levels = [level for level in range(start_level,end_level+200,10) |
---|
| 50 | if level % 100 < 30] |
---|
[7532] | 51 | else: |
---|
[7625] | 52 | # default level range |
---|
[9169] | 53 | levels = [10, ] |
---|
| 54 | levels += [level for level in range(100,1000,10) if level % 100 < 30] |
---|
| 55 | levels.append(999) |
---|
[7532] | 56 | return levels |
---|
[6787] | 57 | |
---|
[7532] | 58 | |
---|
[6725] | 59 | class StudyLevelSource(BasicContextualSourceFactory): |
---|
| 60 | """The StudyLevelSource is based on and extends the |
---|
| 61 | course_levels vocabulary defined in the university package. |
---|
| 62 | Repeating study levels are denoted by increments of 10, the |
---|
| 63 | first spillover level by the certificate's end level plus 100 |
---|
| 64 | and the second spillover level by the end level plus 110. |
---|
| 65 | """ |
---|
| 66 | def getValues(self, context): |
---|
| 67 | return study_levels(context) |
---|
[6724] | 68 | |
---|
[6725] | 69 | def getToken(self, context, value): |
---|
| 70 | return str(value) |
---|
| 71 | |
---|
| 72 | def getTitle(self, context, value): |
---|
[9169] | 73 | certificate = getattr(context, 'certificate', None) |
---|
| 74 | if certificate is not None: |
---|
| 75 | start_level = int(certificate.start_level) |
---|
| 76 | end_level = int(certificate.end_level) |
---|
[7532] | 77 | else: |
---|
[7625] | 78 | # default level range |
---|
[9169] | 79 | start_level = 10 |
---|
[8098] | 80 | end_level = 1000 |
---|
[8471] | 81 | if start_level == 999 or end_level == 999: |
---|
| 82 | if value != 999: |
---|
| 83 | return _('Error: wrong level id ${value}', |
---|
| 84 | mapping={'value': value}) |
---|
[9169] | 85 | if value == 999: |
---|
[8471] | 86 | return course_levels.by_value[999].title |
---|
[7625] | 87 | if value < start_level or value > end_level + 120: |
---|
[7691] | 88 | return _('Error: level id ${value} out of range', |
---|
| 89 | mapping={'value': value}) |
---|
[7625] | 90 | # Special treatment for pre-studies level |
---|
[7617] | 91 | if value == 10: |
---|
| 92 | return course_levels.by_value[value].title |
---|
[6725] | 93 | level,repeat = divmod(value, 100) |
---|
| 94 | level = level * 100 |
---|
| 95 | repeat = repeat//10 |
---|
| 96 | title = course_levels.by_value[level].title |
---|
[7625] | 97 | if level > end_level and repeat == 1: |
---|
[7617] | 98 | title = course_levels.by_value[level - 100].title |
---|
[7691] | 99 | return _('${title} 2nd spillover', mapping={'title': title}) |
---|
[7625] | 100 | if level > end_level and repeat == 2: |
---|
| 101 | title = course_levels.by_value[level - 100].title |
---|
[7691] | 102 | return _('${title} 3rd spillover', mapping={'title': title}) |
---|
[7617] | 103 | if level > end_level: |
---|
| 104 | title = course_levels.by_value[level - 100].title |
---|
[7691] | 105 | return _('${title} 1st spillover', mapping={'title': title}) |
---|
[7625] | 106 | if repeat == 1: |
---|
[7691] | 107 | return _('${title} on 1st probation', mapping={'title': title}) |
---|
[7625] | 108 | if repeat == 2: |
---|
[7691] | 109 | return _('${title} on 2nd probation', mapping={'title': title}) |
---|
[6725] | 110 | return title |
---|
| 111 | |
---|
[6648] | 112 | class GenderSource(BasicSourceFactory): |
---|
| 113 | """A gender source delivers basically a mapping |
---|
| 114 | ``{'m': 'Male', 'f': 'Female'}`` |
---|
| 115 | |
---|
| 116 | Using a source, we make sure that the tokens (which are |
---|
| 117 | stored/expected for instance from CSV files) are something one |
---|
| 118 | can expect and not cryptic IntIDs. |
---|
| 119 | """ |
---|
| 120 | def getValues(self): |
---|
| 121 | return ['m', 'f'] |
---|
| 122 | |
---|
| 123 | def getToken(self, value): |
---|
| 124 | return value[0].lower() |
---|
| 125 | |
---|
| 126 | def getTitle(self, value): |
---|
| 127 | if value == 'm': |
---|
[7872] | 128 | return _('male') |
---|
[6648] | 129 | if value == 'f': |
---|
[7872] | 130 | return _('female') |
---|
[6787] | 131 | |
---|
| 132 | class RegNumNotInSource(ValidationError): |
---|
| 133 | """Registration number exists already |
---|
| 134 | """ |
---|
| 135 | # The docstring of ValidationErrors is used as error description |
---|
| 136 | # by zope.formlib. |
---|
| 137 | pass |
---|
| 138 | |
---|
| 139 | class MatNumNotInSource(ValidationError): |
---|
| 140 | """Matriculation number exists already |
---|
| 141 | """ |
---|
| 142 | # The docstring of ValidationErrors is used as error description |
---|
| 143 | # by zope.formlib. |
---|
| 144 | pass |
---|
| 145 | |
---|
| 146 | class RegNumberSource(object): |
---|
[8766] | 147 | """A source that accepts any entry for a certain field if not used |
---|
| 148 | already. |
---|
| 149 | |
---|
| 150 | Using this kind of source means a way of setting an invariant. |
---|
| 151 | |
---|
| 152 | We accept a value iff: |
---|
| 153 | - the value cannot be found in catalog or |
---|
| 154 | - the value can be found as part of some item but the bound item |
---|
| 155 | is the context object itself. |
---|
| 156 | """ |
---|
[6787] | 157 | implements(ISource) |
---|
| 158 | cat_name = 'students_catalog' |
---|
| 159 | field_name = 'reg_number' |
---|
| 160 | validation_error = RegNumNotInSource |
---|
[8766] | 161 | comp_field = 'student_id' |
---|
[6787] | 162 | def __init__(self, context): |
---|
| 163 | self.context = context |
---|
| 164 | return |
---|
| 165 | |
---|
| 166 | def __contains__(self, value): |
---|
[8766] | 167 | """We accept all values not already given to other students. |
---|
| 168 | """ |
---|
[6787] | 169 | cat = queryUtility(ICatalog, self.cat_name) |
---|
| 170 | if cat is None: |
---|
| 171 | return True |
---|
| 172 | kw = {self.field_name: (value, value)} |
---|
| 173 | results = cat.searchResults(**kw) |
---|
| 174 | for entry in results: |
---|
[8766] | 175 | if not hasattr(self.context, self.comp_field): |
---|
| 176 | # we have no context with comp_field (most probably |
---|
| 177 | # while adding a new object, where the container is |
---|
| 178 | # the context) which means that the value was given |
---|
| 179 | # already to another object (as _something_ was found in |
---|
| 180 | # the catalog with that value). Fail on first round. |
---|
[6787] | 181 | raise self.validation_error(value) |
---|
[8766] | 182 | if getattr(entry, self.comp_field) != getattr( |
---|
| 183 | self.context, self.comp_field): |
---|
| 184 | # An entry already given to another student is not in our |
---|
| 185 | # range of acceptable values. |
---|
| 186 | raise self.validation_error(value) |
---|
[6787] | 187 | #return False |
---|
| 188 | return True |
---|
| 189 | |
---|
| 190 | def contextual_reg_num_source(context): |
---|
| 191 | source = RegNumberSource(context) |
---|
| 192 | return source |
---|
| 193 | directlyProvides(contextual_reg_num_source, IContextSourceBinder) |
---|
| 194 | |
---|
| 195 | class MatNumberSource(RegNumberSource): |
---|
| 196 | field_name = 'matric_number' |
---|
| 197 | validation_error = MatNumNotInSource |
---|
| 198 | |
---|
| 199 | def contextual_mat_num_source(context): |
---|
| 200 | source = MatNumberSource(context) |
---|
| 201 | return source |
---|
| 202 | directlyProvides(contextual_mat_num_source, IContextSourceBinder) |
---|