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