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