1 | ## $Id: interfaces.py 8097 2012-04-10 19:52: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 | ## |
---|
18 | """Interfaces of the university application package. |
---|
19 | """ |
---|
20 | from grokcore.content.interfaces import IContainer |
---|
21 | from zc.sourcefactory.basic import BasicSourceFactory |
---|
22 | from zc.sourcefactory.contextual import BasicContextualSourceFactory |
---|
23 | from zope import schema |
---|
24 | from zope.component import getUtilitiesFor, queryUtility, getUtility |
---|
25 | from zope.catalog.interfaces import ICatalog |
---|
26 | from zope.interface import Interface, Attribute, implements, directlyProvides |
---|
27 | from zope.schema.interfaces import ( |
---|
28 | ValidationError, ISource, IContextSourceBinder) |
---|
29 | from waeup.kofa.schema import TextLineChoice |
---|
30 | from waeup.kofa.interfaces import ( |
---|
31 | IKofaObject, year_range, validate_email, academic_sessions_vocab, |
---|
32 | SimpleKofaVocabulary) |
---|
33 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
34 | from waeup.kofa.payments.interfaces import IOnlinePayment |
---|
35 | #from waeup.kofa.schoolgrades import ResultEntryField |
---|
36 | from waeup.kofa.students.vocabularies import GenderSource |
---|
37 | from waeup.kofa.university.vocabularies import ( |
---|
38 | course_levels, AppCatSource, CertificateSource) |
---|
39 | |
---|
40 | #: Maximum upload size for applicant passport photographs (in bytes) |
---|
41 | MAX_UPLOAD_SIZE = 1024 * 20 |
---|
42 | |
---|
43 | class RegNumInSource(ValidationError): |
---|
44 | """Registration number exists already |
---|
45 | """ |
---|
46 | # The docstring of ValidationErrors is used as error description |
---|
47 | # by zope.formlib. |
---|
48 | pass |
---|
49 | |
---|
50 | class RegNumberSource(object): |
---|
51 | implements(ISource) |
---|
52 | cat_name = 'applicants_catalog' |
---|
53 | field_name = 'reg_number' |
---|
54 | validation_error = RegNumInSource |
---|
55 | def __init__(self, context): |
---|
56 | self.context = context |
---|
57 | return |
---|
58 | |
---|
59 | def __contains__(self, value): |
---|
60 | cat = queryUtility(ICatalog, self.cat_name) |
---|
61 | if cat is None: |
---|
62 | return True |
---|
63 | kw = {self.field_name: (value, value)} |
---|
64 | results = cat.searchResults(**kw) |
---|
65 | for entry in results: |
---|
66 | if entry.applicant_id != self.context.applicant_id: |
---|
67 | # XXX: sources should simply return False. |
---|
68 | # But then we get some stupid error message in forms |
---|
69 | # when validation fails. |
---|
70 | raise self.validation_error(value) |
---|
71 | #return False |
---|
72 | return True |
---|
73 | |
---|
74 | def contextual_reg_num_source(context): |
---|
75 | source = RegNumberSource(context) |
---|
76 | return source |
---|
77 | directlyProvides(contextual_reg_num_source, IContextSourceBinder) |
---|
78 | |
---|
79 | |
---|
80 | class AppCatCertificateSource(CertificateSource): |
---|
81 | """An application certificate source delivers all courses which belong to |
---|
82 | a certain application_category. |
---|
83 | """ |
---|
84 | def getValues(self, context): |
---|
85 | # appliction category not available when certificate was deleted. |
---|
86 | # shouldn't that info be part of applicant info instead? |
---|
87 | # when we cannot determine the appcat, we will display all courses. |
---|
88 | appcat = getattr(getattr(context, '__parent__', None), |
---|
89 | 'application_category', None) |
---|
90 | catalog = getUtility(ICatalog, name='certificates_catalog') |
---|
91 | result = catalog.searchResults( |
---|
92 | application_category=(appcat,appcat)) |
---|
93 | result = sorted(result, key=lambda value: value.code) |
---|
94 | curr_course = context.course1 |
---|
95 | if curr_course is not None and curr_course not in result: |
---|
96 | # display also current course even if it is not catalogued |
---|
97 | # (any more) |
---|
98 | result = [curr_course,] + result |
---|
99 | return result |
---|
100 | |
---|
101 | class ApplicationTypeSource(BasicContextualSourceFactory): |
---|
102 | """An application type source delivers screening types defined in the |
---|
103 | portal. |
---|
104 | """ |
---|
105 | def getValues(self, context): |
---|
106 | appcats_dict = getUtility( |
---|
107 | IApplicantsUtils).APP_TYPES_DICT |
---|
108 | return sorted(appcats_dict.keys()) |
---|
109 | |
---|
110 | def getToken(self, context, value): |
---|
111 | return value |
---|
112 | |
---|
113 | def getTitle(self, context, value): |
---|
114 | appcats_dict = getUtility( |
---|
115 | IApplicantsUtils).APP_TYPES_DICT |
---|
116 | return appcats_dict[value][0] |
---|
117 | |
---|
118 | # Maybe Uniben still needs this ... |
---|
119 | #class ApplicationPinSource(BasicContextualSourceFactory): |
---|
120 | # """An application pin source delivers PIN prefixes for application |
---|
121 | # defined in the portal. |
---|
122 | # """ |
---|
123 | # def getValues(self, context): |
---|
124 | # apppins_dict = getUtility( |
---|
125 | # IApplicantsUtils).APP_TYPES_DICT |
---|
126 | # return sorted(appcats_dict.keys()) |
---|
127 | # |
---|
128 | # def getToken(self, context, value): |
---|
129 | # return value |
---|
130 | # |
---|
131 | # def getTitle(self, context, value): |
---|
132 | # apppins_dict = getUtility( |
---|
133 | # IApplicantsUtils).APP_TYPES_DICT |
---|
134 | # return u"%s (%s)" % ( |
---|
135 | # apppins_dict[value][1],self.apppins_dict[value][0]) |
---|
136 | |
---|
137 | application_modes_vocab = SimpleKofaVocabulary( |
---|
138 | (_('Create Application Records'), 'create'), |
---|
139 | (_('Update Application Records'), 'update'), |
---|
140 | ) |
---|
141 | |
---|
142 | class IApplicantsUtils(Interface): |
---|
143 | """A collection of methods which are subject to customization. |
---|
144 | """ |
---|
145 | |
---|
146 | APP_TYPES_DICT = Attribute(' dict of application types') |
---|
147 | |
---|
148 | class IApplicantsRoot(IKofaObject, IContainer): |
---|
149 | """A container for university applicants containers. |
---|
150 | """ |
---|
151 | pass |
---|
152 | |
---|
153 | class IApplicantsContainer(IKofaObject): |
---|
154 | """An applicants container contains university applicants. |
---|
155 | |
---|
156 | """ |
---|
157 | |
---|
158 | code = schema.TextLine( |
---|
159 | title = _(u'Code'), |
---|
160 | required = True, |
---|
161 | readonly = True, |
---|
162 | ) |
---|
163 | |
---|
164 | title = schema.TextLine( |
---|
165 | title = _(u'Title'), |
---|
166 | required = True, |
---|
167 | readonly = True, |
---|
168 | ) |
---|
169 | |
---|
170 | prefix = schema.Choice( |
---|
171 | title = _(u'Application Target'), |
---|
172 | required = True, |
---|
173 | source = ApplicationTypeSource(), |
---|
174 | readonly = True, |
---|
175 | ) |
---|
176 | |
---|
177 | year = schema.Choice( |
---|
178 | title = _(u'Year of Entrance'), |
---|
179 | required = True, |
---|
180 | values = year_range(), |
---|
181 | readonly = True, |
---|
182 | ) |
---|
183 | |
---|
184 | mode = schema.Choice( |
---|
185 | title = _(u'Application Mode'), |
---|
186 | vocabulary = application_modes_vocab, |
---|
187 | required = True, |
---|
188 | ) |
---|
189 | |
---|
190 | entry_level = schema.Choice( |
---|
191 | title = _(u'Entry Level'), |
---|
192 | vocabulary = course_levels, |
---|
193 | required = True, |
---|
194 | ) |
---|
195 | |
---|
196 | # Maybe Uniben still needs this ... |
---|
197 | #ac_prefix = schema.Choice( |
---|
198 | # title = u'Activation code prefix', |
---|
199 | # required = True, |
---|
200 | # default = None, |
---|
201 | # source = ApplicationPinSource(), |
---|
202 | # ) |
---|
203 | |
---|
204 | application_category = schema.Choice( |
---|
205 | title = _(u'Category for the grouping of certificates'), |
---|
206 | required = True, |
---|
207 | source = AppCatSource(), |
---|
208 | ) |
---|
209 | |
---|
210 | description = schema.Text( |
---|
211 | title = _(u'Human readable description in reST format'), |
---|
212 | required = False, |
---|
213 | default = u'''This text can been seen by anonymous users. |
---|
214 | Here we put multi-lingual information about the study courses provided, the application procedure and deadlines. |
---|
215 | >>de<< |
---|
216 | Dieser Text kann von anonymen Benutzern gelesen werden. |
---|
217 | Hier koennen mehrsprachige Informationen fuer Antragsteller hinterlegt werden.''' |
---|
218 | ) |
---|
219 | |
---|
220 | description_dict = Attribute( |
---|
221 | """Content as language dictionary with values in HTML format.""") |
---|
222 | |
---|
223 | startdate = schema.Date( |
---|
224 | title = _(u'Application Start Date'), |
---|
225 | required = False, |
---|
226 | ) |
---|
227 | |
---|
228 | enddate = schema.Date( |
---|
229 | title = _(u'Application Closing Date'), |
---|
230 | required = False, |
---|
231 | ) |
---|
232 | |
---|
233 | strict_deadline = schema.Bool( |
---|
234 | title = _(u'Forbid additions after deadline (enddate)'), |
---|
235 | required = False, |
---|
236 | default = True, |
---|
237 | ) |
---|
238 | |
---|
239 | def archive(id=None): |
---|
240 | """Create on-dist archive of applicants stored in this term. |
---|
241 | |
---|
242 | If id is `None`, all applicants are archived. |
---|
243 | |
---|
244 | If id contains a single id string, only the respective |
---|
245 | applicants are archived. |
---|
246 | |
---|
247 | If id contains a list of id strings all of the respective |
---|
248 | applicants types are saved to disk. |
---|
249 | """ |
---|
250 | |
---|
251 | def clear(id=None, archive=True): |
---|
252 | """Remove applicants of type given by 'id'. |
---|
253 | |
---|
254 | Optionally archive the applicants. |
---|
255 | |
---|
256 | If id is `None`, all applicants are archived. |
---|
257 | |
---|
258 | If id contains a single id string, only the respective |
---|
259 | applicants are archived. |
---|
260 | |
---|
261 | If id contains a list of id strings all of the respective |
---|
262 | applicant types are saved to disk. |
---|
263 | |
---|
264 | If `archive` is ``False`` none of the archive-handling is done |
---|
265 | and respective applicants are simply removed from the |
---|
266 | database. |
---|
267 | """ |
---|
268 | |
---|
269 | class IApplicantsContainerAdd(IApplicantsContainer): |
---|
270 | """An applicants container contains university applicants. |
---|
271 | """ |
---|
272 | prefix = schema.Choice( |
---|
273 | title = _(u'Application Target'), |
---|
274 | required = True, |
---|
275 | source = ApplicationTypeSource(), |
---|
276 | readonly = False, |
---|
277 | ) |
---|
278 | |
---|
279 | year = schema.Choice( |
---|
280 | title = _(u'Year of Entrance'), |
---|
281 | required = True, |
---|
282 | values = year_range(), |
---|
283 | readonly = False, |
---|
284 | ) |
---|
285 | |
---|
286 | IApplicantsContainerAdd[ |
---|
287 | 'prefix'].order = IApplicantsContainer['prefix'].order |
---|
288 | IApplicantsContainerAdd[ |
---|
289 | 'year'].order = IApplicantsContainer['year'].order |
---|
290 | |
---|
291 | class IApplicantBaseData(IKofaObject): |
---|
292 | """The data for an applicant. |
---|
293 | |
---|
294 | This is a base interface with no field |
---|
295 | required. For use with processors, forms, etc., please use one of |
---|
296 | the derived interfaces below, which set more fields to required |
---|
297 | state, depending on use-case. |
---|
298 | |
---|
299 | This base interface is also implemented by the |
---|
300 | :class:`waeup.kofa.students.StudentApplication` class in the |
---|
301 | students package. Thus, these are the data which are saved after |
---|
302 | admission. |
---|
303 | """ |
---|
304 | |
---|
305 | history = Attribute('Object history, a list of messages') |
---|
306 | state = Attribute('The application state of an applicant') |
---|
307 | display_fullname = Attribute('The fullname of an applicant') |
---|
308 | application_date = Attribute('Date of submission, used for export only') |
---|
309 | password = Attribute('Encrypted password of a applicant') |
---|
310 | application_number = Attribute('The key under which the record is stored') |
---|
311 | |
---|
312 | applicant_id = schema.TextLine( |
---|
313 | title = _(u'Applicant Id'), |
---|
314 | required = False, |
---|
315 | readonly = False, |
---|
316 | ) |
---|
317 | reg_number = TextLineChoice( |
---|
318 | title = _(u'Registration Number'), |
---|
319 | readonly = False, |
---|
320 | required = True, |
---|
321 | source = contextual_reg_num_source, |
---|
322 | ) |
---|
323 | #access_code = schema.TextLine( |
---|
324 | # title = u'Activation Code', |
---|
325 | # required = False, |
---|
326 | # readonly = True, |
---|
327 | # ) |
---|
328 | firstname = schema.TextLine( |
---|
329 | title = _(u'First Name'), |
---|
330 | required = True, |
---|
331 | ) |
---|
332 | middlename = schema.TextLine( |
---|
333 | title = _(u'Middle Name'), |
---|
334 | required = False, |
---|
335 | ) |
---|
336 | lastname = schema.TextLine( |
---|
337 | title = _(u'Last Name (Surname)'), |
---|
338 | required = True, |
---|
339 | ) |
---|
340 | date_of_birth = schema.Date( |
---|
341 | title = _(u'Date of Birth'), |
---|
342 | required = True, |
---|
343 | ) |
---|
344 | sex = schema.Choice( |
---|
345 | title = _(u'Sex'), |
---|
346 | source = GenderSource(), |
---|
347 | required = True, |
---|
348 | ) |
---|
349 | email = schema.ASCIILine( |
---|
350 | title = _(u'Email Address'), |
---|
351 | required = False, |
---|
352 | constraint=validate_email, |
---|
353 | ) |
---|
354 | phone = schema.TextLine( |
---|
355 | title = _(u'Phone'), |
---|
356 | description = u'', |
---|
357 | required = False, |
---|
358 | ) |
---|
359 | course1 = schema.Choice( |
---|
360 | title = _(u'1st Choice Course of Study'), |
---|
361 | source = CertificateSource(), |
---|
362 | required = True, |
---|
363 | ) |
---|
364 | course2 = schema.Choice( |
---|
365 | title = _(u'2nd Choice Course of Study'), |
---|
366 | source = CertificateSource(), |
---|
367 | required = False, |
---|
368 | ) |
---|
369 | #school_grades = schema.List( |
---|
370 | # title = _(u'School Grades'), |
---|
371 | # value_type = ResultEntryField(), |
---|
372 | # required = False, |
---|
373 | # default = [], |
---|
374 | # ) |
---|
375 | |
---|
376 | notice = schema.Text( |
---|
377 | title = _(u'Notice'), |
---|
378 | required = False, |
---|
379 | ) |
---|
380 | screening_venue = schema.TextLine( |
---|
381 | title = _(u'Screening Venue'), |
---|
382 | required = False, |
---|
383 | ) |
---|
384 | screening_score = schema.Int( |
---|
385 | title = _(u'Screening Score'), |
---|
386 | required = False, |
---|
387 | ) |
---|
388 | course_admitted = schema.Choice( |
---|
389 | title = _(u'Admitted Course of Study'), |
---|
390 | source = CertificateSource(), |
---|
391 | required = False, |
---|
392 | ) |
---|
393 | student_id = schema.TextLine( |
---|
394 | title = _(u'Student Id'), |
---|
395 | required = False, |
---|
396 | readonly = False, |
---|
397 | ) |
---|
398 | locked = schema.Bool( |
---|
399 | title = _(u'Form locked'), |
---|
400 | default = False, |
---|
401 | ) |
---|
402 | |
---|
403 | class IApplicant(IApplicantBaseData): |
---|
404 | """An applicant. |
---|
405 | |
---|
406 | This is basically the applicant base data. Here we repeat the |
---|
407 | fields from base data if we have to set the `required` attribute |
---|
408 | to True (which is the default). |
---|
409 | """ |
---|
410 | |
---|
411 | def loggerInfo(ob_class, comment): |
---|
412 | """Adds an INFO message to the log file |
---|
413 | """ |
---|
414 | |
---|
415 | def createStudent(): |
---|
416 | """Create a student object from applicatnt data |
---|
417 | and copy applicant object. |
---|
418 | """ |
---|
419 | |
---|
420 | class IApplicantEdit(IApplicant): |
---|
421 | """An applicant interface for editing. |
---|
422 | |
---|
423 | Here we can repeat the fields from base data and set the |
---|
424 | `required` and `readonly` attributes to True to further restrict |
---|
425 | the data access. Or we can allow only certain certificates to be |
---|
426 | selected by choosing the appropriate source. |
---|
427 | |
---|
428 | We cannot omit fields here. This has to be done in the |
---|
429 | respective form page. |
---|
430 | """ |
---|
431 | |
---|
432 | email = schema.ASCIILine( |
---|
433 | title = _(u'Email Address'), |
---|
434 | required = True, |
---|
435 | constraint=validate_email, |
---|
436 | ) |
---|
437 | course1 = schema.Choice( |
---|
438 | title = _(u'1st Choice Course of Study'), |
---|
439 | source = AppCatCertificateSource(), |
---|
440 | required = True, |
---|
441 | ) |
---|
442 | course2 = schema.Choice( |
---|
443 | title = _(u'2nd Choice Course of Study'), |
---|
444 | source = AppCatCertificateSource(), |
---|
445 | required = False, |
---|
446 | ) |
---|
447 | screening_score = schema.Int( |
---|
448 | title = _(u'Screening Score'), |
---|
449 | required = False, |
---|
450 | readonly = True, |
---|
451 | ) |
---|
452 | screening_venue = schema.TextLine( |
---|
453 | title = _(u'Screening Venue'), |
---|
454 | required = False, |
---|
455 | readonly = True, |
---|
456 | ) |
---|
457 | course_admitted = schema.Choice( |
---|
458 | title = _(u'Admitted Course of Study'), |
---|
459 | source = CertificateSource(), |
---|
460 | required = False, |
---|
461 | readonly = True, |
---|
462 | ) |
---|
463 | notice = schema.Text( |
---|
464 | title = _(u'Notice'), |
---|
465 | required = False, |
---|
466 | readonly = True, |
---|
467 | ) |
---|
468 | |
---|
469 | IApplicantEdit['email'].order = IApplicantEdit[ |
---|
470 | 'sex'].order |
---|
471 | |
---|
472 | class IApplicantUpdateByRegNo(IApplicant): |
---|
473 | """Representation of an applicant. |
---|
474 | |
---|
475 | Skip regular reg_number validation if reg_number is used for finding |
---|
476 | the applicant object. |
---|
477 | """ |
---|
478 | reg_number = schema.TextLine( |
---|
479 | title = u'Registration Number', |
---|
480 | required = False, |
---|
481 | ) |
---|
482 | |
---|
483 | class IApplicantRegisterUpdate(IApplicant): |
---|
484 | """Representation of an applicant for first-time registration. |
---|
485 | |
---|
486 | This interface is used when apllicants use the registration page to |
---|
487 | update their records. |
---|
488 | """ |
---|
489 | reg_number = schema.TextLine( |
---|
490 | title = u'Registration Number', |
---|
491 | required = True, |
---|
492 | ) |
---|
493 | |
---|
494 | firstname = schema.TextLine( |
---|
495 | title = _(u'First Name'), |
---|
496 | required = True, |
---|
497 | ) |
---|
498 | |
---|
499 | email = schema.ASCIILine( |
---|
500 | title = _(u'Email Address'), |
---|
501 | required = True, |
---|
502 | constraint=validate_email, |
---|
503 | ) |
---|
504 | |
---|
505 | class IApplicantOnlinePayment(IOnlinePayment): |
---|
506 | """An applicant payment via payment gateways. |
---|
507 | |
---|
508 | """ |
---|
509 | p_year = schema.Choice( |
---|
510 | title = _(u'Payment Session'), |
---|
511 | source = academic_sessions_vocab, |
---|
512 | required = False, |
---|
513 | ) |
---|
514 | |
---|
515 | IApplicantOnlinePayment['p_year'].order = IApplicantOnlinePayment[ |
---|
516 | 'p_year'].order |
---|
517 | |
---|