source: main/waeup.sirp/trunk/src/waeup/sirp/applicants/applicants.py @ 6296

Last change on this file since 6296 was 6296, checked in by Henrik Bettermann, 13 years ago

Move workflow methods from certificates to applicants.

File size: 4.3 KB
Line 
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##
22import grok
23from grok import index
24from zope.component.interfaces import IFactory
25from zope.interface import implementedBy
26from zope.schema.fieldproperty import FieldProperty
27from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState
28from waeup.sirp.interfaces import IWAeUPSIRPPluggable
29from waeup.sirp.app import University
30from waeup.sirp.applicants.interfaces import (
31    IResultEntry, IApplicant, IApplicantEdit,
32    DEFAULT_PASSPORT_IMAGE_MALE,
33    )
34from waeup.sirp.utils.helpers import attrs_to_fields
35
36class ResultEntry(grok.Context):
37    grok.implements(IResultEntry)
38
39    def __init__(self, subject=None, score=None):
40        self.subject = subject
41        self.score = score
42
43class Applicant(grok.Model):
44    grok.implements(IApplicant,IApplicantEdit)
45    grok.provides(IApplicant)
46
47    def __init__(self):
48        super(Applicant, self).__init__()
49        #Initialize workflow state...
50        self.setApplicationState('initialized')
51
52    def getApplicationState(self):
53        return IWorkflowState(self).getState()
54
55    def setApplicationState(self, state=None):
56        """Try to set new state.
57
58        Instead of simply setting a value, we fire a transition of a
59        workflow. This might fail if, for instance, the required state
60        is not part of the workflow or unreachable from current state.
61        """
62        if state == self.getApplicationState():
63            # Accept staying in the same state, even if workflow does
64            # not allow this.
65            return
66        info = IWorkflowInfo(self)
67        info.fireTransitionToward(state)
68        return
69
70    #application_state = property(getApplicationState, setApplicationState)
71
72# Set all attributes of Applicant required in IApplicant as field
73# properties. Doing this, we do not have to set initial attributes
74# ourselves and as a bonus we get free validation when an attribute is
75# set.
76Applicant = attrs_to_fields(Applicant)
77
78class ApplicantCatalog(grok.Indexes):
79    """A catalog indexing :class:`Applicant` instances in the ZODB.
80    """
81    grok.site(University)
82    grok.name('applicants_catalog')
83    grok.context(IApplicant)
84
85    access_code = index.Field(attribute='access_code')
86
87class ApplicantTraverser(grok.Traverser):
88    """Get image of the context applicant.
89
90    Each applicant can provide a passport photograph which will be
91    returned by this traverser if:
92
93    - we request the exact filename of the picture or
94
95    - ask for a picture named 'passport.jpg'.
96
97    If no picture was stored yet, we get a placeholder image when
98    asking for `passport.jpg`.
99
100    If none of the above applies, we return ``None``, most probably
101    resulting a :exc:`NotFound` exception.
102
103    """
104    grok.context(IApplicant)
105    def traverse(self, name):
106        passport_filename = getattr(self.context.passport, 'filename', None)
107        if name == passport_filename:
108            return self.context.passport
109        if name == 'passport.jpg':
110            if self.context.passport is not None:
111                return self.context.passport
112            return DEFAULT_PASSPORT_IMAGE_MALE
113        return
114
115class ApplicantFactory(grok.GlobalUtility):
116    """A factory for faculty containers.
117    """
118    grok.implements(IFactory)
119    grok.name(u'waeup.Applicant')
120    title = u"Create a new applicant.",
121    description = u"This factory instantiates new applicant instances."
122
123    def __call__(self, *args, **kw):
124        return Applicant()
125
126    def getInterfaces(self):
127        return implementedBy(Applicant)
Note: See TracBrowser for help on using the repository browser.