[7106] | 1 | import os |
---|
[6642] | 2 | import grok |
---|
[7097] | 3 | from zope.component import getUtility |
---|
[6642] | 4 | from zope.interface import Interface |
---|
[7097] | 5 | from waeup.sirp.interfaces import ( |
---|
| 6 | IWAeUPObject, IExtFileStore, IFileStoreNameChooser) |
---|
| 7 | from waeup.sirp.utils.helpers import string_from_bytes, file_size |
---|
| 8 | from waeup.sirp.browser import DEFAULT_IMAGE_PATH |
---|
[7184] | 9 | from waeup.sirp.browser.viewlets import PrimaryNavTab |
---|
[7097] | 10 | from waeup.sirp.students.browser import ( |
---|
[7108] | 11 | StudentClearanceDisplayFormPage, StudentClearanceManageFormPage, |
---|
[7112] | 12 | write_log_message, StudentBaseManageFormPage, StudentBaseDisplayFormPage, |
---|
[7114] | 13 | StudentFilesUploadPage) |
---|
[7112] | 14 | from waeup.sirp.students.interfaces import IStudent, IStudentClearance |
---|
[6642] | 15 | |
---|
| 16 | grok.context(IWAeUPObject) # Make IWAeUPObject the default context |
---|
[6687] | 17 | grok.templatedir('browser_templates') |
---|
[6642] | 18 | |
---|
[7123] | 19 | ALLOWED_FILE_EXTENSIONS = ('jpg', 'png', 'pdf', 'tif') |
---|
| 20 | |
---|
[6687] | 21 | class StudentManageSidebar(grok.ViewletManager): |
---|
| 22 | grok.name('left_studentmanage') |
---|
[6642] | 23 | |
---|
[6687] | 24 | class StudentManageLink(grok.Viewlet): |
---|
[6660] | 25 | """A link displayed in the student box which shows up for StudentNavigation |
---|
[6642] | 26 | objects. |
---|
| 27 | |
---|
| 28 | """ |
---|
| 29 | grok.baseclass() |
---|
[6687] | 30 | grok.viewletmanager(StudentManageSidebar) |
---|
[6642] | 31 | grok.context(IWAeUPObject) |
---|
| 32 | grok.view(Interface) |
---|
| 33 | grok.order(5) |
---|
[6660] | 34 | grok.require('waeup.viewStudent') |
---|
[6642] | 35 | |
---|
| 36 | link = 'index' |
---|
| 37 | text = u'Base Data' |
---|
| 38 | |
---|
| 39 | def render(self): |
---|
| 40 | url = self.view.url(self.context.getStudent(), self.link) |
---|
| 41 | return u'<div class="portlet"><a href="%s">%s</a></div>' % ( |
---|
| 42 | url, self.text) |
---|
| 43 | |
---|
[6687] | 44 | class StudentManageBaseLink(StudentManageLink): |
---|
| 45 | grok.order(1) |
---|
| 46 | link = 'index' |
---|
| 47 | text = u'Base Data' |
---|
| 48 | |
---|
| 49 | class StudentManageClearanceLink(StudentManageLink): |
---|
| 50 | grok.order(2) |
---|
| 51 | link = 'view_clearance' |
---|
| 52 | text = u'Clearance Data' |
---|
| 53 | |
---|
| 54 | class StudentManagePersonalLink(StudentManageLink): |
---|
| 55 | grok.order(2) |
---|
| 56 | link = 'view_personal' |
---|
| 57 | text = u'Personal Data' |
---|
| 58 | |
---|
| 59 | class StudentManageStudyCourseLink(StudentManageLink): |
---|
| 60 | grok.order(3) |
---|
| 61 | link = 'studycourse' |
---|
| 62 | text = u'Study Course' |
---|
| 63 | |
---|
| 64 | class StudentManagePaymentsLink(StudentManageLink): |
---|
| 65 | grok.order(4) |
---|
[7181] | 66 | grok.require('waeup.payStudent') |
---|
[6687] | 67 | link = 'payments' |
---|
| 68 | text = u'Payments' |
---|
| 69 | |
---|
| 70 | class StudentManageAccommodationLink(StudentManageLink): |
---|
| 71 | grok.order(5) |
---|
[7181] | 72 | grok.require('waeup.handleAccommodation') |
---|
[6687] | 73 | link = 'accommodation' |
---|
| 74 | text = u'Accommodation Data' |
---|
| 75 | |
---|
| 76 | class StudentManageHistoryLink(StudentManageLink): |
---|
| 77 | grok.order(6) |
---|
| 78 | link = 'history' |
---|
| 79 | text = u'History' |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | class StudentMenu(grok.ViewletManager): |
---|
| 83 | grok.name('top_student') |
---|
| 84 | |
---|
| 85 | class StudentLink(grok.Viewlet): |
---|
| 86 | """A link displayed in the student box which shows up for StudentNavigation |
---|
| 87 | objects. |
---|
| 88 | |
---|
| 89 | """ |
---|
| 90 | grok.baseclass() |
---|
| 91 | grok.viewletmanager(StudentMenu) |
---|
| 92 | grok.context(IWAeUPObject) |
---|
| 93 | grok.view(Interface) |
---|
| 94 | grok.order(5) |
---|
| 95 | grok.require('waeup.viewStudent') |
---|
| 96 | template = grok.PageTemplateFile('browser_templates/plainactionbutton.pt') |
---|
| 97 | |
---|
| 98 | link = 'index' |
---|
| 99 | text = u'Base Data' |
---|
| 100 | |
---|
| 101 | @property |
---|
| 102 | def target_url(self): |
---|
| 103 | """Get a URL to the target... |
---|
| 104 | """ |
---|
| 105 | return self.view.url(self.context.getStudent(), self.link) |
---|
| 106 | |
---|
[6642] | 107 | class StudentBaseLink(StudentLink): |
---|
| 108 | grok.order(1) |
---|
| 109 | link = 'index' |
---|
| 110 | text = u'Base Data' |
---|
| 111 | |
---|
| 112 | class StudentClearanceLink(StudentLink): |
---|
| 113 | grok.order(2) |
---|
| 114 | link = 'view_clearance' |
---|
| 115 | text = u'Clearance Data' |
---|
| 116 | |
---|
| 117 | class StudentPersonalLink(StudentLink): |
---|
| 118 | grok.order(2) |
---|
| 119 | link = 'view_personal' |
---|
| 120 | text = u'Personal Data' |
---|
| 121 | |
---|
| 122 | class StudentStudyCourseLink(StudentLink): |
---|
| 123 | grok.order(3) |
---|
| 124 | link = 'studycourse' |
---|
| 125 | text = u'Study Course' |
---|
| 126 | |
---|
| 127 | class StudentPaymentsLink(StudentLink): |
---|
| 128 | grok.order(4) |
---|
| 129 | link = 'payments' |
---|
| 130 | text = u'Payments' |
---|
| 131 | |
---|
| 132 | class StudentAccommodationLink(StudentLink): |
---|
| 133 | grok.order(5) |
---|
| 134 | link = 'accommodation' |
---|
[6687] | 135 | text = u'Accommodation' |
---|
[6642] | 136 | |
---|
| 137 | class StudentHistoryLink(StudentLink): |
---|
| 138 | grok.order(6) |
---|
| 139 | link = 'history' |
---|
| 140 | text = u'History' |
---|
[6687] | 141 | |
---|
[7184] | 142 | class StudentsTab(PrimaryNavTab): |
---|
| 143 | """Students tab in primary navigation. |
---|
| 144 | """ |
---|
| 145 | |
---|
| 146 | grok.context(IWAeUPObject) |
---|
| 147 | grok.order(4) |
---|
| 148 | grok.require('waeup.viewStudents') |
---|
| 149 | grok.template('primarynavtab') |
---|
| 150 | |
---|
| 151 | pnav = 4 |
---|
| 152 | tab_title = u'Students' |
---|
| 153 | |
---|
| 154 | @property |
---|
| 155 | def link_target(self): |
---|
| 156 | return self.view.application_url('students') |
---|
| 157 | |
---|
[6687] | 158 | class PrimaryStudentNavManager(grok.ViewletManager): |
---|
| 159 | """Viewlet manager for the primary navigation tab. |
---|
| 160 | """ |
---|
| 161 | grok.name('primary_nav_student') |
---|
| 162 | |
---|
| 163 | class PrimaryStudentNavTab(grok.Viewlet): |
---|
| 164 | """Base for primary student nav tabs. |
---|
| 165 | """ |
---|
| 166 | grok.baseclass() |
---|
| 167 | grok.viewletmanager(PrimaryStudentNavManager) |
---|
| 168 | grok.template('primarynavtab') |
---|
| 169 | grok.order(1) |
---|
[7184] | 170 | grok.require('waeup.Authenticated') |
---|
[6687] | 171 | pnav = 0 |
---|
| 172 | tab_title = u'Some Text' |
---|
| 173 | |
---|
| 174 | @property |
---|
| 175 | def link_target(self): |
---|
| 176 | return self.view.application_url() |
---|
| 177 | |
---|
| 178 | @property |
---|
| 179 | def active(self): |
---|
| 180 | view_pnav = getattr(self.view, 'pnav', 0) |
---|
| 181 | if view_pnav == self.pnav: |
---|
| 182 | return 'active' |
---|
| 183 | return '' |
---|
| 184 | |
---|
[7184] | 185 | #class HomeTab(PrimaryStudentNavTab): |
---|
| 186 | # """Home-tab in primary navigation. |
---|
| 187 | # """ |
---|
| 188 | # grok.order(1) |
---|
| 189 | # grok.require('waeup.Authenticated') |
---|
| 190 | # pnav = 0 |
---|
| 191 | # tab_title = u'Home' |
---|
[6687] | 192 | |
---|
[7184] | 193 | #class ProspectusTab(PrimaryStudentNavTab): |
---|
| 194 | # """Faculties-tab in primary navigation. |
---|
| 195 | # """ |
---|
| 196 | # grok.order(2) |
---|
| 197 | # grok.require('waeup.viewAcademics') |
---|
| 198 | # pnav = 1 |
---|
| 199 | # tab_title = u'Prospectus' |
---|
[6687] | 200 | |
---|
[7184] | 201 | # @property |
---|
| 202 | # def link_target(self): |
---|
| 203 | # return self.view.application_url('faculties') |
---|
[6687] | 204 | |
---|
| 205 | class MyDataTab(PrimaryStudentNavTab): |
---|
| 206 | """MyData-tab in primary navigation. |
---|
| 207 | """ |
---|
| 208 | grok.order(3) |
---|
[7184] | 209 | grok.require('waeup.Authenticated') |
---|
[6687] | 210 | pnav = 4 |
---|
| 211 | tab_title = u'My Data' |
---|
| 212 | |
---|
| 213 | @property |
---|
| 214 | def link_target(self): |
---|
| 215 | rel_link = '/students/%s' % self.request.principal.id |
---|
[7097] | 216 | return self.view.application_url() + rel_link |
---|
| 217 | |
---|
[7107] | 218 | def handle_file_delete(context, view, download_name): |
---|
| 219 | """Handle deletion of student file. |
---|
| 220 | |
---|
| 221 | """ |
---|
| 222 | store = getUtility(IExtFileStore) |
---|
| 223 | store.deleteFileByContext(context, attr=download_name) |
---|
[7108] | 224 | write_log_message(view, 'deleted: %s' % download_name) |
---|
[7123] | 225 | view.flash('%s deleted.' % download_name) |
---|
[7107] | 226 | return |
---|
| 227 | |
---|
[7106] | 228 | def handle_file_upload(upload, context, view, max_size, download_name=None): |
---|
| 229 | """Handle upload of student file. |
---|
[7097] | 230 | |
---|
| 231 | Returns `True` in case of success or `False`. |
---|
| 232 | |
---|
| 233 | Please note that file pointer passed in (`upload`) most probably |
---|
| 234 | points to end of file when leaving this function. |
---|
| 235 | """ |
---|
[7106] | 236 | # Check some file requirements first |
---|
| 237 | if upload.filename.count('.') == 0: |
---|
| 238 | view.flash('File name has no extension.') |
---|
| 239 | return False |
---|
| 240 | if upload.filename.count('.') > 1: |
---|
| 241 | view.flash('File name contains more than one dot.') |
---|
| 242 | return False |
---|
| 243 | basename, expected_ext = os.path.splitext(download_name) |
---|
| 244 | dummy, ext = os.path.splitext(upload.filename) |
---|
| 245 | ext.lower() |
---|
[7123] | 246 | if expected_ext: |
---|
| 247 | if ext != expected_ext: |
---|
| 248 | view.flash('%s file extension expected.' % |
---|
| 249 | expected_ext.replace('.','')) |
---|
| 250 | return False |
---|
| 251 | else: |
---|
| 252 | if not ext.replace('.','') in ALLOWED_FILE_EXTENSIONS: |
---|
| 253 | view.flash( |
---|
| 254 | 'Only the following extension are allowed: %s' % |
---|
| 255 | ', '.join(ALLOWED_FILE_EXTENSIONS)) |
---|
| 256 | return False |
---|
| 257 | download_name += ext |
---|
[7097] | 258 | size = file_size(upload) |
---|
| 259 | if size > max_size: |
---|
[7106] | 260 | view.flash('Uploaded file is too big.') |
---|
[7097] | 261 | return False |
---|
| 262 | upload.seek(0) # file pointer moved when determining size |
---|
| 263 | store = getUtility(IExtFileStore) |
---|
[7106] | 264 | file_id = IFileStoreNameChooser(context).chooseName(attr=download_name) |
---|
[7097] | 265 | store.createFile(file_id, upload) |
---|
[7108] | 266 | write_log_message(view, 'uploaded: %s (%s)' % (download_name,upload.filename)) |
---|
| 267 | view.flash('File %s uploaded.' % download_name) |
---|
[7097] | 268 | return True |
---|
| 269 | |
---|
| 270 | class FileManager(grok.ViewletManager): |
---|
| 271 | """Viewlet manager for uploading files, preferably scanned images. |
---|
| 272 | """ |
---|
| 273 | grok.name('files') |
---|
| 274 | |
---|
| 275 | class FileDisplay(grok.Viewlet): |
---|
| 276 | """Base file display viewlet. |
---|
| 277 | """ |
---|
| 278 | grok.baseclass() |
---|
[7100] | 279 | grok.context(IStudentClearance) |
---|
[7097] | 280 | grok.viewletmanager(FileManager) |
---|
| 281 | grok.view(StudentClearanceDisplayFormPage) |
---|
| 282 | grok.template('filedisplay') |
---|
| 283 | grok.order(1) |
---|
| 284 | grok.require('waeup.viewStudent') |
---|
[7106] | 285 | label = u'File:' |
---|
[7127] | 286 | title = u'Scan' |
---|
[7106] | 287 | download_name = u'filename.jpg' |
---|
[7097] | 288 | |
---|
[7107] | 289 | @property |
---|
| 290 | def file_exists(self): |
---|
| 291 | image = getUtility(IExtFileStore).getFileByContext( |
---|
| 292 | self.context, attr=self.download_name) |
---|
| 293 | if image: |
---|
| 294 | return True |
---|
| 295 | else: |
---|
| 296 | return False |
---|
| 297 | |
---|
[7097] | 298 | class FileUpload(FileDisplay): |
---|
| 299 | """Base upload viewlet. |
---|
| 300 | """ |
---|
| 301 | grok.baseclass() |
---|
[7100] | 302 | grok.context(IStudentClearance) |
---|
[7097] | 303 | grok.viewletmanager(FileManager) |
---|
| 304 | grok.view(StudentClearanceManageFormPage) |
---|
| 305 | grok.template('fileupload') |
---|
[7127] | 306 | grok.require('waeup.uploadStudentFile') |
---|
[7134] | 307 | tab_redirect = '' |
---|
[7097] | 308 | mus = 1024 * 150 |
---|
| 309 | |
---|
[7117] | 310 | @property |
---|
| 311 | def input_name(self): |
---|
| 312 | return "%s" % self.__name__ |
---|
| 313 | |
---|
[7097] | 314 | def update(self): |
---|
| 315 | self.max_upload_size = string_from_bytes(self.mus) |
---|
[7117] | 316 | delete_button = self.request.form.get( |
---|
| 317 | 'delete_%s' % self.input_name, None) |
---|
| 318 | upload_button = self.request.form.get( |
---|
| 319 | 'upload_%s' % self.input_name, None) |
---|
[7108] | 320 | if delete_button: |
---|
[7107] | 321 | handle_file_delete( |
---|
| 322 | context=self.context, view=self.view, |
---|
| 323 | download_name=self.download_name) |
---|
| 324 | self.view.redirect( |
---|
[7134] | 325 | self.view.url( |
---|
| 326 | self.context, self.view.__name__) + self.tab_redirect) |
---|
[7107] | 327 | return |
---|
[7108] | 328 | if upload_button: |
---|
| 329 | upload = self.request.form.get(self.input_name, None) |
---|
| 330 | if upload: |
---|
| 331 | # We got a fresh upload |
---|
[7111] | 332 | handle_file_upload(upload, |
---|
| 333 | self.context, self.view, self.mus, self.download_name) |
---|
| 334 | self.view.redirect( |
---|
[7134] | 335 | self.view.url( |
---|
| 336 | self.context, self.view.__name__) + self.tab_redirect) |
---|
[7117] | 337 | else: |
---|
| 338 | self.view.flash('No local file selected.') |
---|
| 339 | self.view.redirect( |
---|
[7134] | 340 | self.view.url( |
---|
| 341 | self.context, self.view.__name__) + self.tab_redirect) |
---|
[7097] | 342 | return |
---|
| 343 | |
---|
[7112] | 344 | class PassportDisplay(FileDisplay): |
---|
| 345 | """Passport display viewlet. |
---|
| 346 | """ |
---|
| 347 | grok.order(1) |
---|
| 348 | grok.context(IStudent) |
---|
| 349 | grok.view(StudentBaseDisplayFormPage) |
---|
| 350 | grok.require('waeup.viewStudent') |
---|
| 351 | grok.template('imagedisplay') |
---|
| 352 | label = u'Passport Picture:' |
---|
| 353 | download_name = u'passport.jpg' |
---|
| 354 | |
---|
| 355 | class PassportUploadManage(FileUpload): |
---|
| 356 | """Passport upload viewlet for officers. |
---|
| 357 | """ |
---|
| 358 | grok.order(1) |
---|
| 359 | grok.context(IStudent) |
---|
| 360 | grok.view(StudentBaseManageFormPage) |
---|
[7136] | 361 | grok.require('waeup.manageStudent') |
---|
[7112] | 362 | grok.template('imageupload') |
---|
| 363 | label = u'Passport Picture (jpg only):' |
---|
| 364 | mus = 1024 * 50 |
---|
| 365 | download_name = u'passport.jpg' |
---|
[7134] | 366 | tab_redirect = '#tab-2' |
---|
[7112] | 367 | |
---|
| 368 | class PassportUploadEdit(PassportUploadManage): |
---|
| 369 | """Passport upload viewlet for students. |
---|
| 370 | """ |
---|
[7114] | 371 | grok.view(StudentFilesUploadPage) |
---|
[7127] | 372 | grok.require('waeup.uploadStudentFile') |
---|
[7112] | 373 | |
---|
[7097] | 374 | class BirthCertificateDisplay(FileDisplay): |
---|
[7112] | 375 | """Birth Certificate display viewlet. |
---|
[7097] | 376 | """ |
---|
| 377 | grok.order(1) |
---|
| 378 | label = u'Birth Certificate:' |
---|
[7127] | 379 | title = u'Birth Certificate Scan' |
---|
[7123] | 380 | download_name = u'birth_certificate' |
---|
[7097] | 381 | |
---|
[7127] | 382 | class BirthCertificateUpload(FileUpload): |
---|
[7097] | 383 | """Birth Certificate upload viewlet. |
---|
| 384 | """ |
---|
| 385 | grok.order(1) |
---|
[7123] | 386 | label = u'Birth Certificate:' |
---|
[7127] | 387 | title = u'Birth Certificate Scan' |
---|
[7097] | 388 | mus = 1024 * 150 |
---|
[7123] | 389 | download_name = u'birth_certificate' |
---|
[7134] | 390 | tab_redirect = '#tab-2' |
---|
[7097] | 391 | |
---|
[7111] | 392 | class AcceptanceLetterDisplay(FileDisplay): |
---|
[7112] | 393 | """Acceptance Letter display viewlet. |
---|
[7111] | 394 | """ |
---|
| 395 | grok.order(1) |
---|
| 396 | label = u'Acceptance Letter:' |
---|
[7127] | 397 | title = u'Acceptance Letter Scan' |
---|
[7123] | 398 | download_name = u'acceptance_letter' |
---|
[7111] | 399 | |
---|
[7127] | 400 | class AcceptanceLetterUpload(FileUpload): |
---|
[7111] | 401 | """AcceptanceLetter upload viewlet. |
---|
| 402 | """ |
---|
[7112] | 403 | grok.order(2) |
---|
[7123] | 404 | label = u'Acceptance Letter:' |
---|
[7127] | 405 | title = u'Acceptance Letter Scan' |
---|
[7111] | 406 | mus = 1024 * 150 |
---|
[7123] | 407 | download_name = u'acceptance_letter' |
---|
[7134] | 408 | tab_redirect = '#tab-2' |
---|
[7111] | 409 | |
---|
[7097] | 410 | class Image(grok.View): |
---|
[7106] | 411 | """Renders jpeg images for students. |
---|
[7097] | 412 | """ |
---|
[7106] | 413 | grok.baseclass() |
---|
[7097] | 414 | grok.name('none.jpg') |
---|
[7112] | 415 | grok.context(IStudentClearance) |
---|
[7097] | 416 | grok.require('waeup.viewStudent') |
---|
[7106] | 417 | download_name = u'none.jpg' |
---|
[7097] | 418 | |
---|
| 419 | def render(self): |
---|
| 420 | # A filename chooser turns a context into a filename suitable |
---|
| 421 | # for file storage. |
---|
| 422 | image = getUtility(IExtFileStore).getFileByContext( |
---|
[7106] | 423 | self.context, attr=self.download_name) |
---|
[7097] | 424 | if image is None: |
---|
| 425 | # show placeholder image |
---|
[7123] | 426 | self.response.setHeader('Content-Type', 'image/jpeg') |
---|
[7097] | 427 | return open(DEFAULT_IMAGE_PATH, 'rb').read() |
---|
[7123] | 428 | dummy,ext = os.path.splitext(image.name) |
---|
| 429 | if ext == '.jpg': |
---|
| 430 | self.response.setHeader('Content-Type', 'image/jpeg') |
---|
| 431 | elif ext == '.png': |
---|
| 432 | self.response.setHeader('Content-Type', 'image/png') |
---|
| 433 | elif ext == '.pdf': |
---|
| 434 | self.response.setHeader('Content-Type', 'application/pdf') |
---|
| 435 | elif ext == '.tif': |
---|
| 436 | self.response.setHeader('Content-Type', 'image/tiff') |
---|
[7097] | 437 | return image |
---|
| 438 | |
---|
[7112] | 439 | class Passport(Image): |
---|
| 440 | """Renders jpeg passport picture. |
---|
| 441 | """ |
---|
| 442 | grok.name('passport.jpg') |
---|
| 443 | download_name = u'passport.jpg' |
---|
| 444 | grok.context(IStudent) |
---|
| 445 | |
---|
[7097] | 446 | class BirthCertificateImage(Image): |
---|
[7111] | 447 | """Renders birth certificate jpeg scan. |
---|
[7097] | 448 | """ |
---|
[7123] | 449 | grok.name('birth_certificate') |
---|
| 450 | download_name = u'birth_certificate' |
---|
[7111] | 451 | |
---|
| 452 | class AcceptanceLetterImage(Image): |
---|
| 453 | """Renders acceptance letter jpeg scan. |
---|
| 454 | """ |
---|
[7123] | 455 | grok.name('acceptance_letter') |
---|
| 456 | download_name = u'acceptance_letter' |
---|