Ignore:
Timestamp:
12 Jan 2015, 11:00:55 (10 years ago)
Author:
Henrik Bettermann
Message:

Move base file viewlets to browser package.

Location:
main/waeup.kofa/trunk/src/waeup/kofa
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/documents/fileviewlets.py

    r12447 r12448  
    2020from waeup.kofa.interfaces import MessageFactory as _
    2121from waeup.kofa.browser.layout import UtilityView
    22 from waeup.kofa.students.fileviewlets import (
     22from waeup.kofa.browser.fileviewlets import (
    2323    FileDisplay, FileUpload, Image)
    2424
  • main/waeup.kofa/trunk/src/waeup/kofa/students/fileviewlets.py

    r12447 r12448  
    2828from waeup.kofa.students.interfaces import IStudent, IStudentsUtils
    2929
     30from waeup.kofa.browser.fileviewlets import (
     31    FileDisplay, FileUpload, Image)
     32
    3033from waeup.kofa.browser.layout import (
    3134    default_filedisplay_template,
     
    3740    ExportPDFClearanceSlipPage, StudentFilesUploadPage)
    3841
    39 from waeup.kofa.utils.helpers import get_fileformat
    40 
    4142grok.context(IKofaObject) # Make IKofaObject the default context
    4243grok.templatedir('browser_templates')
    4344
    44 ALLOWED_FILE_EXTENSIONS = ('jpg', 'png', 'pdf', 'tif', 'fpm')
     45# File viewlet baseclasses for student base page
    4546
    46 def handle_file_delete(context, view, download_name):
    47     """Handle deletion of student file.
    48 
    49     """
    50     store = getUtility(IExtFileStore)
    51     store.deleteFileByContext(context, attr=download_name)
    52     context.writeLogMessage(view, 'deleted: %s' % download_name)
    53     view.flash(_('${a} deleted.', mapping = {'a':download_name}))
    54     return
    55 
    56 def handle_file_upload(upload, context, view, max_size, download_name=None):
    57     """Handle upload of student file.
    58 
    59     Returns `True` in case of success or `False`.
    60 
    61     Please note that file pointer passed in (`upload`) most probably
    62     points to end of file when leaving this function.
    63     """
    64     # Check some file requirements first
    65     size = file_size(upload)
    66     if size > max_size:
    67         view.flash(_('Uploaded file is too big.'), type="danger")
    68         return False
    69     upload.seek(0) # file pointer moved when determining size
    70     dummy,ext = os.path.splitext(upload.filename)
    71     # fpm files are expected to be fingerprint minutiae, file
    72     # format is not yet checked
    73     if ext == '.fpm':
    74         file_format = 'fpm'
    75     else:
    76         file_format = get_fileformat(None, upload.read(512))
    77         upload.seek(0) # same here
    78     if file_format is None:
    79         view.flash(_('Could not determine file type.'), type="danger")
    80         return False
    81     basename, expected_ext = os.path.splitext(download_name)
    82     if expected_ext:
    83         if '.' + file_format != expected_ext:
    84             view.flash(_('${a} file format expected.',
    85                 mapping = {'a':expected_ext[1:]}), type="danger")
    86             return False
    87     else:
    88         if not file_format in ALLOWED_FILE_EXTENSIONS:
    89             view.flash(
    90                 _('Only the following extensions are allowed: ${a}',
    91                 mapping = {'a':', '.join(ALLOWED_FILE_EXTENSIONS)}),
    92                 type="danger")
    93             return False
    94         download_name += '.' + file_format
    95     store = getUtility(IExtFileStore)
    96     file_id = IFileStoreNameChooser(context).chooseName(attr=download_name)
    97     store.createFile(file_id, upload)
    98     context.writeLogMessage(view, 'uploaded: %s (%s)' % (
    99         download_name,upload.filename))
    100     view.flash(_('File ${a} uploaded.', mapping = {'a':download_name}))
    101     return True
    102 
    103 # File viewlets for student base page
    104 
    105 class FileManager(grok.ViewletManager):
    106     """Viewlet manager for uploading files, preferably scanned images.
    107     """
    108     grok.name('files')
    109 
    110 class FileDisplay(grok.Viewlet):
     47class StudentFileDisplay(FileDisplay):
    11148    """Base file display viewlet.
    11249    """
    11350    grok.baseclass()
    11451    grok.context(IStudent)
    115     grok.viewletmanager(FileManager)
    11652    grok.view(StudentClearanceDisplayFormPage)
    117     template = default_filedisplay_template
    11853    grok.order(1)
    11954    grok.require('waeup.viewStudent')
    120     label = _(u'File')
    121     title = _(u'Scan')
    12255
    123     @property
    124     def download_filename(self):
    125         return self.download_name
    12656
    127     @property
    128     def file_exists(self):
    129         image = getUtility(IExtFileStore).getFileByContext(
    130             self.context, attr=self.download_name)
    131         if image:
    132             return True
    133         else:
    134             return False
    135 
    136 class FileUpload(FileDisplay):
     57class StudentFileUpload(FileUpload):
    13758    """Base upload viewlet.
    13859    """
    13960    grok.baseclass()
    14061    grok.context(IStudent)
    141     grok.viewletmanager(FileManager)
    14262    grok.view(StudentClearanceManageFormPage)
    143     template = default_fileupload_template
    14463    grok.require('waeup.uploadStudentFile')
    145     tab_redirect = '#tab2-top'
    146     mus = 1024 * 150
    147     upload_button =_('Upload selected file')
    148     delete_button = _('Delete')
    14964
    15065    @property
     
    15570        return True
    15671
    157     @property
    158     def input_name(self):
    159         return "%s" % self.__name__
    16072
    161     def update(self):
    162         self.max_upload_size = string_from_bytes(self.mus)
    163         delete_button = self.request.form.get(
    164             'delete_%s' % self.input_name, None)
    165         upload_button = self.request.form.get(
    166             'upload_%s' % self.input_name, None)
    167         if delete_button:
    168             handle_file_delete(
    169                 context=self.context, view=self.view,
    170                 download_name=self.download_name)
    171             self.view.redirect(
    172                 self.view.url(
    173                     self.context, self.view.__name__) + self.tab_redirect)
    174             return
    175         if upload_button:
    176             upload = self.request.form.get(self.input_name, None)
    177             if upload:
    178                 # We got a fresh upload
    179                 handle_file_upload(upload,
    180                     self.context, self.view, self.mus, self.download_name)
    181                 self.view.redirect(
    182                     self.view.url(
    183                         self.context, self.view.__name__) + self.tab_redirect)
    184             else:
    185                 self.view.flash(_('No local file selected.'), type="danger")
    186                 self.view.redirect(
    187                     self.view.url(
    188                         self.context, self.view.__name__) + self.tab_redirect)
    189         return
     73class StudentImage(Image):
     74    """Renders images for students.
     75    """
     76    grok.baseclass()
     77    grok.context(IStudent)
     78    grok.require('waeup.viewStudent')
    19079
    191 class PassportDisplay(FileDisplay):
     80
     81# File viewlets for student base page
     82
     83class PassportDisplay(StudentFileDisplay):
    19284    """Passport display viewlet.
    19385    """
     
    20092    download_name = u'passport.jpg'
    20193
    202 class PassportUploadManage(FileUpload):
     94
     95class PassportUploadManage(StudentFileUpload):
    20396    """Passport upload viewlet for officers.
    20497    """
     
    213106    tab_redirect = '#tab2'
    214107
     108
    215109class PassportUploadEdit(PassportUploadManage):
    216110    """Passport upload viewlet for students.
     
    219113    grok.require('waeup.uploadStudentFile')
    220114
    221 class BirthCertificateDisplay(FileDisplay):
     115
     116class BirthCertificateDisplay(StudentFileDisplay):
    222117    """Birth Certificate display viewlet.
    223118    """
     
    227122    download_name = u'birth_certificate'
    228123
     124
    229125class BirthCertificateSlip(BirthCertificateDisplay):
    230126    grok.view(ExportPDFClearanceSlipPage)
    231127
    232 class BirthCertificateUpload(FileUpload):
     128
     129class BirthCertificateUpload(StudentFileUpload):
    233130    """Birth Certificate upload viewlet.
    234131    """
     
    240137    tab_redirect = '#tab2-top'
    241138
    242 class Image(grok.View):
    243     """Renders images for students.
    244     """
    245     grok.baseclass()
    246     grok.context(IStudent)
    247     grok.require('waeup.viewStudent')
    248139
    249     @property
    250     def download_filename(self):
    251         return self.download_name
    252 
    253     def render(self):
    254         # A filename chooser turns a context into a filename suitable
    255         # for file storage.
    256         image = getUtility(IExtFileStore).getFileByContext(
    257             self.context, attr=self.download_name)
    258         if image is None:
    259             # show placeholder image
    260             self.response.setHeader('Content-Type', 'image/jpeg')
    261             return open(DEFAULT_IMAGE_PATH, 'rb').read()
    262         dummy,ext = os.path.splitext(image.name)
    263         if ext == '.jpg':
    264             self.response.setHeader('Content-Type', 'image/jpeg')
    265         elif ext == '.fpm':
    266             self.response.setHeader('Content-Type', 'application/binary')
    267         elif ext == '.png':
    268             self.response.setHeader('Content-Type', 'image/png')
    269         elif ext == '.tif':
    270             self.response.setHeader('Content-Type', 'image/tiff')
    271         elif ext == '.pdf':
    272             self.response.setHeader('Content-Type', 'application/pdf')
    273             self.response.setHeader('Content-Disposition',
    274                 'attachment; filename="%s.pdf' % self.download_filename)
    275         return image
    276 
    277 class Passport(Image):
     140class Passport(StudentImage):
    278141    """Renders jpeg passport picture.
    279142    """
     
    282145    grok.context(IStudent)
    283146
    284 class ApplicationSlipImage(Image):
     147
     148class ApplicationSlipImage(StudentImage):
    285149    """Renders application slip scan.
    286150    """
     
    288152    download_name = u'application_slip'
    289153
    290 class BirthCertificateImage(Image):
     154
     155class BirthCertificateImage(StudentImage):
    291156    """Renders birth certificate scan.
    292157    """
  • main/waeup.kofa/trunk/src/waeup/kofa/students/utils.py

    r12421 r12448  
    281281    """
    282282    # XXX: fix circular import problem
    283     from waeup.kofa.students.fileviewlets import FileManager
     283    from waeup.kofa.browser.fileviewlets import FileManager
    284284    from waeup.kofa.browser import DEFAULT_IMAGE_PATH
    285285    style = getSampleStyleSheet()
Note: See TracChangeset for help on using the changeset viewer.