- Timestamp:
- 9 Nov 2011, 15:42:45 (13 years ago)
- Location:
- main/waeup.sirp/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk
- Property svn:mergeinfo changed
/main/waeup.sirp/branches/ulif-extimgstore (added) merged: 7001-7002,7010-7011,7016,7031-7041,7043-7044,7046-7055
- Property svn:mergeinfo changed
-
main/waeup.sirp/trunk/src/waeup/sirp/applicants/applicant.py
r6632 r7063 20 20 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 21 ## 22 import os 22 23 import grok 23 24 from grok import index … … 25 26 from zope.interface import implementedBy 26 27 from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState 27 from waeup.sirp.interfaces import IObjectHistory28 28 from waeup.sirp.app import University 29 from waeup.sirp.image import WAeUPImageFile 30 from waeup.sirp.imagestorage import DefaultFileStoreHandler 31 from waeup.sirp.interfaces import ( 32 IObjectHistory, IFileStoreHandler, IFileStoreNameChooser) 33 from waeup.sirp.utils.helpers import attrs_to_fields 29 34 from waeup.sirp.applicants.interfaces import ( 30 IResultEntry, IApplicant, IApplicantEdit, default_passport_image,35 IResultEntry, IApplicant, IApplicantEdit, 31 36 ) 32 from waeup.sirp.utils.helpers import attrs_to_fields 37 38 39 def 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 33 44 34 45 class ResultEntry(grok.Context): … … 49 60 return 50 61 51 #def getApplicantsRootLogger(self):52 # return grok.getSite()['applicants'].logger53 54 62 def loggerInfo(self, ob_class, comment=None): 55 63 target = self.__name__ … … 81 89 access_code = index.Field(attribute='access_code') 82 90 83 class ApplicantTraverser(grok.Traverser):84 """Get image of the context applicant.85 86 Each applicant can provide a passport photograph which will be87 returned by this traverser if:88 89 - we request the exact filename of the picture or90 91 - ask for a picture named 'passport.jpg'.92 93 If no picture was stored yet, we get a placeholder image when94 asking for `passport.jpg`.95 96 If none of the above applies, we return ``None``, most probably97 resulting a :exc:`NotFound` exception.98 99 """100 grok.context(IApplicant)101 def traverse(self, name):102 passport_filename = getattr(self.context.passport, 'filename', None)103 if name == passport_filename:104 return self.context.passport105 if name == 'passport.jpg':106 if self.context.passport is not None:107 return self.context.passport108 return109 110 91 class ApplicantFactory(grok.GlobalUtility): 111 92 """A factory for applicants. … … 121 102 def getInterfaces(self): 122 103 return implementedBy(Applicant) 104 105 106 #: The file id marker for applicant passport images 107 APPLICANT_IMAGE_STORE_NAME = 'img-applicant' 108 109 class ApplicantImageNameChooser(grok.Adapter): 110 """A file id chooser for :class:`Applicant` objects. 111 112 `context` is an :class:`Applicant` instance. 113 114 The :class:`ApplicantImageNameChooser` can build/check file ids 115 for :class:`Applicant` objects suitable for use with 116 :class:`ExtFileStore` instances. The delivered file_id contains 117 the file id marker for :class:`Applicant` object and the 118 registration number or access code of the context applicant. Also 119 the name of the connected applicant container will be part of the 120 generated file id. 121 122 This chooser is registered as an adapter providing 123 :class:`waeup.sirp.interfaces.IFileStoreNameChooser`. 124 125 File store name choosers like this one are only convenience 126 components to ease the task of creating file ids for applicant 127 objects. You are nevertheless encouraged to use them instead of 128 manually setting up filenames for applicants. 129 130 .. seealso:: :mod:`waeup.sirp.imagestorage` 131 132 """ 133 grok.context(IApplicant) 134 grok.implements(IFileStoreNameChooser) 135 136 def checkName(self, name=None): 137 """Check whether the given name is a valid file id for the context. 138 139 Returns ``True`` only if `name` equals the result of 140 :meth:`chooseName`. 141 """ 142 return name == self.chooseName() 143 144 def chooseName(self, name=None): 145 """Get a valid file id for applicant context. 146 147 *Example:* 148 149 For an applicant with registration no. ``'My_reg_no_1234'`` 150 and stored in an applicants container called 151 ``'mycontainer'``, this chooser would create: 152 153 ``'__img-applicant__mycontainer/My_reg_no_1234.jpg'`` 154 155 meaning that the passport image of this applicant would be 156 stored in the site-wide file storage in path: 157 158 ``mycontainer/My_reg_no_1234.jpg`` 159 160 If the context applicant has no parent, ``'_default'`` is used 161 as parent name. 162 """ 163 parent_name = getattr( 164 getattr(self.context, '__parent__', None), 165 '__name__', '_default') 166 marked_filename = '__%s__%s/%s.jpg' % ( 167 APPLICANT_IMAGE_STORE_NAME, 168 parent_name, get_regno_or_ac(self.context)) 169 return marked_filename 170 171 172 class ApplicantImageStoreHandler(DefaultFileStoreHandler, grok.GlobalUtility): 173 """Applicant specific image handling. 174 175 This handler knows in which path in a filestore to store applicant 176 images and how to turn this kind of data into some (browsable) 177 file object. 178 179 It is called from the global file storage, when it wants to 180 get/store a file with a file id starting with 181 ``__img-applicant__`` (the marker string for applicant images). 182 183 Like each other file store handler it does not handle the files 184 really (this is done by the global file store) but only computes 185 paths and things like this. 186 """ 187 grok.implements(IFileStoreHandler) 188 grok.name(APPLICANT_IMAGE_STORE_NAME) 189 190 def pathFromFileID(self, store, root, file_id): 191 """All applicants images are filed in directory ``applicants``. 192 """ 193 marker, filename, basename, ext = store.extractMarker(file_id) 194 return os.path.join(root, 'applicants', filename) 195 196 def createFile(self, store, root, filename, file_id, file): 197 """Create a browsable file-like object. 198 """ 199 # possible other actions: check for jpeg format 200 path = self.pathFromFileID(store, root, file_id) 201 return file, path, WAeUPImageFile(filename, file_id)
Note: See TracChangeset for help on using the changeset viewer.