Ignore:
Timestamp:
9 Nov 2011, 15:42:45 (13 years ago)
Author:
uli
Message:

Merge changes from branch ulif-extimgstore back into trunk.
Beside external image storage also waeupdocs should work again.

Location:
main/waeup.sirp/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk

  • main/waeup.sirp/trunk/src/waeup/sirp/applicants/applicant.py

    r6632 r7063  
    2020## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    2121##
     22import os
    2223import grok
    2324from grok import index
     
    2526from zope.interface import implementedBy
    2627from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState
    27 from waeup.sirp.interfaces import IObjectHistory
    2828from 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
    2934from waeup.sirp.applicants.interfaces import (
    30     IResultEntry, IApplicant, IApplicantEdit, default_passport_image,
     35    IResultEntry, IApplicant, IApplicantEdit,
    3136    )
    32 from waeup.sirp.utils.helpers import attrs_to_fields
     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
    3344
    3445class ResultEntry(grok.Context):
     
    4960        return
    5061
    51     #def getApplicantsRootLogger(self):
    52     #    return grok.getSite()['applicants'].logger
    53 
    5462    def loggerInfo(self, ob_class, comment=None):
    5563        target = self.__name__
     
    8189    access_code = index.Field(attribute='access_code')
    8290
    83 class ApplicantTraverser(grok.Traverser):
    84     """Get image of the context applicant.
    85 
    86     Each applicant can provide a passport photograph which will be
    87     returned by this traverser if:
    88 
    89     - we request the exact filename of the picture or
    90 
    91     - ask for a picture named 'passport.jpg'.
    92 
    93     If no picture was stored yet, we get a placeholder image when
    94     asking for `passport.jpg`.
    95 
    96     If none of the above applies, we return ``None``, most probably
    97     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.passport
    105         if name == 'passport.jpg':
    106             if self.context.passport is not None:
    107                 return self.context.passport
    108         return
    109 
    11091class ApplicantFactory(grok.GlobalUtility):
    11192    """A factory for applicants.
     
    121102    def getInterfaces(self):
    122103        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    """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
     172class 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.