1 | ## |
---|
2 | ## applicants.py |
---|
3 | ## Login : <uli@pu.smp.net> |
---|
4 | ## Started on Fri Jul 16 11:46:55 2010 Uli Fouquet |
---|
5 | ## $Id$ |
---|
6 | ## |
---|
7 | ## Copyright (C) 2010 Uli Fouquet |
---|
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 | import grok |
---|
23 | from grok import index |
---|
24 | from zope.component.interfaces import IFactory |
---|
25 | from zope.interface import implementedBy |
---|
26 | from zope.schema.fieldproperty import FieldProperty |
---|
27 | from waeup.sirp.interfaces import IWAeUPSIRPPluggable |
---|
28 | from waeup.sirp.app import University |
---|
29 | from waeup.sirp.applicants.interfaces import ( |
---|
30 | IResultEntry, IApplicant, IApplicantPDEEditData, |
---|
31 | DEFAULT_PASSPORT_IMAGE_MALE, |
---|
32 | ) |
---|
33 | from waeup.sirp.utils.helpers import attrs_to_fields |
---|
34 | |
---|
35 | class ResultEntry(grok.Context): |
---|
36 | grok.implements(IResultEntry) |
---|
37 | |
---|
38 | def __init__(self, subject=None, score=None): |
---|
39 | self.subject = subject |
---|
40 | self.score = score |
---|
41 | |
---|
42 | class Applicant(grok.Context): |
---|
43 | grok.implements(IApplicant, IApplicantPDEEditData) |
---|
44 | grok.provides(IApplicant) |
---|
45 | |
---|
46 | # Set all attributes of Applicant required in IApplicant as field |
---|
47 | # properties. Doing this, we do not have to set initial attributes |
---|
48 | # ourselves and as a bonus we get free validation when an attribute is |
---|
49 | # set. |
---|
50 | Applicant = attrs_to_fields(Applicant) |
---|
51 | |
---|
52 | class ApplicantCatalog(grok.Indexes): |
---|
53 | """A catalog indexing :class:`Applicant` instances in the ZODB. |
---|
54 | """ |
---|
55 | grok.site(University) |
---|
56 | grok.name('applicants_catalog') |
---|
57 | grok.context(IApplicant) |
---|
58 | |
---|
59 | access_code = index.Field(attribute='access_code') |
---|
60 | |
---|
61 | |
---|
62 | class ApplicantTraverser(grok.Traverser): |
---|
63 | """Get image of the context applicant. |
---|
64 | |
---|
65 | Each applicant can provide a passport photograph which will be |
---|
66 | returned by this traverser if: |
---|
67 | |
---|
68 | - we request the exact filename of the picture or |
---|
69 | |
---|
70 | - ask for a picture named 'passport.jpg'. |
---|
71 | |
---|
72 | If no picture was stored yet, we get a placeholder image when |
---|
73 | asking for `passport.jpg`. |
---|
74 | |
---|
75 | If none of the above applies, we return ``None``, most probably |
---|
76 | resulting a :exc:`NotFound` exception. |
---|
77 | |
---|
78 | """ |
---|
79 | grok.context(IApplicant) |
---|
80 | def traverse(self, name): |
---|
81 | passport_filename = getattr(self.context.passport, 'filename', None) |
---|
82 | if name == passport_filename: |
---|
83 | return self.context.passport |
---|
84 | if name == 'passport.jpg': |
---|
85 | if self.context.passport is not None: |
---|
86 | return self.context.passport |
---|
87 | return DEFAULT_PASSPORT_IMAGE_MALE |
---|
88 | return |
---|
89 | |
---|
90 | class ApplicantFactory(grok.GlobalUtility): |
---|
91 | """A factory for faculty containers. |
---|
92 | """ |
---|
93 | grok.implements(IFactory) |
---|
94 | grok.name(u'waeup.Applicant') |
---|
95 | title = u"Create a new applicant.", |
---|
96 | description = u"This factory instantiates new applicant instances." |
---|
97 | |
---|
98 | def __call__(self, *args, **kw): |
---|
99 | return Applicant() |
---|
100 | |
---|
101 | def getInterfaces(self): |
---|
102 | return implementedBy(Applicant) |
---|