import os import grok from zope.component import getUtility from zope.interface import Interface from waeup.sirp.interfaces import ( IWAeUPObject, IExtFileStore, IFileStoreNameChooser) from waeup.sirp.utils.helpers import string_from_bytes, file_size from waeup.sirp.browser import DEFAULT_IMAGE_PATH from waeup.sirp.students.browser import ( StudentClearanceDisplayFormPage, StudentClearanceManageFormPage, write_log_message) from waeup.sirp.students.interfaces import IStudentClearance grok.context(IWAeUPObject) # Make IWAeUPObject the default context grok.templatedir('browser_templates') class StudentManageSidebar(grok.ViewletManager): grok.name('left_studentmanage') class StudentManageLink(grok.Viewlet): """A link displayed in the student box which shows up for StudentNavigation objects. """ grok.baseclass() grok.viewletmanager(StudentManageSidebar) grok.context(IWAeUPObject) grok.view(Interface) grok.order(5) grok.require('waeup.viewStudent') link = 'index' text = u'Base Data' def render(self): url = self.view.url(self.context.getStudent(), self.link) return u'
%s
' % ( url, self.text) class StudentManageBaseLink(StudentManageLink): grok.order(1) link = 'index' text = u'Base Data' class StudentManageClearanceLink(StudentManageLink): grok.order(2) link = 'view_clearance' text = u'Clearance Data' class StudentManagePersonalLink(StudentManageLink): grok.order(2) link = 'view_personal' text = u'Personal Data' class StudentManageStudyCourseLink(StudentManageLink): grok.order(3) link = 'studycourse' text = u'Study Course' class StudentManagePaymentsLink(StudentManageLink): grok.order(4) link = 'payments' text = u'Payments' class StudentManageAccommodationLink(StudentManageLink): grok.order(5) link = 'accommodation' text = u'Accommodation Data' class StudentManageHistoryLink(StudentManageLink): grok.order(6) link = 'history' text = u'History' class StudentMenu(grok.ViewletManager): grok.name('top_student') class StudentLink(grok.Viewlet): """A link displayed in the student box which shows up for StudentNavigation objects. """ grok.baseclass() grok.viewletmanager(StudentMenu) grok.context(IWAeUPObject) grok.view(Interface) grok.order(5) grok.require('waeup.viewStudent') template = grok.PageTemplateFile('browser_templates/plainactionbutton.pt') link = 'index' text = u'Base Data' @property def alt(self): """Alternative text for icon. """ return self.text @property def target_url(self): """Get a URL to the target... """ return self.view.url(self.context.getStudent(), self.link) class StudentBaseLink(StudentLink): grok.order(1) link = 'index' text = u'Base Data' class StudentClearanceLink(StudentLink): grok.order(2) link = 'view_clearance' text = u'Clearance Data' class StudentPersonalLink(StudentLink): grok.order(2) link = 'view_personal' text = u'Personal Data' class StudentStudyCourseLink(StudentLink): grok.order(3) link = 'studycourse' text = u'Study Course' class StudentPaymentsLink(StudentLink): grok.order(4) link = 'payments' text = u'Payments' class StudentAccommodationLink(StudentLink): grok.order(5) link = 'accommodation' text = u'Accommodation' class StudentHistoryLink(StudentLink): grok.order(6) link = 'history' text = u'History' class PrimaryStudentNavManager(grok.ViewletManager): """Viewlet manager for the primary navigation tab. """ grok.name('primary_nav_student') class PrimaryStudentNavTab(grok.Viewlet): """Base for primary student nav tabs. """ grok.baseclass() grok.viewletmanager(PrimaryStudentNavManager) grok.template('primarynavtab') grok.order(1) grok.require('waeup.View') pnav = 0 tab_title = u'Some Text' @property def link_target(self): return self.view.application_url() @property def active(self): view_pnav = getattr(self.view, 'pnav', 0) if view_pnav == self.pnav: return 'active' return '' class HomeTab(PrimaryStudentNavTab): """Home-tab in primary navigation. """ grok.order(1) grok.require('waeup.Public') pnav = 0 tab_title = u'Home' class ProspectusTab(PrimaryStudentNavTab): """Faculties-tab in primary navigation. """ grok.order(2) grok.require('waeup.View') pnav = 1 tab_title = u'Prospectus' @property def link_target(self): return self.view.application_url('faculties') class MyDataTab(PrimaryStudentNavTab): """MyData-tab in primary navigation. """ grok.order(3) grok.require('waeup.Public') pnav = 4 tab_title = u'My Data' @property def link_target(self): rel_link = '/students/%s' % self.request.principal.id return self.view.application_url() + rel_link def handle_file_delete(context, view, download_name): """Handle deletion of student file. """ store = getUtility(IExtFileStore) store.deleteFileByContext(context, attr=download_name) write_log_message(view, 'deleted: %s' % download_name) view.flash('File %s deleted.' % download_name) return def handle_file_upload(upload, context, view, max_size, download_name=None): """Handle upload of student file. Returns `True` in case of success or `False`. Please note that file pointer passed in (`upload`) most probably points to end of file when leaving this function. """ # Check some file requirements first if upload.filename.count('.') == 0: view.flash('File name has no extension.') return False if upload.filename.count('.') > 1: view.flash('File name contains more than one dot.') return False basename, expected_ext = os.path.splitext(download_name) dummy, ext = os.path.splitext(upload.filename) ext.lower() if ext != expected_ext: view.flash('%s file extension expected.' % expected_ext) return False size = file_size(upload) if size > max_size: view.flash('Uploaded file is too big.') return False upload.seek(0) # file pointer moved when determining size store = getUtility(IExtFileStore) file_id = IFileStoreNameChooser(context).chooseName(attr=download_name) store.createFile(file_id, upload) write_log_message(view, 'uploaded: %s (%s)' % (download_name,upload.filename)) view.flash('File %s uploaded.' % download_name) return True class FileManager(grok.ViewletManager): """Viewlet manager for uploading files, preferably scanned images. """ grok.name('files') class FileDisplay(grok.Viewlet): """Base file display viewlet. """ grok.baseclass() grok.context(IStudentClearance) grok.viewletmanager(FileManager) grok.view(StudentClearanceDisplayFormPage) grok.template('filedisplay') grok.order(1) grok.require('waeup.viewStudent') label = u'File:' download_name = u'filename.jpg' @property def file_exists(self): image = getUtility(IExtFileStore).getFileByContext( self.context, attr=self.download_name) if image: return True else: return False class FileUpload(FileDisplay): """Base upload viewlet. """ grok.baseclass() grok.context(IStudentClearance) grok.viewletmanager(FileManager) grok.view(StudentClearanceManageFormPage) grok.template('fileupload') grok.require('waeup.manageStudents') mus = 1024 * 150 input_name = u'filename' def update(self): self.max_upload_size = string_from_bytes(self.mus) delete_button = self.request.form.get('delete', None) if delete_button: handle_file_delete( context=self.context, view=self.view, download_name=self.download_name) self.view.redirect( self.view.url(self.context, self.view.__name__)) return upload_button = self.request.form.get('upload', None) if upload_button: upload = self.request.form.get(self.input_name, None) if upload: # We got a fresh upload file_changed = handle_file_upload( upload, self.context, self.view, self.mus, self.download_name) if file_changed is False: # False is not None! self.view.redirect( self.view.url(self.context, self.view.__name__)) return # error during file upload. Ignore other values return class BirthCertificateDisplay(FileDisplay): """Birth Certificate upload viewlet. """ grok.order(1) label = u'Birth Certificate:' download_name = u'birth_certificate.jpg' class BirthCertificateUpload(FileUpload): """Birth Certificate upload viewlet. """ grok.order(1) label = u'Birth Certificate (jpg only):' mus = 1024 * 150 download_name = u'birth_certificate.jpg' input_name = u'birth_certificate' class Image(grok.View): """Renders jpeg images for students. """ grok.baseclass() grok.name('none.jpg') grok.view(StudentClearanceManageFormPage) grok.require('waeup.viewStudent') download_name = u'none.jpg' def render(self): # A filename chooser turns a context into a filename suitable # for file storage. image = getUtility(IExtFileStore).getFileByContext( self.context, attr=self.download_name) # We expect that image is a jpeg pictures self.response.setHeader( 'Content-Type', 'image/jpeg') if image is None: # show placeholder image return open(DEFAULT_IMAGE_PATH, 'rb').read() return image class BirthCertificateImage(Image): """Renders birth certificate jpeg image. """ grok.name('birth_certificate.jpg') download_name = u'birth_certificate.jpg'