source: main/waeup.kofa/trunk/src/waeup/kofa/students/viewlets.py @ 9820

Last change on this file since 9820 was 9814, checked in by Henrik Bettermann, 12 years ago

Add clearance_enabled field to ISessionConfiguration. Clearance can now be disabled for certain sessions via this switch.

  • Property svn:keywords set to Id
File size: 29.4 KB
RevLine 
[9138]13## $Id: viewlets.py 9814 2012-12-21 08:51:44Z henrik $
[7191]2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
[7106]18import os
[6642]19import grok
[7097]20from zope.component import getUtility
[6642]21from zope.interface import Interface
[7741]22from zope.i18n import translate
[7811]23from waeup.kofa.interfaces import (
[8467]24    IKofaObject, IExtFileStore, IFileStoreNameChooser)
[7811]25from waeup.kofa.interfaces import MessageFactory as _
26from waeup.kofa.utils.helpers import string_from_bytes, file_size
27from waeup.kofa.browser import DEFAULT_IMAGE_PATH
28from waeup.kofa.browser.viewlets import (
[7591]29    PrimaryNavTab, ManageActionButton, AddActionButton)
[8106]30from waeup.kofa.browser.layout import (
[8467]31    default_primary_nav_template, default_filedisplay_template,
32    default_fileupload_template)
33from waeup.kofa.students.workflow import (
34    ADMITTED, PAID, REQUESTED, RETURNING, CLEARED, REGISTERED,
35    VALIDATED)
[7811]36from waeup.kofa.students.browser import (
[9814]37    clearance_disabled_message,
[8735]38    StudentClearanceManageFormPage,
[8467]39    StudentBaseManageFormPage, StudentFilesUploadPage,
40    ExportPDFClearanceSlipPage, StudentsContainerPage,
[7591]41    StudentsContainerManagePage, StudentBaseDisplayFormPage,
42    StudentClearanceDisplayFormPage, StudentPersonalDisplayFormPage,
43    StudyCourseDisplayFormPage, StudyLevelDisplayFormPage,
44    CourseTicketDisplayFormPage, OnlinePaymentDisplayFormPage,
[8119]45    AccommodationManageFormPage, BedTicketDisplayFormPage,
[9517]46    StudentClearanceEditFormPage, StudentPersonalEditFormPage,
47    PaymentsManageFormPage)
[7811]48from waeup.kofa.students.interfaces import (
[8467]49    IStudentsContainer, IStudent, IStudentStudyCourse, IStudentAccommodation,
50    IStudentStudyLevel, ICourseTicket, IStudentOnlinePayment, IBedTicket,
[9517]51    IStudentPaymentsContainer,
[7591]52    )
[8467]53from waeup.kofa.utils.helpers import get_fileformat
[6642]54
[7819]55grok.context(IKofaObject) # Make IKofaObject the default context
[6687]56grok.templatedir('browser_templates')
[6642]57
[7123]58ALLOWED_FILE_EXTENSIONS = ('jpg', 'png', 'pdf', 'tif')
59
[6687]60class StudentManageSidebar(grok.ViewletManager):
61    grok.name('left_studentmanage')
[6642]62
[6687]63class StudentManageLink(grok.Viewlet):
[6660]64    """A link displayed in the student box which shows up for StudentNavigation
[6642]65    objects.
66
67    """
68    grok.baseclass()
[6687]69    grok.viewletmanager(StudentManageSidebar)
[7819]70    grok.context(IKofaObject)
[6642]71    grok.view(Interface)
72    grok.order(5)
[6660]73    grok.require('waeup.viewStudent')
[6642]74
75    link = 'index'
[7738]76    text = _(u'Base Data')
[6642]77
78    def render(self):
[8736]79        url = self.view.url(self.context.student, self.link)
[7833]80        # Here we know that the cookie has been set
81        lang = self.request.cookies.get('kofa.language')
[7811]82        text = translate(self.text, 'waeup.kofa',
[7741]83            target_language=lang)
[7459]84        return u'<li><a href="%s">%s</a></li>' % (
[7741]85                url, text)
[6642]86
[7459]87class StudentManageApplicationLink(StudentManageLink):
[6687]88    grok.order(1)
[7351]89    link = 'application_slip'
[7738]90    text = _(u'Application Slip')
[7340]91
92    def render(self):
[7351]93        slip = getUtility(IExtFileStore).getFileByContext(
[8736]94            self.context.student, attr=self.link)
[7351]95        if slip:
[9397]96            url = self.view.url(self.context.student,self.link)
[7459]97            return u'<li><a href="%s">%s</a></li>' % (
[7340]98                    url, self.text)
99        return ''
100
[7459]101class StudentManageBaseLink(StudentManageLink):
102    grok.order(2)
103    link = 'index'
[7738]104    text = _(u'Base Data')
[7459]105
[6687]106class StudentManageClearanceLink(StudentManageLink):
[7340]107    grok.order(3)
[6687]108    link = 'view_clearance'
[7738]109    text = _(u'Clearance Data')
[6687]110
111class StudentManagePersonalLink(StudentManageLink):
[7340]112    grok.order(4)
[6687]113    link = 'view_personal'
[7738]114    text = _(u'Personal Data')
[6687]115
116class StudentManageStudyCourseLink(StudentManageLink):
[7340]117    grok.order(5)
[6687]118    link = 'studycourse'
[7738]119    text = _(u'Study Course')
[6687]120
121class StudentManagePaymentsLink(StudentManageLink):
[7340]122    grok.order(6)
[7181]123    grok.require('waeup.payStudent')
[6687]124    link = 'payments'
[7738]125    text = _(u'Payments')
[6687]126
127class StudentManageAccommodationLink(StudentManageLink):
[7340]128    grok.order(7)
[7181]129    grok.require('waeup.handleAccommodation')
[6687]130    link = 'accommodation'
[7738]131    text = _(u'Accommodation')
[6687]132
133class StudentManageHistoryLink(StudentManageLink):
[7340]134    grok.order(8)
[6687]135    link = 'history'
[7738]136    text = _(u'History')
[6687]137
[7591]138
139class StudentsContainerManageActionButton(ManageActionButton):
140    grok.order(1)
141    grok.context(IStudentsContainer)
142    grok.view(StudentsContainerPage)
143    grok.require('waeup.manageStudent')
[7738]144    text = _('Manage student section')
[7591]145
146class StudentsContainerAddActionButton(AddActionButton):
147    grok.order(1)
148    grok.context(IStudentsContainer)
149    grok.view(StudentsContainerManagePage)
150    grok.require('waeup.manageStudent')
[7738]151    text = _('Add student')
[7591]152    target = 'addstudent'
153
154class ContactActionButton(ManageActionButton):
[9191]155    grok.order(5)
[7591]156    grok.context(IStudent)
157    grok.view(StudentBaseDisplayFormPage)
158    grok.require('waeup.manageStudent')
159    icon = 'actionicon_mail.png'
[7738]160    text = _('Send email')
[7591]161    target = 'contactstudent'
162
163class StudentBaseManageActionButton(ManageActionButton):
164    grok.order(1)
165    grok.context(IStudent)
166    grok.view(StudentBaseDisplayFormPage)
167    grok.require('waeup.manageStudent')
[7738]168    text = _('Manage')
[7591]169    target = 'manage_base'
170
[9273]171class StudentTrigTransActionButton(ManageActionButton):
172    grok.order(2)
173    grok.context(IStudent)
174    grok.view(StudentBaseDisplayFormPage)
175    grok.require('waeup.triggerTransition')
176    icon = 'actionicon_trigtrans.png'
177    text = _(u'Trigger transition')
178    target = 'trigtrans'
179
[9338]180class StudentLoginAsActionButton(ManageActionButton):
181    grok.order(3)
182    grok.context(IStudent)
183    grok.view(StudentBaseDisplayFormPage)
184    grok.require('waeup.loginAsStudent')
185    icon = 'actionicon_mask.png'
186    text = _(u'Login as student')
187    target = 'loginasstep1'
188
[9191]189class AdmissionSlipActionButton(ManageActionButton):
190    grok.order(4)
191    grok.context(IStudent)
192    grok.view(StudentBaseDisplayFormPage)
193    grok.require('waeup.viewStudent')
194    icon = 'actionicon_pdf.png'
195    text = _('Download admission letter')
196    target = 'admission_slip.pdf'
197
[9138]198class StudentTransfernButton(ManageActionButton):
[9191]199    grok.order(6)
[9124]200    grok.context(IStudent)
201    grok.view(StudentBaseDisplayFormPage)
202    grok.require('waeup.manageStudent')
[9138]203    text = _('Transfer student')
204    target = 'transfer'
205    icon = 'actionicon_redo.png'
206
207class StudentDeactivateActionButton(ManageActionButton):
[9191]208    grok.order(7)
[9138]209    grok.context(IStudent)
210    grok.view(StudentBaseDisplayFormPage)
211    grok.require('waeup.manageStudent')
[9124]212    text = _('Deactivate account')
213    target = 'deactivate'
214    icon = 'actionicon_traffic_lights_red.png'
215
216    @property
217    def target_url(self):
218        if self.context.suspended:
219            return ''
220        return self.view.url(self.view.context, self.target)
221
[9145]222    @property
223    def onclick(self):
224        return "return window.confirm(%s);" % _(
225            "'A history message will be added. Are you sure?'")
226
[9124]227class StudentActivateActionButton(ManageActionButton):
[9191]228    grok.order(7)
[9124]229    grok.context(IStudent)
230    grok.view(StudentBaseDisplayFormPage)
231    grok.require('waeup.manageStudent')
232    text = _('Activate account')
233    target = 'activate'
234    icon = 'actionicon_traffic_lights_green.png'
235
236    @property
237    def target_url(self):
238        if not self.context.suspended:
239            return ''
240        return self.view.url(self.view.context, self.target)
241
[9145]242    @property
243    def onclick(self):
244        return "return window.confirm(%s);" % _(
245            "'A history message will be added. Are you sure?'")
246
[7591]247class StudentClearanceManageActionButton(ManageActionButton):
248    grok.order(1)
249    grok.context(IStudent)
250    grok.view(StudentClearanceDisplayFormPage)
251    grok.require('waeup.manageStudent')
[7738]252    text = _('Manage')
[8119]253    target = 'manage_clearance'
[7591]254
255class StudentClearActionButton(ManageActionButton):
256    grok.order(2)
257    grok.context(IStudent)
258    grok.view(StudentClearanceDisplayFormPage)
259    grok.require('waeup.clearStudent')
[7738]260    text = _('Clear student')
[7591]261    target = 'clear'
262    icon = 'actionicon_accept.png'
263
264    @property
265    def target_url(self):
[9814]266        if clearance_disabled_message(self.context):
267            return ''
[7591]268        if self.context.state != REQUESTED:
269            return ''
270        return self.view.url(self.view.context, self.target)
271
272class StudentRejectClearanceActionButton(ManageActionButton):
273    grok.order(3)
274    grok.context(IStudent)
275    grok.view(StudentClearanceDisplayFormPage)
276    grok.require('waeup.clearStudent')
[7738]277    text = _('Reject clearance')
[7591]278    target = 'reject_clearance'
279    icon = 'actionicon_reject.png'
280
281    @property
282    def target_url(self):
[9814]283        if clearance_disabled_message(self.context):
284            return ''
[7591]285        if self.context.state not in (REQUESTED, CLEARED):
286            return ''
287        return self.view.url(self.view.context, self.target)
288
289class ClearanceSlipActionButton(ManageActionButton):
290    grok.order(4)
291    grok.context(IStudent)
292    grok.view(StudentClearanceDisplayFormPage)
293    grok.require('waeup.viewStudent')
294    icon = 'actionicon_pdf.png'
[7738]295    text = _('Download clearance slip')
[9452]296    target = 'clearance_slip.pdf'
[7591]297
[8119]298class ClearanceViewActionButton(ManageActionButton):
299    grok.order(1)
300    grok.context(IStudent)
301    grok.view(StudentClearanceEditFormPage)
302    grok.require('waeup.viewStudent')
303    icon = 'actionicon_view.png'
304    text = _('View')
305    target = 'view_clearance'
306
307class PersonalViewActionButton(ManageActionButton):
308    grok.order(1)
309    grok.context(IStudent)
310    grok.view(StudentPersonalEditFormPage)
311    grok.require('waeup.viewStudent')
312    icon = 'actionicon_view.png'
313    text = _('View')
314    target = 'view_personal'
315
[8903]316class StudentPersonalManageActionButton(ManageActionButton):
[7591]317    grok.order(1)
318    grok.context(IStudent)
319    grok.view(StudentPersonalDisplayFormPage)
[8903]320    grok.require('waeup.manageStudent')
321    text = _('Manage')
322    target = 'manage_personal'
323
324class StudentPersonalEditActionButton(ManageActionButton):
325    grok.order(2)
326    grok.context(IStudent)
327    grok.view(StudentPersonalDisplayFormPage)
328    grok.require('waeup.handleStudent')
[7738]329    text = _('Edit')
[7591]330    target = 'edit_personal'
331
332class StudyCourseManageActionButton(ManageActionButton):
333    grok.order(1)
334    grok.context(IStudentStudyCourse)
335    grok.view(StudyCourseDisplayFormPage)
336    grok.require('waeup.manageStudent')
[7738]337    text = _('Manage')
[7591]338    target = 'manage'
339
[9138]340    @property
341    def target_url(self):
342        if self.context.is_current:
343            return self.view.url(self.view.context, self.target)
344        return False
345
[8078]346class StudyLevelManageActionButton(ManageActionButton):
[7591]347    grok.order(1)
348    grok.context(IStudentStudyLevel)
349    grok.view(StudyLevelDisplayFormPage)
350    grok.require('waeup.manageStudent')
[7738]351    text = _('Manage')
[7591]352    target = 'manage'
353
[9138]354    @property
355    def target_url(self):
356        is_current = self.context.__parent__.is_current
357        if not is_current:
358            return ''
359        return self.view.url(self.view.context, self.target)
360
[7591]361class StudentValidateCoursesActionButton(ManageActionButton):
362    grok.order(3)
363    grok.context(IStudentStudyLevel)
364    grok.view(StudyLevelDisplayFormPage)
365    grok.require('waeup.validateStudent')
[7738]366    text = _('Validate courses')
[7591]367    target = 'validate_courses'
368    icon = 'actionicon_accept.png'
369
370    @property
371    def target_url(self):
[9138]372        is_current = self.context.__parent__.is_current
[8736]373        if self.context.student.state != REGISTERED or \
[9138]374            str(self.context.__parent__.current_level) != self.context.__name__ or\
375            not is_current:
[7591]376            return ''
377        return self.view.url(self.view.context, self.target)
378
379class StudentRejectCoursesActionButton(ManageActionButton):
380    grok.order(4)
381    grok.context(IStudentStudyLevel)
382    grok.view(StudyLevelDisplayFormPage)
383    grok.require('waeup.validateStudent')
[7738]384    text = _('Reject courses')
[7591]385    target = 'reject_courses'
386    icon = 'actionicon_reject.png'
387
388    @property
389    def target_url(self):
[9138]390        is_current = self.context.__parent__.is_current
[8736]391        if self.context.student.state not in (VALIDATED, REGISTERED) or \
[9138]392            str(self.context.__parent__.current_level) != self.context.__name__ or\
393            not is_current:
[7591]394            return ''
395        return self.view.url(self.view.context, self.target)
396
[8078]397class CourseRegistrationSlipActionButton(ManageActionButton):
398    grok.order(5)
399    grok.context(IStudentStudyLevel)
400    grok.view(StudyLevelDisplayFormPage)
401    grok.require('waeup.viewStudent')
402    icon = 'actionicon_pdf.png'
403    text = _('Download course registration slip')
[9452]404    target = 'course_registration_slip.pdf'
[8078]405
[9138]406    @property
407    def target_url(self):
408        is_current = self.context.__parent__.is_current
409        if not is_current:
410            return ''
411        return self.view.url(self.view.context, self.target)
412
[7591]413class CourseTicketManageActionButton(ManageActionButton):
414    grok.order(1)
415    grok.context(ICourseTicket)
416    grok.view(CourseTicketDisplayFormPage)
417    grok.require('waeup.manageStudent')
[7738]418    text = _('Manage')
[7591]419    target = 'manage'
420
421#class OnlinePaymentManageActionButton(ManageActionButton):
422#    grok.order(1)
423#    grok.context(IStudentPaymentsContainer)
424#    grok.view(PaymentsDisplayFormPage)
425#    grok.require('waeup.manageStudent')
426#    text = 'Manage payments'
427#    target = 'manage'
428
429class PaymentReceiptActionButton(ManageActionButton):
[9070]430    grok.order(9) # This button should always be the last one.
[7591]431    grok.context(IStudentOnlinePayment)
432    grok.view(OnlinePaymentDisplayFormPage)
433    grok.require('waeup.viewStudent')
434    icon = 'actionicon_pdf.png'
[8262]435    text = _('Download payment slip')
436    target = 'payment_slip.pdf'
[7591]437
438    @property
439    def target_url(self):
[8262]440        #if self.context.p_state != 'paid':
441        #    return ''
[7591]442        return self.view.url(self.view.context, self.target)
443
[8420]444class ApprovePaymentActionButton(ManageActionButton):
[9070]445    grok.order(8)
[7591]446    grok.context(IStudentOnlinePayment)
447    grok.view(OnlinePaymentDisplayFormPage)
[8420]448    grok.require('waeup.managePortal')
[8435]449    icon = 'actionicon_accept.png'
[8420]450    text = _('Approve payment')
451    target = 'approve'
[7591]452
453    @property
454    def target_url(self):
[7888]455        if self.context.p_state == 'paid':
[7591]456            return ''
457        return self.view.url(self.view.context, self.target)
458
459class AddBedTicketActionButton(ManageActionButton):
460    grok.order(1)
461    grok.context(IStudentAccommodation)
462    grok.view(AccommodationManageFormPage)
463    grok.require('waeup.handleAccommodation')
464    icon = 'actionicon_home.png'
[7738]465    text = _('Book accommodation')
[7591]466    target = 'add'
467
468class BedTicketSlipActionButton(ManageActionButton):
469    grok.order(1)
470    grok.context(IBedTicket)
471    grok.view(BedTicketDisplayFormPage)
472    grok.require('waeup.handleAccommodation')
473    icon = 'actionicon_pdf.png'
[7738]474    text = _('Download bed allocation slip')
[9452]475    target = 'bed_allocation_slip.pdf'
[7591]476
477class RelocateStudentActionButton(ManageActionButton):
478    grok.order(2)
479    grok.context(IBedTicket)
480    grok.view(BedTicketDisplayFormPage)
481    grok.require('waeup.manageHostels')
482    icon = 'actionicon_reload.png'
[7738]483    text = _('Relocate student')
[7591]484    target = 'relocate'
485
486class StudentBaseActionButton(ManageActionButton):
487    grok.order(1)
488    grok.context(IStudent)
489    grok.view(StudentBaseDisplayFormPage)
490    grok.require('waeup.handleStudent')
[7876]491    text = _('Edit')
[7591]492    target = 'edit_base'
493
494class StudentPasswordActionButton(ManageActionButton):
495    grok.order(2)
496    grok.context(IStudent)
497    grok.view(StudentBaseDisplayFormPage)
498    grok.require('waeup.handleStudent')
499    icon = 'actionicon_key.png'
[7738]500    text = _('Change password')
[7591]501    target = 'change_password'
502
503class StudentPassportActionButton(ManageActionButton):
504    grok.order(3)
505    grok.context(IStudent)
506    grok.view(StudentBaseDisplayFormPage)
507    grok.require('waeup.handleStudent')
508    icon = 'actionicon_portrait.png'
[7738]509    text = _('Change portrait')
[7591]510    target = 'change_portrait'
511
512    @property
513    def target_url(self):
[7671]514        if self.context.state != ADMITTED:
[7591]515            return ''
516        return self.view.url(self.view.context, self.target)
517
518class StudentClearanceStartActionButton(ManageActionButton):
519    grok.order(1)
520    grok.context(IStudent)
521    grok.view(StudentClearanceDisplayFormPage)
522    grok.require('waeup.handleStudent')
523    icon = 'actionicon_start.gif'
[7738]524    text = _('Start clearance')
[7591]525    target = 'start_clearance'
526
527    @property
528    def target_url(self):
[7671]529        if self.context.state != ADMITTED:
[7591]530            return ''
531        return self.view.url(self.view.context, self.target)
532
533class StudentClearanceEditActionButton(ManageActionButton):
534    grok.order(1)
535    grok.context(IStudent)
536    grok.view(StudentClearanceDisplayFormPage)
537    grok.require('waeup.handleStudent')
[7738]538    text = _('Edit')
[7591]539    target = 'cedit'
540
541    @property
542    def target_url(self):
543        if self.context.clearance_locked:
544            return ''
545        return self.view.url(self.view.context, self.target)
546
[8476]547class StartSessionActionButton(ManageActionButton):
[7591]548    grok.order(1)
549    grok.context(IStudentStudyCourse)
550    grok.view(StudyCourseDisplayFormPage)
551    grok.require('waeup.handleStudent')
552    icon = 'actionicon_start.gif'
[8920]553    text = _('Start new session')
[8471]554    target = 'start_session'
[7591]555
556    @property
557    def target_url(self):
[9138]558        if self.context.next_session_allowed and self.context.is_current:
[8471]559            return self.view.url(self.view.context, self.target)
560        return False
[7591]561
562class AddStudyLevelActionButton(AddActionButton):
563    grok.order(1)
564    grok.context(IStudentStudyCourse)
565    grok.view(StudyCourseDisplayFormPage)
566    grok.require('waeup.handleStudent')
[7738]567    text = _('Add course list')
[7591]568    target = 'add'
569
570    @property
571    def target_url(self):
[8736]572        student = self.view.context.student
[7671]573        condition1 = student.state != PAID
[7591]574        condition2 = str(student['studycourse'].current_level) in \
575            self.view.context.keys()
[9138]576        condition3 = not self.context.is_current
577        if condition1 or condition2 or condition3:
[7591]578            return ''
579        return self.view.url(self.view.context, self.target)
580
581class StudyLevelEditActionButton(ManageActionButton):
[8078]582    grok.order(2)
[7591]583    grok.context(IStudentStudyLevel)
584    grok.view(StudyLevelDisplayFormPage)
585    grok.require('waeup.handleStudent')
[8920]586    text = _('Edit course list')
[7591]587    target = 'edit'
588
589    @property
590    def target_url(self):
[8736]591        student = self.view.context.student
[9257]592        condition1 = student.state == PAID
593        condition2 = self.view.context.is_current_level
[9138]594        is_current = self.context.__parent__.is_current
[9257]595        if condition1 and condition2 and is_current:
596            return self.view.url(self.view.context, self.target)
597        return ''
[7591]598
[9517]599class AddPaymentActionButton(AddActionButton):
600    grok.order(1)
601    grok.context(IStudentPaymentsContainer)
602    grok.view(PaymentsManageFormPage)
603    grok.require('waeup.payStudent')
604    text = _('Add current session payment ticket')
605    target = 'addop'
606
607class AddPreviousPaymentActionButton(AddActionButton):
608    grok.order(2)
609    grok.context(IStudentPaymentsContainer)
610    grok.view(PaymentsManageFormPage)
611    grok.require('waeup.payStudent')
612    text = _('Add previous session payment ticket')
613    target = 'addpp'
614
615    @property
616    def target_url(self):
617        student = self.view.context.student
[9521]618        if student.before_payment:
[9517]619            return ''
620        return self.view.url(self.view.context, self.target)
621
[7184]622class StudentsTab(PrimaryNavTab):
623    """Students tab in primary navigation.
624    """
625
[7819]626    grok.context(IKofaObject)
[7184]627    grok.order(4)
[7240]628    grok.require('waeup.viewStudentsTab')
[7184]629
630    pnav = 4
[7674]631    tab_title = _(u'Students')
[7184]632
633    @property
634    def link_target(self):
635        return self.view.application_url('students')
636
[6687]637class PrimaryStudentNavManager(grok.ViewletManager):
638    """Viewlet manager for the primary navigation tab.
639    """
640    grok.name('primary_nav_student')
641
642class PrimaryStudentNavTab(grok.Viewlet):
643    """Base for primary student nav tabs.
644    """
645    grok.baseclass()
646    grok.viewletmanager(PrimaryStudentNavManager)
[7243]647    template = default_primary_nav_template
[6687]648    grok.order(1)
[7184]649    grok.require('waeup.Authenticated')
[8467]650    pnav = 0
[6687]651    tab_title = u'Some Text'
652
653    @property
654    def link_target(self):
655        return self.view.application_url()
656
657    @property
658    def active(self):
659        view_pnav = getattr(self.view, 'pnav', 0)
660        if view_pnav == self.pnav:
661            return 'active'
662        return ''
663
[7240]664class MyStudentDataTab(PrimaryStudentNavTab):
[7459]665    """MyData dropdown tab in primary navigation.
[6687]666    """
667    grok.order(3)
[7240]668    grok.require('waeup.viewMyStudentDataTab')
[7459]669    grok.template('mydatadropdowntabs')
[6687]670    pnav = 4
[7738]671    tab_title = _(u'My Data')
[6687]672
673    @property
[7459]674    def active(self):
675        view_pnav = getattr(self.view, 'pnav', 0)
676        if view_pnav == self.pnav:
677            return 'active dropdown'
678        return 'dropdown'
[7097]679
[7459]680    @property
681    def targets(self):
[9180]682        student = grok.getSite()['students'][self.request.principal.id]
683        student_url = self.view.url(student)
[9178]684        app_slip = getUtility(IExtFileStore).getFileByContext(
[9180]685            student, 'application_slip')
[7459]686        targets = []
[9178]687        if app_slip:
688            targets = [{'url':student_url + '/application_slip', 'title':'Application Slip'},]
[7459]689        targets += [
690            {'url':student_url, 'title':'Base Data'},
[8467]691            {'url':student_url + '/view_clearance',
692             'title':_('Clearance Data')},
[7738]693            {'url':student_url + '/view_personal', 'title':_('Personal Data')},
694            {'url':student_url + '/studycourse', 'title':_('Study Course')},
695            {'url':student_url + '/payments', 'title':_('Payments')},
[8467]696            {'url':student_url + '/accommodation',
697             'title':_('Accommodation Data')},
[7738]698            {'url':student_url + '/history', 'title':_('History')},
[7459]699            ]
700        return targets
701
[7107]702def handle_file_delete(context, view, download_name):
703    """Handle deletion of student file.
704
705    """
706    store = getUtility(IExtFileStore)
707    store.deleteFileByContext(context, attr=download_name)
[8735]708    context.writeLogMessage(view, 'deleted: %s' % download_name)
[7738]709    view.flash(_('${a} deleted.', mapping = {'a':download_name}))
[7107]710    return
711
[7106]712def handle_file_upload(upload, context, view, max_size, download_name=None):
713    """Handle upload of student file.
[7097]714
715    Returns `True` in case of success or `False`.
716
717    Please note that file pointer passed in (`upload`) most probably
718    points to end of file when leaving this function.
719    """
[7106]720    # Check some file requirements first
[8467]721    size = file_size(upload)
722    if size > max_size:
723        view.flash(_('Uploaded file is too big.'))
[7106]724        return False
[8467]725    upload.seek(0) # file pointer moved when determining size
726    file_format = get_fileformat(None, upload.read(512))
727    upload.seek(0) # same here
728    if file_format is None:
729        view.flash(_('Could not determine file type.'))
[7106]730        return False
731    basename, expected_ext = os.path.splitext(download_name)
[7123]732    if expected_ext:
[8467]733        if '.' + file_format != expected_ext:
[7738]734            view.flash(_('${a} file extension expected.',
[8467]735                mapping = {'a':expected_ext[1:]}))
[7123]736            return False
737    else:
[8467]738        if not file_format in ALLOWED_FILE_EXTENSIONS:
[7123]739            view.flash(
[8467]740                _('Only the following extensions are allowed: ${a}',
[7738]741                mapping = {'a':', '.join(ALLOWED_FILE_EXTENSIONS)}))
[7123]742            return False
[8467]743        download_name += '.' + file_format
[7097]744    store = getUtility(IExtFileStore)
[7106]745    file_id = IFileStoreNameChooser(context).chooseName(attr=download_name)
[7097]746    store.createFile(file_id, upload)
[8735]747    context.writeLogMessage(view, 'uploaded: %s (%s)' % (
[8467]748        download_name,upload.filename))
[7738]749    view.flash(_('File ${a} uploaded.', mapping = {'a':download_name}))
[7097]750    return True
751
752class FileManager(grok.ViewletManager):
753    """Viewlet manager for uploading files, preferably scanned images.
754    """
755    grok.name('files')
756
757class FileDisplay(grok.Viewlet):
758    """Base file display viewlet.
759    """
760    grok.baseclass()
[7993]761    grok.context(IStudent)
[7097]762    grok.viewletmanager(FileManager)
763    grok.view(StudentClearanceDisplayFormPage)
[8106]764    template = default_filedisplay_template
[7097]765    grok.order(1)
766    grok.require('waeup.viewStudent')
[7738]767    label = _(u'File')
768    title = _(u'Scan')
[7106]769    download_name = u'filename.jpg'
[7097]770
[7107]771    @property
772    def file_exists(self):
773        image = getUtility(IExtFileStore).getFileByContext(
774            self.context, attr=self.download_name)
775        if image:
776            return True
777        else:
778            return False
779
[7097]780class FileUpload(FileDisplay):
781    """Base upload viewlet.
782    """
783    grok.baseclass()
[7993]784    grok.context(IStudent)
[7097]785    grok.viewletmanager(FileManager)
786    grok.view(StudentClearanceManageFormPage)
[8106]787    template = default_fileupload_template
[7127]788    grok.require('waeup.uploadStudentFile')
[7491]789    tab_redirect = '?tab2'
[7097]790    mus = 1024 * 150
[7735]791    upload_button =_('Upload new file')
792    delete_button = _('Delete attachment')
[8135]793    show_viewlet = True
[7097]794
[7117]795    @property
796    def input_name(self):
797        return "%s" % self.__name__
798
[7097]799    def update(self):
800        self.max_upload_size = string_from_bytes(self.mus)
[7117]801        delete_button = self.request.form.get(
802            'delete_%s' % self.input_name, None)
803        upload_button = self.request.form.get(
804            'upload_%s' % self.input_name, None)
[7108]805        if delete_button:
[7107]806            handle_file_delete(
807                context=self.context, view=self.view,
808                download_name=self.download_name)
809            self.view.redirect(
[7134]810                self.view.url(
811                    self.context, self.view.__name__) + self.tab_redirect)
[7107]812            return
[7108]813        if upload_button:
814            upload = self.request.form.get(self.input_name, None)
815            if upload:
816                # We got a fresh upload
[7111]817                handle_file_upload(upload,
818                    self.context, self.view, self.mus, self.download_name)
819                self.view.redirect(
[7134]820                    self.view.url(
821                        self.context, self.view.__name__) + self.tab_redirect)
[7117]822            else:
[7738]823                self.view.flash(_('No local file selected.'))
[7117]824                self.view.redirect(
[7134]825                    self.view.url(
826                        self.context, self.view.__name__) + self.tab_redirect)
[7097]827        return
828
[7112]829class PassportDisplay(FileDisplay):
830    """Passport display viewlet.
831    """
832    grok.order(1)
833    grok.context(IStudent)
834    grok.view(StudentBaseDisplayFormPage)
835    grok.require('waeup.viewStudent')
836    grok.template('imagedisplay')
[7738]837    label = _(u'Passport Picture')
[7112]838    download_name = u'passport.jpg'
839
840class PassportUploadManage(FileUpload):
841    """Passport upload viewlet for officers.
842    """
843    grok.order(1)
844    grok.context(IStudent)
845    grok.view(StudentBaseManageFormPage)
[7136]846    grok.require('waeup.manageStudent')
[7112]847    grok.template('imageupload')
[7738]848    label = _(u'Passport Picture (jpg only)')
[7112]849    mus = 1024 * 50
850    download_name = u'passport.jpg'
[7491]851    tab_redirect = '?tab2'
[7112]852
853class PassportUploadEdit(PassportUploadManage):
854    """Passport upload viewlet for students.
855    """
[7114]856    grok.view(StudentFilesUploadPage)
[7127]857    grok.require('waeup.uploadStudentFile')
[7112]858
[7097]859class BirthCertificateDisplay(FileDisplay):
[7112]860    """Birth Certificate display viewlet.
[7097]861    """
862    grok.order(1)
[7738]863    label = _(u'Birth Certificate')
864    title = _(u'Birth Certificate Scan')
[7123]865    download_name = u'birth_certificate'
[7097]866
[7280]867class BirthCertificateSlip(BirthCertificateDisplay):
868    grok.view(ExportPDFClearanceSlipPage)
869
[7127]870class BirthCertificateUpload(FileUpload):
[7097]871    """Birth Certificate upload viewlet.
872    """
873    grok.order(1)
[7738]874    label = _(u'Birth Certificate')
875    title = _(u'Birth Certificate Scan')
[7097]876    mus = 1024 * 150
[7123]877    download_name = u'birth_certificate'
[7491]878    tab_redirect = '?tab2'
[7097]879
880class Image(grok.View):
[7351]881    """Renders images for students.
[7097]882    """
[7106]883    grok.baseclass()
[7097]884    grok.name('none.jpg')
[7993]885    grok.context(IStudent)
[7097]886    grok.require('waeup.viewStudent')
[7106]887    download_name = u'none.jpg'
[7097]888
889    def render(self):
890        # A filename chooser turns a context into a filename suitable
891        # for file storage.
892        image = getUtility(IExtFileStore).getFileByContext(
[7106]893            self.context, attr=self.download_name)
[7097]894        if image is None:
895            # show placeholder image
[7123]896            self.response.setHeader('Content-Type', 'image/jpeg')
[7097]897            return open(DEFAULT_IMAGE_PATH, 'rb').read()
[7123]898        dummy,ext = os.path.splitext(image.name)
899        if ext == '.jpg':
900            self.response.setHeader('Content-Type', 'image/jpeg')
901        elif ext == '.png':
902            self.response.setHeader('Content-Type', 'image/png')
903        elif ext == '.pdf':
904            self.response.setHeader('Content-Type', 'application/pdf')
905        elif ext == '.tif':
906            self.response.setHeader('Content-Type', 'image/tiff')
[7097]907        return image
908
[7112]909class Passport(Image):
910    """Renders jpeg passport picture.
911    """
912    grok.name('passport.jpg')
913    download_name = u'passport.jpg'
914    grok.context(IStudent)
915
[7351]916class ApplicationSlipImage(Image):
917    """Renders application slip scan.
918    """
919    grok.name('application_slip')
920    download_name = u'application_slip'
921
[7097]922class BirthCertificateImage(Image):
[7351]923    """Renders birth certificate scan.
[7097]924    """
[7123]925    grok.name('birth_certificate')
926    download_name = u'birth_certificate'
Note: See TracBrowser for help on using the repository browser.