1 | ## |
---|
2 | ## interfaces.py |
---|
3 | ## Login : <uli@pu.smp.net> |
---|
4 | ## Started on Sun Jan 16 15:30:01 2011 Uli Fouquet |
---|
5 | ## $Id$ |
---|
6 | ## |
---|
7 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
8 | ## This program is free software; you can redistribute it and/or modify |
---|
9 | ## it under the terms of the GNU General Public License as published by |
---|
10 | ## the Free Software Foundation; either version 2 of the License, or |
---|
11 | ## (at your option) any later version. |
---|
12 | ## |
---|
13 | ## This program is distributed in the hope that it will be useful, |
---|
14 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | ## GNU General Public License for more details. |
---|
17 | ## |
---|
18 | ## You should have received a copy of the GNU General Public License |
---|
19 | ## along with this program; if not, write to the Free Software |
---|
20 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
21 | ## |
---|
22 | """Interfaces regarding student applicants and related components. |
---|
23 | """ |
---|
24 | import os |
---|
25 | import waeup.sirp.browser |
---|
26 | |
---|
27 | from grokcore.content.interfaces import IContainer |
---|
28 | |
---|
29 | from zope import schema |
---|
30 | from zope.interface import Interface, Attribute, provider |
---|
31 | from zope.component import getUtility, getUtilitiesFor |
---|
32 | from zope.pluggableauth.interfaces import IPrincipalInfo |
---|
33 | from zope.security.interfaces import IGroupClosureAwarePrincipal as IPrincipal |
---|
34 | from zc.sourcefactory.basic import BasicSourceFactory |
---|
35 | from waeup.sirp.image.schema import ImageFile |
---|
36 | from waeup.sirp.image.image import WAeUPImageFile |
---|
37 | from waeup.sirp.interfaces import IWAeUPObject |
---|
38 | from waeup.sirp.university.vocabularies import application_categories |
---|
39 | from waeup.sirp.applicants.vocabularies import ( |
---|
40 | year_range, application_types_vocab, application_pins_vocab, |
---|
41 | lgas_vocab, CertificateSource, AppCatCertificateSource, |
---|
42 | GenderSource, entry_session_vocab |
---|
43 | ) |
---|
44 | |
---|
45 | IMAGE_PATH = os.path.join( |
---|
46 | os.path.dirname(waeup.sirp.browser.__file__), |
---|
47 | 'static' |
---|
48 | ) |
---|
49 | DEFAULT_PASSPORT_IMAGE_MALE = open( |
---|
50 | os.path.join(IMAGE_PATH, 'placeholder_m.jpg')).read() |
---|
51 | DEFAULT_PASSPORT_IMAGE_FEMALE = open( |
---|
52 | os.path.join(IMAGE_PATH, 'placeholder_f.jpg')).read() |
---|
53 | |
---|
54 | @provider(schema.interfaces.IContextAwareDefaultFactory) |
---|
55 | def default_passport_image(context): |
---|
56 | """A default value factory for ImageFile fields. |
---|
57 | |
---|
58 | Returns some default image as WAeUPImageFile. We cannot set the |
---|
59 | default directly in ImageFile fields, as, if we want to set |
---|
60 | max_size or min_size as well, some utility lookups are needed |
---|
61 | which are not possible during startup. |
---|
62 | |
---|
63 | Developers which use IContextAwareDefaultFactories like this one |
---|
64 | always should make sure that the delivered default meets all |
---|
65 | constraints of the field that makes use of this default value |
---|
66 | provider. |
---|
67 | """ |
---|
68 | return WAeUPImageFile( |
---|
69 | 'placeholder_m.jpg', DEFAULT_PASSPORT_IMAGE_MALE) |
---|
70 | |
---|
71 | class ApplicantContainerProviderSource(BasicSourceFactory): |
---|
72 | """A source offering all available applicants container types. |
---|
73 | |
---|
74 | The values returned by this source are names of utilities that can |
---|
75 | create :class:`ApplicantContainer` instances. So, if you get a |
---|
76 | name like ``'myactype'`` from this source, then you can do: |
---|
77 | |
---|
78 | >>> from zope.component import getUtility |
---|
79 | >>> p = getUtility(IApplicantsContainerProvider, name=myactype) |
---|
80 | >>> my_applicants_container = p.factory() |
---|
81 | |
---|
82 | Or you can access class-attributes like |
---|
83 | |
---|
84 | >>> my_applicants_container.container_title |
---|
85 | 'Pretty' |
---|
86 | |
---|
87 | """ |
---|
88 | def getValues(self): |
---|
89 | """Returns a list of ``(<name>, <provider>)`` tuples. |
---|
90 | |
---|
91 | Here ``<name>`` is the name under which an |
---|
92 | :class:``ApplicantContainerProvider`` was registered as a |
---|
93 | utility and ``<provider>`` is the utility itself. |
---|
94 | """ |
---|
95 | return getUtilitiesFor(IApplicantsContainerProvider) |
---|
96 | |
---|
97 | def getToken(self, value): |
---|
98 | """Return the name of the ``(<name>, <provider>)`` tuple. |
---|
99 | """ |
---|
100 | return value[0] |
---|
101 | |
---|
102 | def getTitle(self, value): |
---|
103 | """Get a 'title - description' string for a container type. |
---|
104 | """ |
---|
105 | factory = value[1].factory |
---|
106 | return "%s - %s" % ( |
---|
107 | factory.container_title, factory.container_description) |
---|
108 | |
---|
109 | class IResultEntry(IWAeUPObject): |
---|
110 | subject = schema.TextLine( |
---|
111 | title = u'Subject', |
---|
112 | description = u'The subject', |
---|
113 | required=False, |
---|
114 | ) |
---|
115 | score = schema.TextLine( |
---|
116 | title = u'Score', |
---|
117 | description = u'The score', |
---|
118 | required=False, |
---|
119 | ) |
---|
120 | |
---|
121 | class IApplicantsRoot(IWAeUPObject, IContainer): |
---|
122 | """A container for university applicants containers. |
---|
123 | """ |
---|
124 | pass |
---|
125 | |
---|
126 | class IApplicantsContainer(IWAeUPObject): |
---|
127 | """An applicants container contains university applicants. |
---|
128 | |
---|
129 | """ |
---|
130 | |
---|
131 | container_title = Attribute( |
---|
132 | u'classattribute: title for type of container') |
---|
133 | container_description = Attribute( |
---|
134 | u'classattribute: description for type of container') |
---|
135 | |
---|
136 | |
---|
137 | code = schema.TextLine( |
---|
138 | title = u'Code', |
---|
139 | default = u'-', |
---|
140 | required = True, |
---|
141 | readonly = True, |
---|
142 | ) |
---|
143 | |
---|
144 | title = schema.TextLine( |
---|
145 | title = u'Title', |
---|
146 | required = True, |
---|
147 | default = u'-', |
---|
148 | readonly = True, |
---|
149 | ) |
---|
150 | |
---|
151 | prefix = schema.Choice( |
---|
152 | title = u'Application target', |
---|
153 | required = True, |
---|
154 | default = None, |
---|
155 | source = application_types_vocab, |
---|
156 | readonly = True, |
---|
157 | ) |
---|
158 | |
---|
159 | year = schema.Choice( |
---|
160 | title = u'Year of entrance', |
---|
161 | required = True, |
---|
162 | default = None, |
---|
163 | values = year_range(), |
---|
164 | readonly = True, |
---|
165 | ) |
---|
166 | |
---|
167 | provider = schema.Choice( |
---|
168 | title = u'Applicants container type', |
---|
169 | required = True, |
---|
170 | default = None, |
---|
171 | source = ApplicantContainerProviderSource(), |
---|
172 | readonly = True, |
---|
173 | ) |
---|
174 | |
---|
175 | ac_prefix = schema.Choice( |
---|
176 | title = u'Access code prefix', |
---|
177 | required = True, |
---|
178 | default = None, |
---|
179 | source = application_pins_vocab, |
---|
180 | ) |
---|
181 | |
---|
182 | application_category = schema.Choice( |
---|
183 | title = u'Category for the grouping of study courses', |
---|
184 | required = True, |
---|
185 | default = None, |
---|
186 | source = application_categories, |
---|
187 | ) |
---|
188 | |
---|
189 | description = schema.Text( |
---|
190 | title = u'Human readable description in reST format', |
---|
191 | required = False, |
---|
192 | default = u'No description yet.' |
---|
193 | ) |
---|
194 | |
---|
195 | startdate = schema.Date( |
---|
196 | title = u'Date when the application period starts', |
---|
197 | required = False, |
---|
198 | default = None, |
---|
199 | ) |
---|
200 | |
---|
201 | enddate = schema.Date( |
---|
202 | title = u'Date when the application period ends', |
---|
203 | required = False, |
---|
204 | default = None, |
---|
205 | ) |
---|
206 | |
---|
207 | strict_deadline = schema.Bool( |
---|
208 | title = u'Forbid additions after deadline (enddate)', |
---|
209 | required = True, |
---|
210 | default = True, |
---|
211 | ) |
---|
212 | |
---|
213 | def archive(id=None): |
---|
214 | """Create on-dist archive of applicants stored in this term. |
---|
215 | |
---|
216 | If id is `None`, all applicants are archived. |
---|
217 | |
---|
218 | If id contains a single id string, only the respective |
---|
219 | applicants are archived. |
---|
220 | |
---|
221 | If id contains a list of id strings all of the respective |
---|
222 | applicants types are saved to disk. |
---|
223 | """ |
---|
224 | |
---|
225 | def clear(id=None, archive=True): |
---|
226 | """Remove applicants of type given by 'id'. |
---|
227 | |
---|
228 | Optionally archive the applicants. |
---|
229 | |
---|
230 | If id is `None`, all applicants are archived. |
---|
231 | |
---|
232 | If id contains a single id string, only the respective |
---|
233 | applicants are archived. |
---|
234 | |
---|
235 | If id contains a list of id strings all of the respective |
---|
236 | applicant types are saved to disk. |
---|
237 | |
---|
238 | If `archive` is ``False`` none of the archive-handling is done |
---|
239 | and respective applicants are simply removed from the |
---|
240 | database. |
---|
241 | """ |
---|
242 | |
---|
243 | class IApplicantsContainerAdd(IApplicantsContainer): |
---|
244 | """An applicants container contains university applicants. |
---|
245 | """ |
---|
246 | prefix = schema.Choice( |
---|
247 | title = u'Application target', |
---|
248 | required = True, |
---|
249 | default = None, |
---|
250 | source = application_types_vocab, |
---|
251 | readonly = False, |
---|
252 | ) |
---|
253 | |
---|
254 | year = schema.Choice( |
---|
255 | title = u'Year of entrance', |
---|
256 | required = True, |
---|
257 | default = None, |
---|
258 | values = year_range(), |
---|
259 | readonly = False, |
---|
260 | ) |
---|
261 | |
---|
262 | provider = schema.Choice( |
---|
263 | title = u'Applicants container type', |
---|
264 | required = True, |
---|
265 | default = None, |
---|
266 | source = ApplicantContainerProviderSource(), |
---|
267 | readonly = False, |
---|
268 | ) |
---|
269 | |
---|
270 | IApplicantsContainerAdd[ |
---|
271 | 'prefix'].order = IApplicantsContainer['prefix'].order |
---|
272 | IApplicantsContainerAdd[ |
---|
273 | 'year'].order = IApplicantsContainer['year'].order |
---|
274 | IApplicantsContainerAdd[ |
---|
275 | 'provider'].order = IApplicantsContainer['provider'].order |
---|
276 | |
---|
277 | class IApplicantBaseData(IWAeUPObject): |
---|
278 | """The data for an applicant. |
---|
279 | |
---|
280 | This is a base interface with no field (except ``reg_no``) |
---|
281 | required. For use with importers, forms, etc., please use one of |
---|
282 | the derived interfaces below, which set more fields to required |
---|
283 | state, depending on use-case. |
---|
284 | """ |
---|
285 | msgs = Attribute('Workflow transitions with date of transition.') |
---|
286 | state = Attribute('Returns the application state of an applicant') |
---|
287 | |
---|
288 | reg_no = schema.TextLine( |
---|
289 | title = u'JAMB Registration Number', |
---|
290 | readonly = True, |
---|
291 | ) |
---|
292 | access_code = schema.TextLine( |
---|
293 | title = u'Access Code', |
---|
294 | required = False, |
---|
295 | readonly = True, |
---|
296 | ) |
---|
297 | course1 = schema.Choice( |
---|
298 | title = u'1st Choice Course of Study', |
---|
299 | source = AppCatCertificateSource(), |
---|
300 | required = False, |
---|
301 | ) |
---|
302 | course2 = schema.Choice( |
---|
303 | title = u'2nd Choice Course of Study', |
---|
304 | source = AppCatCertificateSource(), |
---|
305 | required = False, |
---|
306 | ) |
---|
307 | firstname = schema.TextLine( |
---|
308 | title = u'First Name', |
---|
309 | required = False, |
---|
310 | ) |
---|
311 | middlenames = schema.TextLine( |
---|
312 | title = u'Middle Names', |
---|
313 | required = False, |
---|
314 | ) |
---|
315 | lastname = schema.TextLine( |
---|
316 | title = u'Last Name (Surname)', |
---|
317 | required = False, |
---|
318 | ) |
---|
319 | date_of_birth = schema.Date( |
---|
320 | title = u'Date of Birth', |
---|
321 | required = False, |
---|
322 | ) |
---|
323 | lga = schema.Choice( |
---|
324 | source = lgas_vocab, |
---|
325 | title = u'State/LGA', |
---|
326 | default = 'foreigner', |
---|
327 | required = True, |
---|
328 | ) |
---|
329 | sex = schema.Choice( |
---|
330 | title = u'Sex', |
---|
331 | source = GenderSource(), |
---|
332 | default = u'm', |
---|
333 | required = False, |
---|
334 | ) |
---|
335 | email = schema.TextLine( |
---|
336 | title = u'Email', |
---|
337 | required = False, |
---|
338 | ) |
---|
339 | phone = schema.TextLine( |
---|
340 | title = u'Phone', |
---|
341 | required = False, |
---|
342 | ) |
---|
343 | passport = ImageFile( |
---|
344 | title = u'Passport Photograph', |
---|
345 | #default = DEFAULT_PASSPORT_IMAGE_MALE, |
---|
346 | defaultFactory = default_passport_image, |
---|
347 | required = True, |
---|
348 | max_size = 20480, |
---|
349 | ) |
---|
350 | |
---|
351 | # |
---|
352 | # Process Data |
---|
353 | # |
---|
354 | application_date = schema.Date( |
---|
355 | title = u'Application Date', |
---|
356 | required = False, |
---|
357 | readonly = True, |
---|
358 | ) |
---|
359 | screening_score = schema.Int( |
---|
360 | title = u'Screening Score', |
---|
361 | required = False, |
---|
362 | ) |
---|
363 | screening_venue = schema.TextLine( |
---|
364 | title = u'Screening Venue', |
---|
365 | required = False, |
---|
366 | ) |
---|
367 | course_admitted = schema.Choice( |
---|
368 | title = u'Admitted Course of Study', |
---|
369 | source = CertificateSource(), |
---|
370 | default = None, |
---|
371 | required = False, |
---|
372 | ) |
---|
373 | entry_session = schema.Choice( |
---|
374 | source = entry_session_vocab, |
---|
375 | title = u'Entry Session', |
---|
376 | required = False, |
---|
377 | ) |
---|
378 | notice = schema.Text( |
---|
379 | title = u'Notice', |
---|
380 | required = False, |
---|
381 | ) |
---|
382 | student_id = schema.TextLine( |
---|
383 | title = u'Student ID', |
---|
384 | required = False, |
---|
385 | readonly = True, |
---|
386 | ) |
---|
387 | locked = schema.Bool( |
---|
388 | title = u'Form locked', |
---|
389 | default = False, |
---|
390 | ) |
---|
391 | |
---|
392 | class IApplicant(IApplicantBaseData): |
---|
393 | """An applicant. |
---|
394 | |
---|
395 | This is basically the applicant base data. Here we repeat the |
---|
396 | fields from base data if we have to set the `required` attribute |
---|
397 | to True (which is the default). |
---|
398 | """ |
---|
399 | |
---|
400 | class IApplicantEdit(IApplicantBaseData): |
---|
401 | """An applicant. |
---|
402 | |
---|
403 | Here we can repeat the fields from base data and set the `required` and |
---|
404 | `readonly` attributes to True to further restrict the data access. We cannot |
---|
405 | omit fields. This has to be done in the respective form page. |
---|
406 | """ |
---|
407 | screening_score = schema.Int( |
---|
408 | title = u'Screening Score', |
---|
409 | required = False, |
---|
410 | readonly = True, |
---|
411 | ) |
---|
412 | screening_venue = schema.TextLine( |
---|
413 | title = u'Screening Venue', |
---|
414 | required = False, |
---|
415 | readonly = True, |
---|
416 | ) |
---|
417 | course_admitted = schema.Choice( |
---|
418 | title = u'Admitted Course of Study', |
---|
419 | source = CertificateSource(), |
---|
420 | default = None, |
---|
421 | required = False, |
---|
422 | readonly = True, |
---|
423 | ) |
---|
424 | entry_session = schema.Choice( |
---|
425 | source = entry_session_vocab, |
---|
426 | title = u'Entry Session', |
---|
427 | required = False, |
---|
428 | readonly = True |
---|
429 | ) |
---|
430 | notice = schema.Text( |
---|
431 | title = u'Notice', |
---|
432 | required = False, |
---|
433 | readonly = True, |
---|
434 | ) |
---|
435 | |
---|
436 | class IApplicantPrincipalInfo(IPrincipalInfo): |
---|
437 | """Infos about principals that are applicants. |
---|
438 | """ |
---|
439 | access_code = Attribute("The Access Code the user purchased") |
---|
440 | |
---|
441 | class IApplicantPrincipal(IPrincipal): |
---|
442 | """A principal that is an applicant. |
---|
443 | |
---|
444 | This interface extends zope.security.interfaces.IPrincipal and |
---|
445 | requires also an `id` and other attributes defined there. |
---|
446 | """ |
---|
447 | access_code = schema.TextLine( |
---|
448 | title = u'Access Code', |
---|
449 | description = u'The access code purchased by the user.', |
---|
450 | required = True, |
---|
451 | readonly = True) |
---|
452 | |
---|
453 | class IApplicantsFormChallenger(Interface): |
---|
454 | """A challenger that uses a browser form to collect applicant |
---|
455 | credentials. |
---|
456 | """ |
---|
457 | loginpagename = schema.TextLine( |
---|
458 | title = u'Loginpagename', |
---|
459 | description = u"""Name of the login form used by challenger. |
---|
460 | |
---|
461 | The form must provide an ``access_code`` input field. |
---|
462 | """) |
---|
463 | |
---|
464 | accesscode_field = schema.TextLine( |
---|
465 | title = u'Access code field', |
---|
466 | description = u'''Field of the login page which is looked up for |
---|
467 | access_code''', |
---|
468 | default = u'access_code', |
---|
469 | ) |
---|
470 | |
---|
471 | |
---|
472 | class IApplicantSessionCredentials(Interface): |
---|
473 | """Interface for storing and accessing applicant credentials in a |
---|
474 | session. |
---|
475 | """ |
---|
476 | |
---|
477 | def __init__(access_code): |
---|
478 | """Create applicant session credentials.""" |
---|
479 | |
---|
480 | def getAccessCode(): |
---|
481 | """Return the access code.""" |
---|
482 | |
---|
483 | |
---|
484 | class IApplicantsContainerProvider(Interface): |
---|
485 | """A provider for applicants containers. |
---|
486 | |
---|
487 | Applicants container providers are meant to be looked up as |
---|
488 | utilities. This way we can find all applicant container types |
---|
489 | defined somewhere. |
---|
490 | |
---|
491 | Each applicants container provider registered as utility provides |
---|
492 | one container type and one should be able to call the `factory` |
---|
493 | attribute to create an instance of the requested container type. |
---|
494 | |
---|
495 | .. THE FOLLOWING SHOULD GO INTO SPHINX DOCS (and be tested) |
---|
496 | |
---|
497 | Samples |
---|
498 | ******* |
---|
499 | |
---|
500 | Given, you had an IApplicantsContainer implementation somewhere |
---|
501 | and you would like to make it findable on request, then you would |
---|
502 | normally create an appropriate provider utility like this:: |
---|
503 | |
---|
504 | import grok |
---|
505 | from waeup.sirp.applicants.interfaces import IApplicantsContainerProvider |
---|
506 | |
---|
507 | class MyContainerProvider(grok.GlobalUtility): |
---|
508 | grok.implements(IApplicantsContainerProvider) |
---|
509 | grok.name('MyContainerProvider') # Must be unique |
---|
510 | factory = MyContainer # A class implementing IApplicantsContainer |
---|
511 | # or derivations thereof. |
---|
512 | |
---|
513 | This utility would be registered on startup and could then be used |
---|
514 | like this: |
---|
515 | |
---|
516 | >>> from zope.component import getAllUtilitiesRegisteredFor |
---|
517 | >>> from waeup.sirp.applicants.interfaces import ( |
---|
518 | ... IApplicantsContainerProvider) |
---|
519 | >>> all_providers = getAllUtilitiesRegisteredFor( |
---|
520 | ... IApplicantsContainerProvider) |
---|
521 | >>> all_providers |
---|
522 | [<MyContainerProvider object at 0x...>] |
---|
523 | |
---|
524 | You could look up this specific provider by name: |
---|
525 | |
---|
526 | >>> from zope.component import getUtility |
---|
527 | >>> p = getUtility(IApplicantsContainerProvider, name='MyProvider') |
---|
528 | >>> p |
---|
529 | <MyContainerProvider object at 0x...> |
---|
530 | |
---|
531 | An applicants container would then be created like this: |
---|
532 | |
---|
533 | >>> provider = all_providers[0] |
---|
534 | >>> container = provider.factory() |
---|
535 | >>> container |
---|
536 | <MyContainer object at 0x...> |
---|
537 | |
---|
538 | """ |
---|
539 | factory = Attribute("A class that can create instances of the " |
---|
540 | "requested container type") |
---|