[7191] | 1 | ## $Id: vocabularies.py 7611 2012-02-08 17:44:44Z 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 | ## |
---|
[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 |
---|
[7321] | 27 | from waeup.sirp.interfaces import SimpleSIRPVocabulary |
---|
[6648] | 28 | from waeup.sirp.students.lgas import LGAS |
---|
[7523] | 29 | from waeup.sirp.students.nats import NATS |
---|
[6725] | 30 | from waeup.sirp.university.vocabularies import course_levels |
---|
[6648] | 31 | |
---|
[7321] | 32 | lgas_vocab = SimpleSIRPVocabulary( |
---|
[6648] | 33 | *sorted([(x[1],x[0]) for x in LGAS])) |
---|
| 34 | |
---|
[7523] | 35 | nats_vocab = SimpleSIRPVocabulary( |
---|
| 36 | *sorted([(x[1],x[0]) for x in NATS])) |
---|
| 37 | |
---|
[6725] | 38 | def study_levels(studycourse): |
---|
[7532] | 39 | if studycourse.certificate is not None: |
---|
[6725] | 40 | start_level = int(studycourse.certificate.start_level) |
---|
| 41 | end_level = int(studycourse.certificate.end_level) |
---|
[7532] | 42 | else: |
---|
[7601] | 43 | start_level = 10 |
---|
[7611] | 44 | end_level = 800 |
---|
[7532] | 45 | levels = [level for level in range(start_level,end_level+200,10) |
---|
| 46 | if level % 100 < 30 and level < end_level + 120] |
---|
| 47 | return levels |
---|
[6787] | 48 | |
---|
[7532] | 49 | |
---|
[6725] | 50 | class StudyLevelSource(BasicContextualSourceFactory): |
---|
| 51 | """The StudyLevelSource is based on and extends the |
---|
| 52 | course_levels vocabulary defined in the university package. |
---|
| 53 | Repeating study levels are denoted by increments of 10, the |
---|
| 54 | first spillover level by the certificate's end level plus 100 |
---|
| 55 | and the second spillover level by the end level plus 110. |
---|
| 56 | """ |
---|
| 57 | def getValues(self, context): |
---|
| 58 | return study_levels(context) |
---|
[6724] | 59 | |
---|
[6725] | 60 | def getToken(self, context, value): |
---|
| 61 | return str(value) |
---|
| 62 | |
---|
| 63 | def getTitle(self, context, value): |
---|
[7532] | 64 | if context.certificate is not None: |
---|
| 65 | end_level = int(context.certificate.end_level) |
---|
| 66 | else: |
---|
[7611] | 67 | end_level = 800 |
---|
[6725] | 68 | level,repeat = divmod(value, 100) |
---|
| 69 | level = level * 100 |
---|
| 70 | repeat = repeat//10 |
---|
| 71 | title = course_levels.by_value[level].title |
---|
| 72 | if level > end_level and repeat: |
---|
| 73 | title = course_levels.by_value[level-100].title |
---|
| 74 | title = "%s 2nd spillover" % title |
---|
| 75 | elif level > end_level: |
---|
| 76 | title = course_levels.by_value[level-100].title |
---|
| 77 | title = "%s spillover" % title |
---|
| 78 | elif repeat: |
---|
| 79 | title = "%s on %d. probation" % (title, repeat) |
---|
| 80 | return title |
---|
| 81 | |
---|
[7321] | 82 | verdicts = SimpleSIRPVocabulary( |
---|
[6804] | 83 | ('not yet','0'), |
---|
[6724] | 84 | ('Successful student','A'), |
---|
| 85 | ('Student with carryover courses','B'), |
---|
| 86 | ('Student on probation','C'), |
---|
[7611] | 87 | ('Withdrawn from the faculty','D'), |
---|
[6724] | 88 | ('Student who were previously on probation','E'), |
---|
| 89 | ('Medical case','F'), |
---|
| 90 | ('Absent from examination','G'), |
---|
| 91 | ('Withheld results','H'), |
---|
| 92 | ('Expelled/rusticated/suspended student','I'), |
---|
| 93 | ('Temporary withdrawn from the university','J'), |
---|
| 94 | ('Unregistered student','K'), |
---|
| 95 | ('Referred student','L'), |
---|
| 96 | ('Reinstatement','M'), |
---|
| 97 | ('Student on transfer','N'), |
---|
| 98 | ('NCE-III repeater','O'), |
---|
| 99 | ('New 300 level student','X'), |
---|
| 100 | ('No previous verdict','Y'), |
---|
| 101 | ('Successful student (provisional)','Z'), |
---|
| 102 | ('First Class','A1'), |
---|
| 103 | ('Second Class Upper','A2'), |
---|
| 104 | ('Second Class Lower','A3'), |
---|
| 105 | ('Third Class','A4'), |
---|
| 106 | ('Pass','A5'), |
---|
| 107 | ('Distinction','A6'), |
---|
| 108 | ('Credit','A7'), |
---|
| 109 | ('Merit','A8'), |
---|
| 110 | ) |
---|
| 111 | |
---|
| 112 | |
---|
[6648] | 113 | class CertificateSource(BasicContextualSourceFactory): |
---|
| 114 | """A certificate source delivers all certificates provided |
---|
| 115 | in the portal. |
---|
| 116 | """ |
---|
| 117 | def getValues(self, context): |
---|
| 118 | catalog = getUtility(ICatalog, name='certificates_catalog') |
---|
| 119 | return sorted(list( |
---|
| 120 | catalog.searchResults( |
---|
| 121 | code=('', 'z*'))), |
---|
| 122 | key=lambda value: value.code) |
---|
| 123 | |
---|
| 124 | def getToken(self, context, value): |
---|
| 125 | return value.code |
---|
| 126 | |
---|
| 127 | def getTitle(self, context, value): |
---|
| 128 | return "%s - %s" % (value.code, value.title[:64]) |
---|
| 129 | |
---|
| 130 | |
---|
| 131 | class GenderSource(BasicSourceFactory): |
---|
| 132 | """A gender source delivers basically a mapping |
---|
| 133 | ``{'m': 'Male', 'f': 'Female'}`` |
---|
| 134 | |
---|
| 135 | Using a source, we make sure that the tokens (which are |
---|
| 136 | stored/expected for instance from CSV files) are something one |
---|
| 137 | can expect and not cryptic IntIDs. |
---|
| 138 | """ |
---|
| 139 | def getValues(self): |
---|
| 140 | return ['m', 'f'] |
---|
| 141 | |
---|
| 142 | def getToken(self, value): |
---|
| 143 | return value[0].lower() |
---|
| 144 | |
---|
| 145 | def getTitle(self, value): |
---|
| 146 | if value == 'm': |
---|
| 147 | return 'Male' |
---|
| 148 | if value == 'f': |
---|
| 149 | return 'Female' |
---|
[6787] | 150 | |
---|
| 151 | class RegNumNotInSource(ValidationError): |
---|
| 152 | """Registration number exists already |
---|
| 153 | """ |
---|
| 154 | # The docstring of ValidationErrors is used as error description |
---|
| 155 | # by zope.formlib. |
---|
| 156 | pass |
---|
| 157 | |
---|
| 158 | class MatNumNotInSource(ValidationError): |
---|
| 159 | """Matriculation number exists already |
---|
| 160 | """ |
---|
| 161 | # The docstring of ValidationErrors is used as error description |
---|
| 162 | # by zope.formlib. |
---|
| 163 | pass |
---|
| 164 | |
---|
| 165 | class RegNumberSource(object): |
---|
| 166 | implements(ISource) |
---|
| 167 | cat_name = 'students_catalog' |
---|
| 168 | field_name = 'reg_number' |
---|
| 169 | validation_error = RegNumNotInSource |
---|
| 170 | def __init__(self, context): |
---|
| 171 | self.context = context |
---|
| 172 | return |
---|
| 173 | |
---|
| 174 | def __contains__(self, value): |
---|
| 175 | cat = queryUtility(ICatalog, self.cat_name) |
---|
| 176 | if cat is None: |
---|
| 177 | return True |
---|
| 178 | kw = {self.field_name: (value, value)} |
---|
| 179 | results = cat.searchResults(**kw) |
---|
| 180 | for entry in results: |
---|
| 181 | if entry.student_id != self.context.student_id: |
---|
| 182 | # XXX: sources should simply return False. |
---|
| 183 | # But then we get some stupid error message in forms |
---|
| 184 | # when validation fails. |
---|
| 185 | raise self.validation_error(value) |
---|
| 186 | #return False |
---|
| 187 | return True |
---|
| 188 | |
---|
| 189 | def contextual_reg_num_source(context): |
---|
| 190 | source = RegNumberSource(context) |
---|
| 191 | return source |
---|
| 192 | directlyProvides(contextual_reg_num_source, IContextSourceBinder) |
---|
| 193 | |
---|
| 194 | class MatNumberSource(RegNumberSource): |
---|
| 195 | field_name = 'matric_number' |
---|
| 196 | validation_error = MatNumNotInSource |
---|
| 197 | |
---|
| 198 | def contextual_mat_num_source(context): |
---|
| 199 | source = MatNumberSource(context) |
---|
| 200 | return source |
---|
| 201 | directlyProvides(contextual_mat_num_source, IContextSourceBinder) |
---|