source: main/waeup.sirp/branches/ulif-extimgstore/src/waeup/sirp/applicants/applicant.py @ 7038

Last change on this file since 7038 was 7038, checked in by uli, 13 years ago

Remove obsolete imports/code, reorder imports

File size: 5.2 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 & 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##
22import os
23import grok
24from grok import index
25from zope.component.interfaces import IFactory
26from zope.interface import implementedBy
27from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState
28from waeup.sirp.app import University
29from waeup.sirp.image import WAeUPImageFile
30from waeup.sirp.imagestorage import DefaultFileStoreHandler
31from waeup.sirp.interfaces import (
32    IObjectHistory, IFileStoreHandler, IFileStoreNameChooser)
33from waeup.sirp.utils.helpers import attrs_to_fields
34from waeup.sirp.applicants.interfaces import (
35    IResultEntry, IApplicant, IApplicantEdit,
36    )
37
38
39def get_regno_or_ac(context):
40    reg_no = getattr(context, 'reg_no', None)
41    if reg_no is None:
42        return getattr(context, 'access_code', None)
43    return reg_no
44
45class ResultEntry(grok.Context):
46    grok.implements(IResultEntry)
47
48    def __init__(self, subject=None, score=None):
49        self.subject = subject
50        self.score = score
51
52class Applicant(grok.Model):
53    grok.implements(IApplicant,IApplicantEdit)
54    grok.provides(IApplicant)
55
56    def __init__(self):
57        super(Applicant, self).__init__()
58        IWorkflowInfo(self).fireTransition('init')
59        self.application_date = None
60        return
61
62    def loggerInfo(self, ob_class, comment=None):
63        target = self.__name__
64        return grok.getSite()['applicants'].logger_info(ob_class,target,comment)
65
66    @property
67    def state(self):
68        state = IWorkflowState(self).getState()
69        return state
70
71    @property
72    def history(self):
73        history = IObjectHistory(self)
74        return history
75
76# Set all attributes of Applicant required in IApplicant as field
77# properties. Doing this, we do not have to set initial attributes
78# ourselves and as a bonus we get free validation when an attribute is
79# set.
80Applicant = attrs_to_fields(Applicant)
81
82class ApplicantCatalog(grok.Indexes):
83    """A catalog indexing :class:`Applicant` instances in the ZODB.
84    """
85    grok.site(University)
86    grok.name('applicants_catalog')
87    grok.context(IApplicant)
88
89    access_code = index.Field(attribute='access_code')
90
91class ApplicantFactory(grok.GlobalUtility):
92    """A factory for applicants.
93    """
94    grok.implements(IFactory)
95    grok.name(u'waeup.Applicant')
96    title = u"Create a new applicant.",
97    description = u"This factory instantiates new applicant instances."
98
99    def __call__(self, *args, **kw):
100        return Applicant()
101
102    def getInterfaces(self):
103        return implementedBy(Applicant)
104
105
106#: The file id marker for applicant passport images
107APPLICANT_IMAGE_STORE_NAME = 'img-applicant'
108
109class ApplicantImageNameChooser(grok.Adapter):
110    grok.context(IApplicant)
111    grok.implements(IFileStoreNameChooser)
112
113    def checkName(self, name=None):
114        return name == self.chooseName(name, self.context)
115
116    def chooseName(self, name=None):
117        parent_name = getattr(
118            getattr(self.context, '__parent__', None),
119            '__name__', '_default')
120        marked_filename = '__%s__%s/%s.jpg' % (
121            APPLICANT_IMAGE_STORE_NAME,
122            parent_name, get_regno_or_ac(self.context))
123        return marked_filename
124
125
126class ApplicantImageStoreHandler(DefaultFileStoreHandler, grok.GlobalUtility):
127    """Applicant specific image handling.
128
129    This handler knows in which path in a filestore to store applicant
130    images and how to turn this kind of data into some (browsable)
131    file object.
132
133    It is called from the global file storage, when it wants to
134    get/store a file with a file id starting with
135    ``__img-applicant__`` (the marker string for applicant images).
136
137    Like each other file store handler it does not handle the files
138    really (this is done by the global file store) but only computes
139    paths and things like this.
140    """
141    grok.implements(IFileStoreHandler)
142    grok.name(APPLICANT_IMAGE_STORE_NAME)
143
144    def pathFromFileID(self, store, root, file_id):
145        """All applicants images are filed in directory ``applicants``.
146        """
147        marker, filename, basename, ext = store.extractMarker(file_id)
148        return os.path.join(root, 'applicants', filename)
149
150    def createFile(self, store, root, filename, file_id, file):
151        """Create a browsable file-like object.
152        """
153        # possible other actions: check for jpeg format
154        path = self.pathFromFileID(store, root, file_id)
155        return file, path, WAeUPImageFile(filename, file_id)
Note: See TracBrowser for help on using the repository browser.