source: main/waeup.sirp/trunk/src/waeup/sirp/students/browser.py @ 7408

Last change on this file since 7408 was 7407, checked in by Henrik Bettermann, 13 years ago

Harmonize sendCredentials which can now be used for all kinds of users.

  • Property svn:keywords set to Id
File size: 77.0 KB
RevLine 
[7191]1## $Id: browser.py 7407 2011-12-20 11:05:07Z henrik $
2##
[6621]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##
18"""UI components for students and related components.
19"""
[7006]20import sys
[6621]21import grok
[7275]22from urllib import urlencode
[6869]23from time import time
[7256]24from datetime import datetime
[7015]25from zope.event import notify
[6996]26from zope.catalog.interfaces import ICatalog
[7386]27from zope.component import queryUtility, getUtility, createObject
28from zope.formlib.textwidgets import BytesDisplayWidget
[7369]29#from hurry.query import Eq
30#from hurry.query.query import Query
[6621]31from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState
[6936]32from waeup.sirp.accesscodes import (
[6937]33    invalidate_accesscode, get_access_code, create_accesscode)
[6621]34from waeup.sirp.accesscodes.workflow import USED
35from waeup.sirp.browser import (
[7321]36    SIRPPage, SIRPEditFormPage, SIRPAddFormPage, SIRPDisplayFormPage,
[7369]37    ContactAdminForm, SIRPForm)
38from waeup.sirp.browser.interfaces import ICaptchaManager
[6621]39from waeup.sirp.browser.breadcrumbs import Breadcrumb
[7329]40from waeup.sirp.browser.resources import datepicker, datatable, tabs, warning
[6621]41from waeup.sirp.browser.viewlets import (
[7184]42    ManageActionButton, AddActionButton)
[7329]43from waeup.sirp.browser.layout import jsaction, JSAction
[7147]44from waeup.sirp.interfaces import (
[7358]45    ISIRPObject, IUserAccount, IExtFileStore, IPasswordValidator, IContactForm,
[7369]46    ISIRPUtils, IUniversity)
[6621]47from waeup.sirp.widgets.datewidget import (
[6869]48    FriendlyDateWidget, FriendlyDateDisplayWidget,
49    FriendlyDatetimeDisplayWidget)
[6912]50from waeup.sirp.university.vocabularies import study_modes
[6621]51from waeup.sirp.students.interfaces import (
[7144]52    IStudentsContainer, IStudent, IStudentClearance,
[6859]53    IStudentPersonal, IStudentBase, IStudentStudyCourse,
[6774]54    IStudentAccommodation, IStudentClearanceEdit, IStudentStudyLevel,
[6877]55    ICourseTicket, ICourseTicketAdd, IStudentPaymentsContainer,
[7369]56    IStudentOnlinePayment, IBedTicket, IStudentsUtils, IStudentChangePassword
[6621]57    )
[6626]58from waeup.sirp.students.catalog import search
[6992]59from waeup.sirp.students.workflow import (
[7158]60    CLEARANCE, REQUESTED, RETURNING, CLEARED, REGISTERED, VALIDATED)
[6795]61from waeup.sirp.students.studylevel import StudentStudyLevel, CourseTicket
[6775]62from waeup.sirp.students.vocabularies import StudyLevelSource
[6820]63from waeup.sirp.browser.resources import toggleall
[7340]64from waeup.sirp.applicants.interfaces import IApplicantBaseData
[6997]65from waeup.sirp.hostels.hostel import NOT_OCCUPIED
[6621]66
[6943]67def write_log_message(view, message):
68    ob_class = view.__implemented__.__name__.replace('waeup.sirp.','')
69    view.context.getStudent().loggerInfo(ob_class, message)
70    return
71
[7133]72# Save function used for save methods in pages
[6762]73def msave(view, **data):
74    changed_fields = view.applyData(view.context, **data)
[6771]75    # Turn list of lists into single list
76    if changed_fields:
77        changed_fields = reduce(lambda x,y: x+y, changed_fields.values())
[7205]78    # Inform catalog if certificate has changed
79    # (applyData does this only for the context)
80    if 'certificate' in changed_fields:
81        notify(grok.ObjectModifiedEvent(view.context.getStudent()))
[6771]82    fields_string = ' + '.join(changed_fields)
[6762]83    view.flash('Form has been saved.')
84    if fields_string:
[7108]85        write_log_message(view, 'saved: %s' % fields_string)
[6762]86    return
87
[7145]88def emit_lock_message(view):
[7133]89    view.flash('The requested form is locked (read-only).')
90    view.redirect(view.url(view.context))
91    return
92
[6629]93class StudentsBreadcrumb(Breadcrumb):
94    """A breadcrumb for the students container.
95    """
96    grok.context(IStudentsContainer)
[7318]97    title = 'Students'
[6629]98
[6818]99class StudentBreadcrumb(Breadcrumb):
100    """A breadcrumb for the student container.
101    """
102    grok.context(IStudent)
103
104    def title(self):
[7364]105        return self.context.display_fullname
[6818]106
[6635]107class SudyCourseBreadcrumb(Breadcrumb):
108    """A breadcrumb for the student study course.
109    """
110    grok.context(IStudentStudyCourse)
[7318]111    title = 'Study Course'
[6635]112
113class PaymentsBreadcrumb(Breadcrumb):
114    """A breadcrumb for the student payments folder.
115    """
[6859]116    grok.context(IStudentPaymentsContainer)
[7318]117    title = 'Payments'
[6635]118
[6870]119class OnlinePaymentBreadcrumb(Breadcrumb):
[7251]120    """A breadcrumb for payments.
[6870]121    """
[6877]122    grok.context(IStudentOnlinePayment)
[6870]123
124    @property
125    def title(self):
126        return self.context.p_id
127
[6635]128class AccommodationBreadcrumb(Breadcrumb):
129    """A breadcrumb for the student accommodation folder.
130    """
131    grok.context(IStudentAccommodation)
[7318]132    title = 'Accommodation'
[6635]133
[7009]134    #@property
135    #def target(self):
136    #    prm = get_principal_role_manager()
137    #    principal = get_current_principal()
138    #    roles = [x[0] for x in prm.getRolesForPrincipal(principal.id)]
139    #    if 'waeup.Student' in roles:
140    #        return 'index'
141    #    else:
142    #        return 'manage'
143
[6994]144class BedTicketBreadcrumb(Breadcrumb):
145    """A breadcrumb for bed tickets.
146    """
147    grok.context(IBedTicket)
[7009]148
[6994]149    @property
150    def title(self):
151        return 'Bed Ticket %s' % self.context.getSessionString()
152
[6776]153class StudyLevelBreadcrumb(Breadcrumb):
154    """A breadcrumb for course lists.
155    """
156    grok.context(IStudentStudyLevel)
157
158    @property
159    def title(self):
160        return self.context.level_title
161
[7321]162class StudentsContainerPage(SIRPPage):
[6626]163    """The standard view for student containers.
[6621]164    """
165    grok.context(IStudentsContainer)
166    grok.name('index')
[7240]167    grok.require('waeup.viewStudentsContainer')
[6695]168    grok.template('containerpage')
[6654]169    label = 'Student Section'
170    title = 'Students'
[6642]171    pnav = 4
[6621]172
[6626]173    def update(self, *args, **kw):
174        datatable.need()
175        form = self.request.form
176        self.hitlist = []
177        if 'searchterm' in form and form['searchterm']:
178            self.searchterm = form['searchterm']
179            self.searchtype = form['searchtype']
180        elif 'old_searchterm' in form:
181            self.searchterm = form['old_searchterm']
182            self.searchtype = form['old_searchtype']
183        else:
184            if 'search' in form:
185                self.flash('Empty search string.')
186            return
[7068]187        if self.searchtype == 'current_session':
188            self.searchterm = int(self.searchterm)
[6626]189        self.hitlist = search(query=self.searchterm,
190            searchtype=self.searchtype, view=self)
191        if not self.hitlist:
192            self.flash('No student found.')
193        return
194
[7321]195class SetPasswordPage(SIRPPage):
196    grok.context(ISIRPObject)
[6699]197    grok.name('setpassword')
[7369]198    grok.require('waeup.Anonymous')
[6774]199    grok.template('setpassword')
[6699]200    title = ''
201    label = 'Set password for first-time login'
[6758]202    ac_prefix = 'PWD'
[6715]203    pnav = 0
[6699]204
205    def update(self, SUBMIT=None):
[6758]206        self.reg_number = self.request.form.get('reg_number', None)
207        self.ac_series = self.request.form.get('ac_series', None)
208        self.ac_number = self.request.form.get('ac_number', None)
[6756]209
[6699]210        if SUBMIT is None:
211            return
212        hitlist = search(query=self.reg_number,
213            searchtype='reg_number', view=self)
214        if not hitlist:
215            self.flash('No student found.')
216            return
217        if len(hitlist) != 1:   # Cannot happen but anyway
218            self.flash('More than one student found.')
219            return
[6704]220        student = hitlist[0].context
221        self.student_id = student.student_id
222        student_pw = student.password
[6758]223        pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number)
[6699]224        code = get_access_code(pin)
225        if not code:
226            self.flash('Access code is invalid.')
227            return
[6704]228        if student_pw and pin == student.adm_code:
229            self.flash('Password has already been set. Your Student Id is %s'
230                % self.student_id)
231            return
232        elif student_pw:
[6800]233            self.flash('Password has already been set. You are using the wrong Access Code.')
[6704]234            return
[6699]235        # Mark pin as used (this also fires a pin related transition)
236        # and set student password
237        if code.state == USED:
238            self.flash('Access code has already been used.')
239            return
240        else:
[6704]241            comment = u"AC invalidated for %s" % self.student_id
[6699]242            # Here we know that the ac is in state initialized so we do not
243            # expect an exception
244            #import pdb; pdb.set_trace()
245            invalidate_accesscode(pin,comment)
[6758]246            IUserAccount(student).setPassword(self.ac_number)
[6769]247            student.adm_code = pin
[6704]248        self.flash('Password has been set. Your Student Id is %s'
249            % self.student_id)
[6699]250        return
251
[6626]252class StudentsContainerManageActionButton(ManageActionButton):
[6622]253    grok.order(1)
254    grok.context(IStudentsContainer)
255    grok.view(StudentsContainerPage)
[7136]256    grok.require('waeup.manageStudent')
[6647]257    text = 'Manage student section'
[6622]258
[6626]259
[7321]260class StudentsContainerManagePage(SIRPPage):
[6626]261    """The manage page for student containers.
[6622]262    """
263    grok.context(IStudentsContainer)
264    grok.name('manage')
[7136]265    grok.require('waeup.manageStudent')
[6695]266    grok.template('containermanagepage')
[6642]267    pnav = 4
[6647]268    title = 'Manage student section'
[6622]269
270    @property
271    def label(self):
272        return self.title
273
[6626]274    def update(self, *args, **kw):
275        datatable.need()
[6820]276        toggleall.need()
[7329]277        warning.need()
[6626]278        form = self.request.form
279        self.hitlist = []
280        if 'searchterm' in form and form['searchterm']:
281            self.searchterm = form['searchterm']
282            self.searchtype = form['searchtype']
283        elif 'old_searchterm' in form:
284            self.searchterm = form['old_searchterm']
285            self.searchtype = form['old_searchtype']
286        else:
287            if 'search' in form:
288                self.flash('Empty search string.')
289            return
290        if not 'entries' in form:
291            self.hitlist = search(query=self.searchterm,
292                searchtype=self.searchtype, view=self)
293            if not self.hitlist:
294                self.flash('No student found.')
295            return
296        entries = form['entries']
297        if isinstance(entries, basestring):
298            entries = [entries]
299        deleted = []
300        for entry in entries:
301            if 'remove' in form:
302                del self.context[entry]
303                deleted.append(entry)
304        self.hitlist = search(query=self.searchterm,
305            searchtype=self.searchtype, view=self)
306        if len(deleted):
307            self.flash('Successfully removed: %s' % ', '.join(deleted))
[6622]308        return
309
[6626]310class StudentsContainerAddActionButton(AddActionButton):
311    grok.order(1)
312    grok.context(IStudentsContainer)
313    grok.view(StudentsContainerManagePage)
[7136]314    grok.require('waeup.manageStudent')
[6626]315    text = 'Add student'
316    target = 'addstudent'
317
[7321]318class StudentAddFormPage(SIRPAddFormPage):
[6622]319    """Add-form to add a student.
320    """
321    grok.context(IStudentsContainer)
[7136]322    grok.require('waeup.manageStudent')
[6622]323    grok.name('addstudent')
[7357]324    form_fields = grok.AutoFields(IStudent).select(
325        'firstname', 'middlename', 'lastname')
[6622]326    title = 'Students'
327    label = 'Add student'
[6642]328    pnav = 4
[6622]329
330    @grok.action('Create student record')
331    def addStudent(self, **data):
332        student = createObject(u'waeup.Student')
333        self.applyData(student, **data)
[6652]334        self.context.addStudent(student)
[6626]335        self.flash('Student record created.')
[6651]336        self.redirect(self.url(self.context[student.student_id], 'index'))
[6622]337        return
338
[7321]339class StudentBaseDisplayFormPage(SIRPDisplayFormPage):
[6631]340    """ Page to display student base data
341    """
[6622]342    grok.context(IStudent)
343    grok.name('index')
[6660]344    grok.require('waeup.viewStudent')
[6695]345    grok.template('basepage')
[6756]346    form_fields = grok.AutoFields(IStudentBase).omit('password')
[6642]347    pnav = 4
348    title = 'Base Data'
[6622]349
350    @property
351    def label(self):
[7364]352        return '%s: Base Data' % self.context.display_fullname
[6631]353
[6699]354    @property
355    def hasPassword(self):
356        if self.context.password:
357            return 'set'
358        return 'unset'
359
[7229]360class ContactActionButton(ManageActionButton):
361    grok.order(4)
362    grok.context(IStudent)
363    grok.view(StudentBaseDisplayFormPage)
364    grok.require('waeup.manageStudent')
365    icon = 'actionicon_mail.png'
366    text = 'Send email'
[7230]367    target = 'contactstudent'
[7229]368
369class ContactStudentForm(ContactAdminForm):
370    grok.context(IStudent)
[7230]371    grok.name('contactstudent')
[7275]372    grok.require('waeup.viewStudent')
[7229]373    pnav = 4
374    title = 'Contact'
375    form_fields = grok.AutoFields(IContactForm).select('subject', 'body')
376
[7275]377    def update(self, subject=u''):
378        self.form_fields.get('subject').field.default = subject
379        self.subject = subject
380        return
381
[7229]382    def label(self):
[7364]383        return u'Send message to %s' % self.context.display_fullname
[7229]384
385    @grok.action('Send message now')
386    def send(self, *args, **data):
[7234]387        try:
[7403]388            email = self.request.principal.email
[7234]389        except AttributeError:
[7403]390            email = self.config.email_admin
391        usertype = getattr(self.request.principal,
392                           'user_type', 'system').title()
[7358]393        sirp_utils = getUtility(ISIRPUtils)
[7404]394        success = sirp_utils.sendContactForm(
[7403]395                self.request.principal.title,email,
396                self.context.display_fullname,self.context.email,
397                self.request.principal.id,usertype,
398                self.config.name,
399                data['body'],data['subject'])
[7229]400        if success:
401            self.flash('Your message has been sent.')
402        else:
403            self.flash('An smtp server error occurred.')
404        return
405
[6631]406class StudentBaseManageActionButton(ManageActionButton):
407    grok.order(1)
408    grok.context(IStudent)
409    grok.view(StudentBaseDisplayFormPage)
[7136]410    grok.require('waeup.manageStudent')
[6695]411    text = 'Manage'
[7133]412    target = 'manage_base'
[6631]413
[7321]414class StudentBaseManageFormPage(SIRPEditFormPage):
[7133]415    """ View to manage student base data
[6631]416    """
417    grok.context(IStudent)
[7133]418    grok.name('manage_base')
[7136]419    grok.require('waeup.manageStudent')
[6631]420    form_fields = grok.AutoFields(IStudentBase).omit('student_id')
[6695]421    grok.template('basemanagepage')
422    label = 'Manage base data'
[6642]423    title = 'Base Data'
424    pnav = 4
[6631]425
[6638]426    def update(self):
427        datepicker.need() # Enable jQuery datepicker in date fields.
[7134]428        tabs.need()
[6638]429        super(StudentBaseManageFormPage, self).update()
430        self.wf_info = IWorkflowInfo(self.context)
431        return
432
433    def getTransitions(self):
434        """Return a list of dicts of allowed transition ids and titles.
435
436        Each list entry provides keys ``name`` and ``title`` for
437        internal name and (human readable) title of a single
438        transition.
439        """
440        allowed_transitions = self.wf_info.getManualTransitions()
441        return [dict(name='', title='No transition')] +[
442            dict(name=x, title=y) for x, y in allowed_transitions]
443
444    @grok.action('Save')
445    def save(self, **data):
[6701]446        form = self.request.form
[6790]447        password = form.get('password', None)
448        password_ctl = form.get('control_password', None)
449        if password:
[7147]450            validator = getUtility(IPasswordValidator)
451            errors = validator.validate_password(password, password_ctl)
452            if errors:
453                self.flash( ' '.join(errors))
454                return
455        changed_fields = self.applyData(self.context, **data)
[6771]456        # Turn list of lists into single list
457        if changed_fields:
458            changed_fields = reduce(lambda x,y: x+y, changed_fields.values())
[7147]459        else:
460            changed_fields = []
461        if password:
462            # Now we know that the form has no errors and can set password ...
463            IUserAccount(self.context).setPassword(password)
464            changed_fields.append('password')
465        # ... and execute transition
[6638]466        if form.has_key('transition') and form['transition']:
467            transition_id = form['transition']
468            self.wf_info.fireTransition(transition_id)
[7147]469        fields_string = ' + '.join(changed_fields)
[6638]470        self.flash('Form has been saved.')
[6644]471        if fields_string:
[6943]472            write_log_message(self, 'saved: % s' % fields_string)
[6638]473        return
474
[7321]475class StudentClearanceDisplayFormPage(SIRPDisplayFormPage):
[6631]476    """ Page to display student clearance data
477    """
478    grok.context(IStudent)
479    grok.name('view_clearance')
[6660]480    grok.require('waeup.viewStudent')
[6695]481    form_fields = grok.AutoFields(IStudentClearance).omit('clearance_locked')
[6650]482    form_fields['date_of_birth'].custom_widget = FriendlyDateDisplayWidget('le')
[6642]483    title = 'Clearance Data'
484    pnav = 4
[6631]485
486    @property
487    def label(self):
[7364]488        return '%s: Clearance Data' % self.context.display_fullname
[6631]489
490class StudentClearanceManageActionButton(ManageActionButton):
491    grok.order(1)
492    grok.context(IStudent)
493    grok.view(StudentClearanceDisplayFormPage)
[7136]494    grok.require('waeup.manageStudent')
[6695]495    text = 'Manage'
[6631]496    target = 'edit_clearance'
497
[7158]498class StudentClearActionButton(ManageActionButton):
499    grok.order(2)
500    grok.context(IStudent)
501    grok.view(StudentClearanceDisplayFormPage)
502    grok.require('waeup.clearStudent')
503    text = 'Clear student'
504    target = 'clear'
[7160]505    icon = 'actionicon_accept.png'
[7158]506
507    @property
508    def target_url(self):
509        if self.context.state != REQUESTED:
510            return ''
511        return self.view.url(self.view.context, self.target)
512
513class StudentRejectClearanceActionButton(ManageActionButton):
[7277]514    grok.order(3)
[7158]515    grok.context(IStudent)
516    grok.view(StudentClearanceDisplayFormPage)
517    grok.require('waeup.clearStudent')
518    text = 'Reject clearance'
519    target = 'reject_clearance'
[7160]520    icon = 'actionicon_reject.png'
[7158]521
522    @property
523    def target_url(self):
524        if self.context.state not in (REQUESTED, CLEARED):
525            return ''
526        return self.view.url(self.view.context, self.target)
527
[7277]528class ClearanceSlipActionButton(ManageActionButton):
529    grok.order(4)
530    grok.context(IStudent)
531    grok.view(StudentClearanceDisplayFormPage)
532    grok.require('waeup.viewStudent')
533    icon = 'actionicon_pdf.png'
534    text = 'Download clearance slip'
535    target = 'clearance.pdf'
536
537class ExportPDFClearanceSlipPage(grok.View):
538    """Deliver a PDF slip of the context.
539    """
540    grok.context(IStudent)
541    grok.name('clearance.pdf')
542    grok.require('waeup.viewStudent')
543    form_fields = grok.AutoFields(IStudentClearance).omit('clearance_locked')
[7318]544    form_fields['date_of_birth'].custom_widget = FriendlyDateDisplayWidget('le')
[7277]545    prefix = 'form'
[7318]546    title = 'Clearance Data'
[7277]547
548    @property
549    def label(self):
[7364]550        return 'Clearance Slip of %s' % self.context.display_fullname
[7277]551
552    def render(self):
553        studentview = StudentBaseDisplayFormPage(self.context.getStudent(),
554            self.request)
555        students_utils = getUtility(IStudentsUtils)
556        return students_utils.renderPDF(
[7318]557            self, 'clearance.pdf',
[7310]558            self.context.getStudent(), studentview)
[7277]559
[7321]560class StudentClearanceManageFormPage(SIRPEditFormPage):
[6631]561    """ Page to edit student clearance data
562    """
563    grok.context(IStudent)
564    grok.name('edit_clearance')
[7136]565    grok.require('waeup.manageStudent')
[7134]566    grok.template('clearanceeditpage')
[6631]567    form_fields = grok.AutoFields(IStudentClearance)
[6695]568    label = 'Manage clearance data'
[6642]569    title = 'Clearance Data'
570    pnav = 4
[6650]571    form_fields['date_of_birth'].custom_widget = FriendlyDateWidget('le-year')
572
573    def update(self):
574        datepicker.need() # Enable jQuery datepicker in date fields.
[7134]575        tabs.need()
[6650]576        return super(StudentClearanceManageFormPage, self).update()
577
[7134]578    @grok.action('Save')
[6695]579    def save(self, **data):
[6762]580        msave(self, **data)
[6695]581        return
582
[7158]583class StudentClearPage(grok.View):
584    """ Clear student by clearance officer
585    """
586    grok.context(IStudent)
587    grok.name('clear')
588    grok.require('waeup.clearStudent')
589
590    def update(self):
591        if self.context.state == REQUESTED:
592            IWorkflowInfo(self.context).fireTransition('clear')
593            self.flash('Student has been cleared.')
594        else:
595            self.flash('Student is in the wrong state.')
596        self.redirect(self.url(self.context,'view_clearance'))
597        return
598
599    def render(self):
600        return
601
602class StudentRejectClearancePage(grok.View):
603    """ Reject clearance by clearance officers
604    """
605    grok.context(IStudent)
606    grok.name('reject_clearance')
607    grok.require('waeup.clearStudent')
608
609    def update(self):
610        if self.context.state == CLEARED:
611            IWorkflowInfo(self.context).fireTransition('reset4')
[7275]612            message = 'Clearance has been annulled'
613            self.flash(message)
[7158]614        elif self.context.state == REQUESTED:
615            IWorkflowInfo(self.context).fireTransition('reset3')
[7275]616            message = 'Clearance request has been rejected'
617            self.flash(message)
[7158]618        else:
619            self.flash('Student is in the wrong state.')
[7334]620            self.redirect(self.url(self.context,'view_clearance'))
[7275]621            return
622        args = {'subject':message}
623        self.redirect(self.url(self.context) +
624            '/contactstudent?%s' % urlencode(args))
[7158]625        return
626
627    def render(self):
628        return
629
[7321]630class StudentPersonalDisplayFormPage(SIRPDisplayFormPage):
[6631]631    """ Page to display student personal data
632    """
633    grok.context(IStudent)
634    grok.name('view_personal')
[6660]635    grok.require('waeup.viewStudent')
[6631]636    form_fields = grok.AutoFields(IStudentPersonal)
[7386]637    form_fields['perm_address'].custom_widget = BytesDisplayWidget
[6642]638    title = 'Personal Data'
639    pnav = 4
[6631]640
641    @property
642    def label(self):
[7364]643        return '%s: Personal Data' % self.context.display_fullname
[6631]644
645class StudentPersonalManageActionButton(ManageActionButton):
646    grok.order(1)
647    grok.context(IStudent)
648    grok.view(StudentPersonalDisplayFormPage)
[7136]649    grok.require('waeup.manageStudent')
[6695]650    text = 'Manage'
[6631]651    target = 'edit_personal'
652
[7321]653class StudentPersonalManageFormPage(SIRPEditFormPage):
[6631]654    """ Page to edit student clearance data
655    """
656    grok.context(IStudent)
657    grok.name('edit_personal')
[6660]658    grok.require('waeup.viewStudent')
[6631]659    form_fields = grok.AutoFields(IStudentPersonal)
[6695]660    label = 'Manage personal data'
[6642]661    title = 'Personal Data'
662    pnav = 4
[6631]663
[6762]664    @grok.action('Save')
665    def save(self, **data):
666        msave(self, **data)
667        return
668
[7321]669class StudyCourseDisplayFormPage(SIRPDisplayFormPage):
[6635]670    """ Page to display the student study course data
671    """
672    grok.context(IStudentStudyCourse)
673    grok.name('index')
[6660]674    grok.require('waeup.viewStudent')
[6635]675    form_fields = grok.AutoFields(IStudentStudyCourse)
[6775]676    grok.template('studycoursepage')
[6642]677    title = 'Study Course'
678    pnav = 4
[6635]679
680    @property
681    def label(self):
[7364]682        return '%s: Study Course' % self.context.__parent__.display_fullname
[6635]683
[6912]684    @property
685    def current_mode(self):
[7171]686        if self.context.certificate:
687            current_mode = study_modes.getTermByToken(
688                self.context.certificate.study_mode).title
689            return current_mode
690        return
691       
692    @property
693    def department(self):
[7205]694        if self.context.certificate is not None:
[7171]695            return self.context.certificate.__parent__.__parent__
696        return
[6912]697
[7171]698    @property
699    def faculty(self):
[7205]700        if self.context.certificate is not None:
[7171]701            return self.context.certificate.__parent__.__parent__.__parent__
702        return
703
[6649]704class StudyCourseManageActionButton(ManageActionButton):
705    grok.order(1)
706    grok.context(IStudentStudyCourse)
707    grok.view(StudyCourseDisplayFormPage)
[7136]708    grok.require('waeup.manageStudent')
[6695]709    text = 'Manage'
[6775]710    target = 'manage'
[6649]711
[7321]712class StudyCourseManageFormPage(SIRPEditFormPage):
[6649]713    """ Page to edit the student study course data
714    """
715    grok.context(IStudentStudyCourse)
[6775]716    grok.name('manage')
[7136]717    grok.require('waeup.manageStudent')
[6775]718    grok.template('studycoursemanagepage')
[6649]719    form_fields = grok.AutoFields(IStudentStudyCourse)
720    title = 'Study Course'
[6695]721    label = 'Manage study course'
[6649]722    pnav = 4
[6775]723    taboneactions = ['Save','Cancel']
724    tabtwoactions = ['Remove selected levels','Cancel']
725    tabthreeactions = ['Add study level']
[6649]726
[6775]727    def update(self):
728        super(StudyCourseManageFormPage, self).update()
729        tabs.need()
[7329]730        warning.need()
[6775]731        datatable.need()
732        return
733
[6761]734    @grok.action('Save')
735    def save(self, **data):
[6762]736        msave(self, **data)
[6761]737        return
738
[6775]739    @property
740    def level_dict(self):
741        studylevelsource = StudyLevelSource().factory
742        for code in studylevelsource.getValues(self.context):
743            title = studylevelsource.getTitle(self.context, code)
744            yield(dict(code=code, title=title))
745
746    @grok.action('Add study level')
[6774]747    def addStudyLevel(self, **data):
[6775]748        level_code = self.request.form.get('addlevel', None)
[6774]749        studylevel = StudentStudyLevel()
[6775]750        studylevel.level = int(level_code)
751        try:
[6782]752            self.context.addStudentStudyLevel(
753                self.context.certificate,studylevel)
[6775]754        except KeyError:
755            self.flash('This level exists.')
756        self.redirect(self.url(self.context, u'@@manage')+'#tab-2')
[6774]757        return
758
[7329]759    @jsaction('Remove selected levels')
[6775]760    def delStudyLevels(self, **data):
761        form = self.request.form
762        if form.has_key('val_id'):
763            child_id = form['val_id']
764        else:
765            self.flash('No study level selected.')
766            self.redirect(self.url(self.context, '@@manage')+'#tab-2')
767            return
768        if not isinstance(child_id, list):
769            child_id = [child_id]
770        deleted = []
771        for id in child_id:
772            try:
773                del self.context[id]
774                deleted.append(id)
775            except:
776                self.flash('Could not delete %s: %s: %s' % (
777                        id, sys.exc_info()[0], sys.exc_info()[1]))
778        if len(deleted):
779            self.flash('Successfully removed: %s' % ', '.join(deleted))
780        self.redirect(self.url(self.context, u'@@manage')+'#tab-2')
781        return
[6774]782
[7321]783class StudyLevelDisplayFormPage(SIRPDisplayFormPage):
[6774]784    """ Page to display student study levels
785    """
786    grok.context(IStudentStudyLevel)
787    grok.name('index')
788    grok.require('waeup.viewStudent')
[6775]789    form_fields = grok.AutoFields(IStudentStudyLevel)
[6783]790    grok.template('studylevelpage')
[6774]791    pnav = 4
792
[7310]793    def update(self):
794        super(StudyLevelDisplayFormPage, self).update()
795        datatable.need()
796        return
797
[6774]798    @property
[6792]799    def title(self):
800        return 'Study Level %s' % self.context.level_title
801
802    @property
[6774]803    def label(self):
[6776]804        return '%s: Study Level %s' % (
[7364]805            self.context.getStudent().display_fullname,self.context.level_title)
[6774]806
[6803]807    @property
808    def total_credits(self):
809        total_credits = 0
810        for key, val in self.context.items():
811            total_credits += val.credits
812        return total_credits
813
[7028]814class CourseRegistrationSlipActionButton(ManageActionButton):
[6792]815    grok.order(1)
816    grok.context(IStudentStudyLevel)
[7029]817    grok.view(StudyLevelDisplayFormPage)
[7028]818    grok.require('waeup.viewStudent')
819    icon = 'actionicon_pdf.png'
820    text = 'Download course registration slip'
821    target = 'course_registration.pdf'
822
823class ExportPDFCourseRegistrationSlipPage(grok.View):
824    """Deliver a PDF slip of the context.
825    """
826    grok.context(IStudentStudyLevel)
827    grok.name('course_registration.pdf')
828    grok.require('waeup.viewStudent')
829    form_fields = grok.AutoFields(IStudentStudyLevel)
830    prefix = 'form'
[7318]831    title = 'Level Data'
832    content_title = 'Course List'
[7028]833
834    @property
835    def label(self):
836        return 'Course Registration Slip %s' % self.context.level_title
837
838    def render(self):
839        studentview = StudentBaseDisplayFormPage(self.context.getStudent(),
840            self.request)
[7150]841        students_utils = getUtility(IStudentsUtils)
[7318]842        tabledata = sorted(self.context.values(),
843            key=lambda value: str(value.semester) + value.code)
[7186]844        return students_utils.renderPDF(
[7318]845            self, 'course_registration.pdf',
[7310]846            self.context.getStudent(), studentview,
847            tableheader=[('Sem.','semester', 1.5),('Code','code', 2.5),
848                         ('Title','title', 5),
849                         ('Dept.','dcode', 1.5), ('Faculty','fcode', 1.5),
850                         ('Cred.', 'credits', 1.5),
851                         ('Mand.', 'core_or_elective', 1.5),
852                         ('Score', 'score', 1.5),('Auto', 'automatic', 1.5)
[7304]853                         ],
[7318]854            tabledata=tabledata)
[7028]855
856class StudyLevelManageActionButton(ManageActionButton):
857    grok.order(2)
858    grok.context(IStudentStudyLevel)
[6792]859    grok.view(StudyLevelDisplayFormPage)
[7136]860    grok.require('waeup.manageStudent')
[6792]861    text = 'Manage'
862    target = 'manage'
863
[7334]864class StudentValidateCoursesActionButton(ManageActionButton):
865    grok.order(3)
866    grok.context(IStudentStudyLevel)
867    grok.view(StudyLevelDisplayFormPage)
868    grok.require('waeup.validateStudent')
869    text = 'Validate courses'
870    target = 'validate_courses'
871    icon = 'actionicon_accept.png'
872
873    @property
874    def target_url(self):
875        if self.context.getStudent().state != REGISTERED or \
876            str(self.context.__parent__.current_level) != self.context.__name__:
877            return ''
878        return self.view.url(self.view.context, self.target)
879
880class StudentRejectCoursesActionButton(ManageActionButton):
881    grok.order(4)
882    grok.context(IStudentStudyLevel)
883    grok.view(StudyLevelDisplayFormPage)
884    grok.require('waeup.validateStudent')
885    text = 'Reject courses'
886    target = 'reject_courses'
887    icon = 'actionicon_reject.png'
888
889    @property
890    def target_url(self):
891        if self.context.getStudent().state not in (VALIDATED, REGISTERED) or \
892            str(self.context.__parent__.current_level) != self.context.__name__:
893            return ''
894        return self.view.url(self.view.context, self.target)
895
[7321]896class StudyLevelManageFormPage(SIRPEditFormPage):
[6792]897    """ Page to edit the student study level data
898    """
899    grok.context(IStudentStudyLevel)
900    grok.name('manage')
[7136]901    grok.require('waeup.manageStudent')
[6792]902    grok.template('studylevelmanagepage')
903    form_fields = grok.AutoFields(IStudentStudyLevel)
904    pnav = 4
905    taboneactions = ['Save','Cancel']
[6795]906    tabtwoactions = ['Add course ticket','Remove selected tickets','Cancel']
[6792]907
908    def update(self):
909        super(StudyLevelManageFormPage, self).update()
910        tabs.need()
[7329]911        warning.need()
[6792]912        datatable.need()
913        return
914
915    @property
916    def title(self):
917        return 'Study Level %s' % self.context.level_title
918
919    @property
920    def label(self):
921        return 'Manage study level %s' % self.context.level_title
922
923    @grok.action('Save')
924    def save(self, **data):
925        msave(self, **data)
926        return
927
928    @grok.action('Add course ticket')
[6795]929    def addCourseTicket(self, **data):
930        self.redirect(self.url(self.context, '@@add'))
[6792]931
[7329]932    @jsaction('Remove selected tickets')
[6792]933    def delCourseTicket(self, **data):
934        form = self.request.form
935        if form.has_key('val_id'):
936            child_id = form['val_id']
937        else:
938            self.flash('No ticket selected.')
939            self.redirect(self.url(self.context, '@@manage')+'#tab-2')
940            return
941        if not isinstance(child_id, list):
942            child_id = [child_id]
943        deleted = []
944        for id in child_id:
945            try:
946                del self.context[id]
947                deleted.append(id)
948            except:
949                self.flash('Could not delete %s: %s: %s' % (
950                        id, sys.exc_info()[0], sys.exc_info()[1]))
951        if len(deleted):
952            self.flash('Successfully removed: %s' % ', '.join(deleted))
953        self.redirect(self.url(self.context, u'@@manage')+'#tab-2')
954        return
955
[7334]956class ValidateCoursesPage(grok.View):
957    """ Validate course list by course adviser
958    """
959    grok.context(IStudentStudyLevel)
960    grok.name('validate_courses')
961    grok.require('waeup.validateStudent')
962
963    def update(self):
964        if str(self.context.__parent__.current_level) != self.context.__name__:
965            self.flash('This level does not correspond current level.')
966        elif self.context.getStudent().state == REGISTERED:
967            IWorkflowInfo(self.context.getStudent()).fireTransition('validate_courses')
[7337]968            self.flash('Course list has been validated.')
[7334]969        else:
970            self.flash('Student is in the wrong state.')
971        self.redirect(self.url(self.context))
972        return
973
974    def render(self):
975        return
976
977class RejectCoursesPage(grok.View):
978    """ Reject course list by course adviser
979    """
980    grok.context(IStudentStudyLevel)
981    grok.name('reject_courses')
982    grok.require('waeup.validateStudent')
983
984    def update(self):
985        if str(self.context.__parent__.current_level) != self.context.__name__:
986            self.flash('This level does not correspond current level.')
987            self.redirect(self.url(self.context))
988            return
989        elif self.context.getStudent().state == VALIDATED:
990            IWorkflowInfo(self.context.getStudent()).fireTransition('reset8')
991            message = 'Course list request has been annulled'
992            self.flash(message)
993        elif self.context.getStudent().state == REGISTERED:
994            IWorkflowInfo(self.context.getStudent()).fireTransition('reset7')
995            message = 'Course list request has been rejected'
996            self.flash(message)
997        else:
998            self.flash('Student is in the wrong state.')
999            self.redirect(self.url(self.context))
1000            return
1001        args = {'subject':message}
1002        self.redirect(self.url(self.context.getStudent()) +
1003            '/contactstudent?%s' % urlencode(args))
1004        return
1005
1006    def render(self):
1007        return
1008
[7321]1009class CourseTicketAddFormPage(SIRPAddFormPage):
[6808]1010    """Add a course ticket.
[6795]1011    """
1012    grok.context(IStudentStudyLevel)
1013    grok.name('add')
[7136]1014    grok.require('waeup.manageStudent')
[6795]1015    label = 'Add course ticket'
[6808]1016    form_fields = grok.AutoFields(ICourseTicketAdd).omit(
1017        'grade', 'score', 'automatic')
[6795]1018    pnav = 4
1019
1020    @property
1021    def title(self):
1022        return 'Study Level %s' % self.context.level_title
1023
1024    @grok.action('Add course ticket')
1025    def addCourseTicket(self, **data):
1026        ticket = CourseTicket()
1027        course = data['course']
1028        ticket.core_or_elective = data['core_or_elective']
[6802]1029        ticket.automatic = False
[6795]1030        ticket.code = course.code
1031        ticket.title = course.title
1032        ticket.faculty = course.__parent__.__parent__.__parent__.title
1033        ticket.department = course.__parent__.__parent__.title
[7304]1034        ticket.fcode = course.__parent__.__parent__.__parent__.code
1035        ticket.dcode = course.__parent__.__parent__.code
[6795]1036        ticket.credits = course.credits
1037        ticket.passmark = course.passmark
1038        ticket.semester = course.semester
1039        try:
1040            self.context.addCourseTicket(ticket)
1041        except KeyError:
1042            self.flash('The ticket exists.')
1043            return
[6799]1044        self.flash('Successfully added %s.' % ticket.code)
[6795]1045        self.redirect(self.url(self.context, u'@@manage')+'#tab-2')
1046        return
1047
1048    @grok.action('Cancel')
1049    def cancel(self, **data):
1050        self.redirect(self.url(self.context))
1051
[7321]1052class CourseTicketDisplayFormPage(SIRPDisplayFormPage):
[6796]1053    """ Page to display course tickets
1054    """
1055    grok.context(ICourseTicket)
1056    grok.name('index')
1057    grok.require('waeup.viewStudent')
1058    form_fields = grok.AutoFields(ICourseTicket)
1059    grok.template('courseticketpage')
1060    pnav = 4
1061
1062    @property
1063    def title(self):
1064        return 'Course Ticket %s' % self.context.code
1065
1066    @property
1067    def label(self):
1068        return '%s: Course Ticket %s' % (
[7364]1069            self.context.getStudent().display_fullname,self.context.code)
[6796]1070
1071class CourseTicketManageActionButton(ManageActionButton):
1072    grok.order(1)
1073    grok.context(ICourseTicket)
1074    grok.view(CourseTicketDisplayFormPage)
[7136]1075    grok.require('waeup.manageStudent')
[6796]1076    text = 'Manage'
1077    target = 'manage'
1078
[7321]1079class CourseTicketManageFormPage(SIRPEditFormPage):
[6796]1080    """ Page to manage course tickets
1081    """
1082    grok.context(ICourseTicket)
1083    grok.name('manage')
[7136]1084    grok.require('waeup.manageStudent')
[6796]1085    form_fields = grok.AutoFields(ICourseTicket)
1086    grok.template('courseticketmanagepage')
1087    pnav = 4
1088
1089    @property
1090    def title(self):
1091        return 'Course Ticket %s' % self.context.code
1092
1093    @property
1094    def label(self):
1095        return 'Manage course ticket %s' % self.context.code
1096
1097    @grok.action('Save')
1098    def save(self, **data):
1099        msave(self, **data)
1100        return
1101
[6940]1102# We don't need the display form page yet
[7321]1103#class PaymentsDisplayFormPage(SIRPDisplayFormPage):
[6943]1104#    """ Page to display the student payments
1105#    """
1106#    grok.context(IStudentPaymentsContainer)
1107#    grok.name('view')
1108#    grok.require('waeup.viewStudent')
1109#    form_fields = grok.AutoFields(IStudentPaymentsContainer)
1110#    grok.template('paymentspage')
1111#    title = 'Payments'
1112#    pnav = 4
[6635]1113
[6943]1114#    @property
1115#    def label(self):
[7364]1116#        return '%s: Payments' % self.context.__parent__.display_fullname
[6635]1117
[6943]1118#    def update(self):
1119#        super(PaymentsDisplayFormPage, self).update()
1120#        datatable.need()
1121#        return
[6869]1122
[6940]1123# This manage form page is for both students and students officers.
[7321]1124class PaymentsManageFormPage(SIRPEditFormPage):
[6869]1125    """ Page to manage the student payments
1126    """
1127    grok.context(IStudentPaymentsContainer)
[6940]1128    grok.name('index')
[7181]1129    grok.require('waeup.payStudent')
[6869]1130    form_fields = grok.AutoFields(IStudentPaymentsContainer)
1131    grok.template('paymentsmanagepage')
1132    title = 'Payments'
1133    pnav = 4
1134
[6940]1135    def unremovable(self, ticket):
[7251]1136        usertype = getattr(self.request.principal, 'user_type', None)
1137        if not usertype:
1138            return False
1139        return (self.request.principal.user_type == 'student' and ticket.r_code)
[6940]1140
[6869]1141    @property
1142    def label(self):
[7364]1143        return '%s: Payments' % self.context.__parent__.display_fullname
[6869]1144
1145    def update(self):
1146        super(PaymentsManageFormPage, self).update()
1147        datatable.need()
[7329]1148        warning.need()
[6869]1149        return
1150
[7329]1151    @jsaction('Remove selected tickets')
[6869]1152    def delPaymentTicket(self, **data):
1153        form = self.request.form
1154        if form.has_key('val_id'):
1155            child_id = form['val_id']
1156        else:
1157            self.flash('No payment selected.')
[6940]1158            self.redirect(self.url(self.context))
[6869]1159            return
1160        if not isinstance(child_id, list):
1161            child_id = [child_id]
1162        deleted = []
1163        for id in child_id:
[6992]1164            # Students are not allowed to remove used payment tickets
[6940]1165            if not self.unremovable(self.context[id]):
1166                try:
1167                    del self.context[id]
1168                    deleted.append(id)
1169                except:
1170                    self.flash('Could not delete %s: %s: %s' % (
1171                            id, sys.exc_info()[0], sys.exc_info()[1]))
[6869]1172        if len(deleted):
1173            self.flash('Successfully removed: %s' % ', '.join(deleted))
[6943]1174            write_log_message(self,'removed: % s' % ', '.join(deleted))
[6940]1175        self.redirect(self.url(self.context))
[6869]1176        return
1177
1178    @grok.action('Add online payment ticket')
1179    def addPaymentTicket(self, **data):
1180        self.redirect(self.url(self.context, '@@addop'))
1181
[6940]1182#class OnlinePaymentManageActionButton(ManageActionButton):
1183#    grok.order(1)
1184#    grok.context(IStudentPaymentsContainer)
1185#    grok.view(PaymentsDisplayFormPage)
[7136]1186#    grok.require('waeup.manageStudent')
[6940]1187#    text = 'Manage payments'
1188#    target = 'manage'
[6869]1189
[7321]1190class OnlinePaymentAddFormPage(SIRPAddFormPage):
[6869]1191    """ Page to add an online payment ticket
1192    """
1193    grok.context(IStudentPaymentsContainer)
1194    grok.name('addop')
[7181]1195    grok.require('waeup.payStudent')
[6877]1196    form_fields = grok.AutoFields(IStudentOnlinePayment).select(
[6869]1197        'p_category')
[6906]1198    #zzgrok.template('addpaymentpage')
[6869]1199    label = 'Add online payment'
1200    title = 'Payments'
1201    pnav = 4
[6947]1202   
[6869]1203    @grok.action('Create ticket')
1204    def createTicket(self, **data):
[7024]1205        p_category = data['p_category']
1206        student = self.context.__parent__
1207        if p_category == 'bed_allocation' and student[
1208            'studycourse'].current_session != grok.getSite()[
1209            'configuration'].accommodation_session:
1210                self.flash(
1211                    'Your current session does not match accommodation session.')
1212                self.redirect(self.url(self.context))
1213                return
[7150]1214        students_utils = getUtility(IStudentsUtils)
[7186]1215        pay_details  = students_utils.getPaymentDetails(
[7024]1216            p_category,student)
[6994]1217        if pay_details['error']:
1218            self.flash(pay_details['error'])
[6940]1219            self.redirect(self.url(self.context))
[6920]1220            return
[7025]1221        p_item = pay_details['p_item']
1222        p_session = pay_details['p_session']
1223        for key in self.context.keys():
1224            ticket = self.context[key]
[7248]1225            if ticket.p_state == 'paid' and\
1226               ticket.p_category == p_category and \
[7025]1227               ticket.p_item == p_item and \
1228               ticket.p_session == p_session:
1229                  self.flash(
[7251]1230                      'This type of payment has already been made.')
[7025]1231                  self.redirect(self.url(self.context))
1232                  return
1233        payment = createObject(u'waeup.StudentOnlinePayment')
1234        self.applyData(payment, **data)
1235        timestamp = "%d" % int(time()*1000)
1236        #order_id = "%s%s" % (student_id[1:],timestamp)
1237        payment.p_id = "p%s" % timestamp
1238        payment.p_item = p_item
1239        payment.p_session = p_session
[6994]1240        payment.amount_auth = pay_details['amount']
1241        payment.surcharge_1 = pay_details['surcharge_1']
1242        payment.surcharge_2 = pay_details['surcharge_2']
1243        payment.surcharge_3 = pay_details['surcharge_3']
[6869]1244        self.context[payment.p_id] = payment
1245        self.flash('Payment ticket created.')
[6940]1246        self.redirect(self.url(self.context))
[6869]1247        return
1248
[7321]1249class OnlinePaymentDisplayFormPage(SIRPDisplayFormPage):
[6869]1250    """ Page to view an online payment ticket
1251    """
[6877]1252    grok.context(IStudentOnlinePayment)
[6869]1253    grok.name('index')
1254    grok.require('waeup.viewStudent')
[6877]1255    form_fields = grok.AutoFields(IStudentOnlinePayment)
[6869]1256    form_fields['creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
1257    form_fields['payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
1258    pnav = 4
1259
1260    @property
1261    def title(self):
1262        return 'Online Payment Ticket %s' % self.context.p_id
1263
1264    @property
1265    def label(self):
1266        return '%s: Online Payment Ticket %s' % (
[7364]1267            self.context.getStudent().display_fullname,self.context.p_id)
[6869]1268
[7019]1269class PaymentReceiptActionButton(ManageActionButton):
[7028]1270    grok.order(1)
[7019]1271    grok.context(IStudentOnlinePayment)
[7029]1272    grok.view(OnlinePaymentDisplayFormPage)
[7019]1273    grok.require('waeup.viewStudent')
1274    icon = 'actionicon_pdf.png'
1275    text = 'Download payment receipt'
1276    target = 'payment_receipt.pdf'
1277
[7028]1278    @property
1279    def target_url(self):
1280        if self.context.p_state != 'paid':
1281            return ''
1282        return self.view.url(self.view.context, self.target)
1283
[7056]1284class RequestCallbackActionButton(ManageActionButton):
1285    grok.order(2)
1286    grok.context(IStudentOnlinePayment)
1287    grok.view(OnlinePaymentDisplayFormPage)
[7181]1288    grok.require('waeup.payStudent')
[7056]1289    icon = 'actionicon_call.png'
1290    text = 'Request callback'
1291    target = 'callback'
1292
1293    @property
1294    def target_url(self):
1295        if self.context.p_state != 'unpaid':
1296            return ''
1297        return self.view.url(self.view.context, self.target)
1298
[6930]1299class OnlinePaymentCallbackPage(grok.View):
1300    """ Callback view
1301    """
1302    grok.context(IStudentOnlinePayment)
1303    grok.name('callback')
1304    grok.require('waeup.payStudent')
1305
1306    # This update method simulates a valid callback und must be
1307    # specified in the customization package. The parameters must be taken
1308    # from the incoming request.
1309    def update(self):
[7026]1310        if self.context.p_state == 'paid':
1311            self.flash('This ticket has already been paid.')
1312            return
[6936]1313        student = self.context.getStudent()
[6943]1314        write_log_message(self,'valid callback: %s' % self.context.p_id)
[6930]1315        self.context.r_amount_approved = self.context.amount_auth
1316        self.context.r_card_num = u'0000'
1317        self.context.r_code = u'00'
1318        self.context.p_state = 'paid'
1319        self.context.payment_date = datetime.now()
1320        if self.context.p_category == 'clearance':
[6936]1321            # Create CLR access code
[6937]1322            pin, error = create_accesscode('CLR',0,student.student_id)
[6936]1323            if error:
[6940]1324                self.flash('Valid callback received. ' + error)
[6936]1325                return
1326            self.context.ac = pin
[6930]1327        elif self.context.p_category == 'schoolfee':
[6936]1328            # Create SFE access code
[6940]1329            pin, error = create_accesscode('SFE',0,student.student_id)
[6936]1330            if error:
[6940]1331                self.flash('Valid callback received. ' + error)
[6936]1332                return
1333            self.context.ac = pin
[6994]1334        elif self.context.p_category == 'bed_allocation':
1335            # Create HOS access code
1336            pin, error = create_accesscode('HOS',0,student.student_id)
1337            if error:
1338                self.flash('Valid callback received. ' + error)
1339                return
1340            self.context.ac = pin
[6940]1341        self.flash('Valid callback received.')
1342        return
[6930]1343
1344    def render(self):
[6940]1345        self.redirect(self.url(self.context, '@@index'))
[6930]1346        return
1347
[7019]1348class ExportPDFPaymentSlipPage(grok.View):
1349    """Deliver a PDF slip of the context.
1350    """
1351    grok.context(IStudentOnlinePayment)
1352    grok.name('payment_receipt.pdf')
1353    grok.require('waeup.viewStudent')
1354    form_fields = grok.AutoFields(IStudentOnlinePayment)
1355    form_fields['creation_date'].custom_widget = FriendlyDateDisplayWidget('le')
1356    form_fields['payment_date'].custom_widget = FriendlyDateDisplayWidget('le')
1357    prefix = 'form'
[7318]1358    title = 'Payment Data'
[7019]1359
1360    @property
1361    def label(self):
1362        return 'Online Payment Receipt %s' % self.context.p_id
1363
1364    def render(self):
[7028]1365        if self.context.p_state != 'paid':
1366            self.flash('Ticket not yet paid.')
1367            self.redirect(self.url(self.context))
1368            return
[7019]1369        studentview = StudentBaseDisplayFormPage(self.context.getStudent(),
1370            self.request)
[7150]1371        students_utils = getUtility(IStudentsUtils)
[7318]1372        return students_utils.renderPDF(self, 'payment_receipt.pdf',
[7310]1373            self.context.getStudent(), studentview)
[7019]1374
[6992]1375# We don't need the display form page yet
[7321]1376#class AccommodationDisplayFormPage(SIRPDisplayFormPage):
[6992]1377#    """ Page to display the student accommodation data
1378#    """
1379#    grok.context(IStudentAccommodation)
1380#    grok.name('xxx')
1381#    grok.require('waeup.viewStudent')
1382#    form_fields = grok.AutoFields(IStudentAccommodation)
1383#    #grok.template('accommodationpage')
1384#    title = 'Accommodation'
1385#    pnav = 4
1386
1387#    @property
1388#    def label(self):
[7364]1389#        return '%s: Accommodation Data' % self.context.__parent__.display_fullname
[6992]1390
1391# This manage form page is for both students and students officers.
[7321]1392class AccommodationManageFormPage(SIRPEditFormPage):
[7009]1393    """ Page to manage bed tickets.
[6635]1394    """
1395    grok.context(IStudentAccommodation)
1396    grok.name('index')
[7181]1397    grok.require('waeup.handleAccommodation')
[6635]1398    form_fields = grok.AutoFields(IStudentAccommodation)
[6992]1399    grok.template('accommodationmanagepage')
[6642]1400    title = 'Accommodation'
1401    pnav = 4
[7017]1402    officers_only_actions = ['Remove selected']
[6635]1403
1404    @property
1405    def label(self):
[7364]1406        return '%s: Accommodation' % self.context.__parent__.display_fullname
[6637]1407
[6992]1408    def update(self):
1409        super(AccommodationManageFormPage, self).update()
1410        datatable.need()
[7329]1411        warning.need()
[6992]1412        return
1413
[7329]1414    @jsaction('Remove selected')
[7009]1415    def delBedTickets(self, **data):
[7240]1416        if getattr(self.request.principal, 'user_type', None) == 'student':
[7017]1417            self.flash('You are not allowed to remove bed tickets.')
1418            self.redirect(self.url(self.context))
1419            return
[6992]1420        form = self.request.form
1421        if form.has_key('val_id'):
1422            child_id = form['val_id']
1423        else:
1424            self.flash('No bed ticket selected.')
1425            self.redirect(self.url(self.context))
1426            return
1427        if not isinstance(child_id, list):
1428            child_id = [child_id]
1429        deleted = []
1430        for id in child_id:
[7068]1431            del self.context[id]
1432            deleted.append(id)
[6992]1433        if len(deleted):
1434            self.flash('Successfully removed: %s' % ', '.join(deleted))
1435            write_log_message(self,'removed: % s' % ', '.join(deleted))
1436        self.redirect(self.url(self.context))
1437        return
1438
[7009]1439    @property
1440    def selected_actions(self):
1441        sa = self.actions
[7240]1442        if getattr(self.request.principal, 'user_type', None) == 'student':
[7009]1443            sa = [action for action in self.actions
[7017]1444                  if not action.label in self.officers_only_actions]
[7009]1445        return sa
1446
[7015]1447class AddBedTicketActionButton(ManageActionButton):
1448    grok.order(1)
1449    grok.context(IStudentAccommodation)
1450    grok.view(AccommodationManageFormPage)
[7181]1451    grok.require('waeup.handleAccommodation')
[7015]1452    icon = 'actionicon_home.png'
1453    text = 'Book accommodation'
1454    target = 'add'
1455
[7321]1456class BedTicketAddPage(SIRPPage):
[6992]1457    """ Page to add an online payment ticket
1458    """
1459    grok.context(IStudentAccommodation)
1460    grok.name('add')
[7181]1461    grok.require('waeup.handleAccommodation')
[6992]1462    grok.template('enterpin')
[6993]1463    ac_prefix = 'HOS'
[6992]1464    label = 'Add bed ticket'
1465    title = 'Add bed ticket'
1466    pnav = 4
1467    buttonname = 'Create bed ticket'
[6993]1468    notice = ''
[6992]1469
1470    def update(self, SUBMIT=None):
[6996]1471        student = self.context.getStudent()
[7150]1472        students_utils = getUtility(IStudentsUtils)
[7186]1473        acc_details  = students_utils.getAccommodationDetails(student)
[7369]1474        if not acc_details:
1475            self.flash("Your data are incomplete.")
1476            self.redirect(self.url(self.context))
1477            return
[6996]1478        if not student.state in acc_details['allowed_states']:
[7015]1479            self.flash("You are in the wrong registration state.")
[6992]1480            self.redirect(self.url(self.context))
1481            return
[7061]1482        if student['studycourse'].current_session != acc_details['booking_session']:
1483            self.flash(
1484                'Your current session does not match accommodation session.')
1485            self.redirect(self.url(self.context))
1486            return
1487        if str(acc_details['booking_session']) in self.context.keys():
[7060]1488            self.flash('You already booked a bed space in current accommodation session.')
[7004]1489            self.redirect(self.url(self.context))
1490            return
[6992]1491        self.ac_series = self.request.form.get('ac_series', None)
1492        self.ac_number = self.request.form.get('ac_number', None)
1493        if SUBMIT is None:
1494            return
1495        pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number)
1496        code = get_access_code(pin)
1497        if not code:
1498            self.flash('Activation code is invalid.')
1499            return
[7060]1500        # Search and book bed
[6997]1501        cat = queryUtility(ICatalog, name='beds_catalog', default=None)
1502        entries = cat.searchResults(
[7003]1503            owner=(student.student_id,student.student_id))
1504        if len(entries):
[7060]1505            # If bed space has bee manually allocated use this bed
[7003]1506            bed = [entry for entry in entries][0]
[7060]1507        else:
1508            # else search for other available beds
1509            entries = cat.searchResults(
1510                bed_type=(acc_details['bt'],acc_details['bt']))
1511            available_beds = [
1512                entry for entry in entries if entry.owner == NOT_OCCUPIED]
1513            if available_beds:
[7150]1514                students_utils = getUtility(IStudentsUtils)
[7186]1515                bed = students_utils.selectBed(available_beds)
[7060]1516                bed.bookBed(student.student_id)
1517            else:
1518                self.flash('There is no free bed in your category %s.'
1519                            % acc_details['bt'])
1520                return
[6992]1521        # Mark pin as used (this also fires a pin related transition)
1522        if code.state == USED:
1523            self.flash('Activation code has already been used.')
1524            return
1525        else:
1526            comment = u"AC invalidated for %s" % self.context.getStudent().student_id
1527            # Here we know that the ac is in state initialized so we do not
1528            # expect an exception, but the owner might be different
1529            if not invalidate_accesscode(
1530                pin,comment,self.context.getStudent().student_id):
1531                self.flash('You are not the owner of this access code.')
1532                return
[7060]1533        # Create bed ticket
[6992]1534        bedticket = createObject(u'waeup.BedTicket')
1535        bedticket.booking_code = pin
[6994]1536        bedticket.booking_session = acc_details['booking_session']
[6996]1537        bedticket.bed_type = acc_details['bt']
[7006]1538        bedticket.bed = bed
[6996]1539        hall_title = bed.__parent__.hostel_name
1540        coordinates = bed.getBedCoordinates()[1:]
1541        block, room_nr, bed_nr = coordinates
[7068]1542        bedticket.bed_coordinates = '%s, Block %s, Room %s, Bed %s (%s)' % (
1543            hall_title, block, room_nr, bed_nr, bed.bed_type)
[6996]1544        key = str(acc_details['booking_session'])
1545        self.context[key] = bedticket
1546        self.flash('Bed ticket created and bed booked: %s'
1547            % bedticket.bed_coordinates)
[6992]1548        self.redirect(self.url(self.context))
1549        return
1550
[7321]1551class BedTicketDisplayFormPage(SIRPDisplayFormPage):
[6994]1552    """ Page to display bed tickets
1553    """
1554    grok.context(IBedTicket)
1555    grok.name('index')
[7181]1556    grok.require('waeup.handleAccommodation')
[6994]1557    form_fields = grok.AutoFields(IBedTicket)
1558    form_fields[
1559        'booking_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
1560    pnav = 4
1561
1562    @property
1563    def label(self):
1564        return 'Bed Ticket for Session %s' % self.context.getSessionString()
1565
1566    @property
1567    def title(self):
1568        return 'Bed Ticket %s' % self.context.getSessionString()
1569
[7027]1570class BedTicketSlipActionButton(ManageActionButton):
[7028]1571    grok.order(1)
[7027]1572    grok.context(IBedTicket)
[7029]1573    grok.view(BedTicketDisplayFormPage)
[7181]1574    grok.require('waeup.handleAccommodation')
[7027]1575    icon = 'actionicon_pdf.png'
1576    text = 'Download bed allocation slip'
1577    target = 'bed_allocation.pdf'
1578
1579class ExportPDFBedTicketSlipPage(grok.View):
1580    """Deliver a PDF slip of the context.
1581    """
1582    grok.context(IBedTicket)
1583    grok.name('bed_allocation.pdf')
[7181]1584    grok.require('waeup.handleAccommodation')
[7027]1585    form_fields = grok.AutoFields(IBedTicket)
1586    form_fields['booking_date'].custom_widget = FriendlyDateDisplayWidget('le')
1587    prefix = 'form'
[7318]1588    title = 'Bed Allocation Data'
[7027]1589
1590    @property
1591    def label(self):
[7318]1592        return 'Bed Allocation: %s' % self.context.bed_coordinates
[7027]1593
1594    def render(self):
1595        studentview = StudentBaseDisplayFormPage(self.context.getStudent(),
1596            self.request)
[7150]1597        students_utils = getUtility(IStudentsUtils)
[7186]1598        return students_utils.renderPDF(
[7318]1599            self, 'bed_allocation.pdf',
[7310]1600            self.context.getStudent(), studentview)
[7027]1601
[7015]1602class RelocateStudentActionButton(ManageActionButton):
[7027]1603    grok.order(2)
[7015]1604    grok.context(IBedTicket)
1605    grok.view(BedTicketDisplayFormPage)
1606    grok.require('waeup.manageHostels')
1607    icon = 'actionicon_reload.png'
1608    text = 'Relocate student'
1609    target = 'relocate'
1610
1611class BedTicketRelocationPage(grok.View):
1612    """ Callback view
1613    """
1614    grok.context(IBedTicket)
1615    grok.name('relocate')
1616    grok.require('waeup.manageHostels')
1617
[7059]1618    # Relocate student if student parameters have changed or the bed_type
1619    # of the bed has changed
[7015]1620    def update(self):
1621        student = self.context.getStudent()
[7150]1622        students_utils = getUtility(IStudentsUtils)
[7186]1623        acc_details  = students_utils.getAccommodationDetails(student)
[7068]1624        if self.context.bed != None and \
1625              'reserved' in self.context.bed.bed_type:
1626            self.flash("Students in reserved beds can't be relocated.")
1627            self.redirect(self.url(self.context))
1628            return
[7059]1629        if acc_details['bt'] == self.context.bed_type and \
[7068]1630                self.context.bed != None and \
[7059]1631                self.context.bed.bed_type == self.context.bed_type:
[7068]1632            self.flash("Student can't be relocated.")
1633            self.redirect(self.url(self.context))
[7015]1634            return
[7068]1635        # Search a bed
[7015]1636        cat = queryUtility(ICatalog, name='beds_catalog', default=None)
1637        entries = cat.searchResults(
[7068]1638            owner=(student.student_id,student.student_id))
1639        if len(entries) and self.context.bed == None:
1640            # If booking has been cancelled but other bed space has been
1641            # manually allocated after cancellation use this bed
1642            new_bed = [entry for entry in entries][0]
1643        else:
1644            # Search for other available beds
1645            entries = cat.searchResults(
1646                bed_type=(acc_details['bt'],acc_details['bt']))
1647            available_beds = [
1648                entry for entry in entries if entry.owner == NOT_OCCUPIED]
1649            if available_beds:
[7150]1650                students_utils = getUtility(IStudentsUtils)
[7186]1651                new_bed = students_utils.selectBed(available_beds)
[7068]1652                new_bed.bookBed(student.student_id)
1653            else:
1654                self.flash('There is no free bed in your category %s.'
1655                            % acc_details['bt'])
1656                self.redirect(self.url(self.context))
1657                return
1658        # Rlease old bed if exists
1659        if self.context.bed != None:
1660            self.context.bed.owner = NOT_OCCUPIED
1661            notify(grok.ObjectModifiedEvent(self.context.bed))
[7015]1662        # Alocate new bed
1663        self.context.bed_type = acc_details['bt']
[7068]1664        self.context.bed = new_bed
1665        hall_title = new_bed.__parent__.hostel_name
1666        coordinates = new_bed.getBedCoordinates()[1:]
[7015]1667        block, room_nr, bed_nr = coordinates
[7068]1668        self.context.bed_coordinates = '%s, Block %s, Room %s, Bed %s (%s)' % (
1669            hall_title, block, room_nr, bed_nr, new_bed.bed_type)
1670        self.flash('Student relocated: %s' % self.context.bed_coordinates)
[7015]1671        self.redirect(self.url(self.context))
1672        return
1673
1674    def render(self):
[7068]1675        #self.redirect(self.url(self.context, '@@index'))
[7015]1676        return
1677
[7321]1678class StudentHistoryPage(SIRPPage):
[6637]1679    """ Page to display student clearance data
1680    """
1681    grok.context(IStudent)
1682    grok.name('history')
[6660]1683    grok.require('waeup.viewStudent')
[6637]1684    grok.template('studenthistory')
[6642]1685    title = 'History'
1686    pnav = 4
[6637]1687
1688    @property
1689    def label(self):
[7364]1690        return '%s: History' % self.context.display_fullname
[6694]1691
1692# Pages for students only
1693
[7133]1694class StudentBaseActionButton(ManageActionButton):
[6694]1695    grok.order(1)
1696    grok.context(IStudent)
1697    grok.view(StudentBaseDisplayFormPage)
1698    grok.require('waeup.handleStudent')
[7133]1699    text = 'Edit base data'
1700    target = 'edit_base'
1701
1702class StudentPasswordActionButton(ManageActionButton):
1703    grok.order(2)
1704    grok.context(IStudent)
1705    grok.view(StudentBaseDisplayFormPage)
1706    grok.require('waeup.handleStudent')
[7114]1707    icon = 'actionicon_key.png'
[6694]1708    text = 'Change password'
[7114]1709    target = 'change_password'
[6694]1710
[7114]1711class StudentPassportActionButton(ManageActionButton):
[7133]1712    grok.order(3)
[7114]1713    grok.context(IStudent)
1714    grok.view(StudentBaseDisplayFormPage)
1715    grok.require('waeup.handleStudent')
1716    icon = 'actionicon_portrait.png'
1717    text = 'Change portrait'
1718    target = 'change_portrait'
1719
[7133]1720    @property
1721    def target_url(self):
1722        if self.context.state != 'admitted':
1723            return ''
1724        return self.view.url(self.view.context, self.target)
1725
[7321]1726class StudentBaseEditFormPage(SIRPEditFormPage):
[7133]1727    """ View to edit student base data
1728    """
1729    grok.context(IStudent)
1730    grok.name('edit_base')
1731    grok.require('waeup.handleStudent')
1732    form_fields = grok.AutoFields(IStudentBase).select(
1733        'email', 'phone')
1734    label = 'Edit base data'
1735    title = 'Base Data'
1736    pnav = 4
1737
1738    @grok.action('Save')
1739    def save(self, **data):
1740        msave(self, **data)
1741        return
1742
[7321]1743class StudentChangePasswordPage(SIRPEditFormPage):
[7144]1744    """ View to manage student base data
[6756]1745    """
1746    grok.context(IStudent)
[7114]1747    grok.name('change_password')
[6694]1748    grok.require('waeup.handleStudent')
[7144]1749    grok.template('change_password')
[6694]1750    label = 'Change password'
1751    title = 'Base Data'
1752    pnav = 4
1753
[7144]1754    @grok.action('Save')
1755    def save(self, **data):
1756        form = self.request.form
1757        password = form.get('change_password', None)
1758        password_ctl = form.get('change_password_repeat', None)
1759        if password:
[7147]1760            validator = getUtility(IPasswordValidator)
1761            errors = validator.validate_password(password, password_ctl)
1762            if not errors:
1763                IUserAccount(self.context).setPassword(password)
1764                write_log_message(self, 'saved: password')
1765                self.flash('Password changed.')
[6756]1766            else:
[7147]1767                self.flash( ' '.join(errors))
[6756]1768        return
1769
[7321]1770class StudentFilesUploadPage(SIRPPage):
[7114]1771    """ View to upload files by student
1772    """
1773    grok.context(IStudent)
1774    grok.name('change_portrait')
[7127]1775    grok.require('waeup.uploadStudentFile')
[7114]1776    grok.template('filesuploadpage')
1777    label = 'Upload portrait'
1778    title = 'Base Data'
1779    pnav = 4
1780
[7133]1781    def update(self):
1782        if self.context.getStudent().state != 'admitted':
[7145]1783            emit_lock_message(self)
[7133]1784            return
1785        super(StudentFilesUploadPage, self).update()
1786        return
1787
[6719]1788class StudentClearanceStartActionButton(ManageActionButton):
1789    grok.order(1)
1790    grok.context(IStudent)
1791    grok.view(StudentClearanceDisplayFormPage)
1792    grok.require('waeup.handleStudent')
[7116]1793    icon = 'actionicon_start.gif'
[6719]1794    text = 'Start clearance'
1795    target = 'start_clearance'
1796
1797    @property
1798    def target_url(self):
1799        if self.context.state != 'admitted':
1800            return ''
1801        return self.view.url(self.view.context, self.target)
1802
[7321]1803class StartClearancePage(SIRPPage):
[6770]1804    grok.context(IStudent)
1805    grok.name('start_clearance')
1806    grok.require('waeup.handleStudent')
1807    grok.template('enterpin')
1808    title = 'Start clearance'
1809    label = 'Start clearance'
1810    ac_prefix = 'CLR'
1811    notice = ''
1812    pnav = 4
1813    buttonname = 'Start clearance now'
1814
[7133]1815    @property
1816    def all_required_fields_filled(self):
1817        if self.context.email and self.context.phone:
1818            return True
1819        return False
1820
1821    @property
1822    def portrait_uploaded(self):
1823        store = getUtility(IExtFileStore)
1824        if store.getFileByContext(self.context, attr=u'passport.jpg'):
1825            return True
1826        return False
1827
[6770]1828    def update(self, SUBMIT=None):
[6936]1829        if not self.context.state == 'admitted':
1830            self.flash("Wrong state.")
1831            self.redirect(self.url(self.context))
1832            return
[7133]1833        if not self.portrait_uploaded:
1834            self.flash("No portrait uploaded.")
1835            self.redirect(self.url(self.context, 'change_portrait'))
1836            return
1837        if not self.all_required_fields_filled:
1838            self.flash("Not all required fields filled.")
1839            self.redirect(self.url(self.context, 'edit_base'))
1840            return
[6770]1841        self.ac_series = self.request.form.get('ac_series', None)
1842        self.ac_number = self.request.form.get('ac_number', None)
1843
1844        if SUBMIT is None:
1845            return
1846        pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number)
1847        code = get_access_code(pin)
1848        if not code:
[6936]1849            self.flash('Activation code is invalid.')
[6770]1850            return
1851        # Mark pin as used (this also fires a pin related transition)
1852        # and fire transition start_clearance
1853        if code.state == USED:
[6936]1854            self.flash('Activation code has already been used.')
[6770]1855            return
1856        else:
1857            comment = u"AC invalidated for %s" % self.context.student_id
1858            # Here we know that the ac is in state initialized so we do not
[6927]1859            # expect an exception, but the owner might be different
1860            if not invalidate_accesscode(pin,comment,self.context.student_id):
1861                self.flash('You are not the owner of this access code.')
1862                return
[6770]1863            self.context.clr_code = pin
1864        IWorkflowInfo(self.context).fireTransition('start_clearance')
[6771]1865        self.flash('Clearance process has been started.')
[6770]1866        self.redirect(self.url(self.context,'cedit'))
1867        return
1868
[6695]1869class StudentClearanceEditActionButton(ManageActionButton):
1870    grok.order(1)
1871    grok.context(IStudent)
1872    grok.view(StudentClearanceDisplayFormPage)
1873    grok.require('waeup.handleStudent')
[6722]1874    text = 'Edit'
[6695]1875    target = 'cedit'
1876
[6717]1877    @property
1878    def target_url(self):
1879        if self.context.clearance_locked:
1880            return ''
1881        return self.view.url(self.view.context, self.target)
1882
[6695]1883class StudentClearanceEditFormPage(StudentClearanceManageFormPage):
1884    """ View to edit student clearance data by student
1885    """
1886    grok.context(IStudent)
1887    grok.name('cedit')
1888    grok.require('waeup.handleStudent')
[6756]1889    form_fields = grok.AutoFields(
1890        IStudentClearanceEdit).omit('clearance_locked')
[6722]1891    label = 'Edit clearance data'
[6695]1892    title = 'Clearance Data'
1893    form_fields['date_of_birth'].custom_widget = FriendlyDateWidget('le-year')
[6718]1894
1895    def update(self):
1896        if self.context.clearance_locked:
[7145]1897            emit_lock_message(self)
[6718]1898            return
1899        return super(StudentClearanceEditFormPage, self).update()
[6719]1900
[6722]1901    @grok.action('Save')
1902    def save(self, **data):
1903        self.applyData(self.context, **data)
[6771]1904        self.flash('Clearance form has been saved.')
[6722]1905        return
1906
[7253]1907    # To be specified in the customisation package
1908    def dataNotComplete(self):
1909        #store = getUtility(IExtFileStore)
1910        #if not store.getFileByContext(self.context, attr=u'xyz.jpg'):
1911        #    return 'No xyz scan uploaded.'
1912        return False
1913
[6722]1914    @grok.action('Save and request clearance')
[7186]1915    def requestClearance(self, **data):
[6722]1916        self.applyData(self.context, **data)
[7275]1917        #self.context._p_changed = True
[7253]1918        if self.dataNotComplete():
1919            self.flash(self.dataNotComplete())
1920            return
[6771]1921        self.flash('Clearance form has been saved.')
[6769]1922        self.redirect(self.url(self.context,'request_clearance'))
[6722]1923        return
1924
[7321]1925class RequestClearancePage(SIRPPage):
[6769]1926    grok.context(IStudent)
1927    grok.name('request_clearance')
1928    grok.require('waeup.handleStudent')
1929    grok.template('enterpin')
1930    title = 'Request clearance'
1931    label = 'Request clearance'
1932    notice = 'Enter the CLR access code used for starting clearance.'
1933    ac_prefix = 'CLR'
1934    pnav = 4
1935    buttonname = 'Request clearance now'
1936
1937    def update(self, SUBMIT=None):
1938        self.ac_series = self.request.form.get('ac_series', None)
1939        self.ac_number = self.request.form.get('ac_number', None)
1940        if SUBMIT is None:
1941            return
1942        pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number)
1943        if self.context.clr_code != pin:
1944            self.flash("This isn't your CLR access code.")
1945            return
1946        state = IWorkflowState(self.context).getState()
1947        # This shouldn't happen, but the application officer
1948        # might have forgotten to lock the form after changing the state
1949        if state != CLEARANCE:
1950            self.flash('This form cannot be submitted. Wrong state!')
1951            return
1952        IWorkflowInfo(self.context).fireTransition('request_clearance')
1953        self.flash('Clearance has been requested.')
1954        self.redirect(self.url(self.context))
[6789]1955        return
[6806]1956
[6944]1957class CourseRegistrationStartActionButton(ManageActionButton):
1958    grok.order(1)
1959    grok.context(IStudentStudyCourse)
1960    grok.view(StudyCourseDisplayFormPage)
1961    grok.require('waeup.handleStudent')
[7116]1962    icon = 'actionicon_start.gif'
[6944]1963    text = 'Start course registration'
1964    target = 'start_course_registration'
1965
1966    @property
1967    def target_url(self):
1968        if not self.context.getStudent().state in (CLEARED,RETURNING):
1969            return ''
1970        return self.view.url(self.view.context, self.target)
1971
[7321]1972class StartCourseRegistrationPage(SIRPPage):
[6944]1973    grok.context(IStudentStudyCourse)
1974    grok.name('start_course_registration')
1975    grok.require('waeup.handleStudent')
1976    grok.template('enterpin')
1977    title = 'Start course registration'
1978    label = 'Start course registration'
1979    ac_prefix = 'SFE'
1980    notice = ''
1981    pnav = 4
1982    buttonname = 'Start course registration now'
1983
1984    def update(self, SUBMIT=None):
1985        if not self.context.getStudent().state in (CLEARED,RETURNING):
1986            self.flash("Wrong state.")
1987            self.redirect(self.url(self.context))
1988            return
1989        self.ac_series = self.request.form.get('ac_series', None)
1990        self.ac_number = self.request.form.get('ac_number', None)
1991
1992        if SUBMIT is None:
1993            return
1994        pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number)
1995        code = get_access_code(pin)
1996        if not code:
1997            self.flash('Activation code is invalid.')
1998            return
1999        # Mark pin as used (this also fires a pin related transition)
2000        # and fire transition start_clearance
2001        if code.state == USED:
2002            self.flash('Activation code has already been used.')
2003            return
2004        else:
2005            comment = u"AC invalidated for %s" % self.context.getStudent().student_id
2006            # Here we know that the ac is in state initialized so we do not
2007            # expect an exception, but the owner might be different
2008            if not invalidate_accesscode(
2009                pin,comment,self.context.getStudent().student_id):
2010                self.flash('You are not the owner of this access code.')
2011                return
2012        if self.context.getStudent().state == CLEARED:
2013            IWorkflowInfo(self.context.getStudent()).fireTransition(
2014                'pay_first_school_fee')
2015        elif self.context.getStudent().state == RETURNING:
2016            IWorkflowInfo(self.context.getStudent()).fireTransition(
2017                'pay_school_fee')
2018        self.flash('Course registration has been started.')
2019        self.redirect(self.url(self.context))
2020        return
2021
2022
[6808]2023class AddStudyLevelActionButton(AddActionButton):
2024    grok.order(1)
2025    grok.context(IStudentStudyCourse)
2026    grok.view(StudyCourseDisplayFormPage)
2027    grok.require('waeup.handleStudent')
2028    text = 'Add course list'
2029    target = 'add'
2030
2031    @property
2032    def target_url(self):
2033        student = self.view.context.getStudent()
2034        condition1 = student.state != 'school fee paid'
2035        condition2 = str(student['studycourse'].current_level) in \
2036            self.view.context.keys()
2037        if condition1 or condition2:
2038            return ''
2039        return self.view.url(self.view.context, self.target)
2040
[7321]2041class AddStudyLevelFormPage(SIRPEditFormPage):
[6806]2042    """ Page for students to add current study levels
2043    """
2044    grok.context(IStudentStudyCourse)
2045    grok.name('add')
2046    grok.require('waeup.handleStudent')
2047    grok.template('studyleveladdpage')
2048    form_fields = grok.AutoFields(IStudentStudyCourse)
2049    title = 'Study Course'
2050    pnav = 4
2051
2052    @property
2053    def label(self):
2054        studylevelsource = StudyLevelSource().factory
2055        code = self.context.current_level
2056        title = studylevelsource.getTitle(self.context, code)
2057        return 'Add current level %s' % title
2058
2059    def update(self):
2060        if self.context.getStudent().state != 'school fee paid':
[7145]2061            emit_lock_message(self)
[6806]2062            return
2063        super(AddStudyLevelFormPage, self).update()
2064        return
2065
2066    @grok.action('Create course list now')
2067    def addStudyLevel(self, **data):
2068        studylevel = StudentStudyLevel()
2069        studylevel.level = self.context.current_level
2070        studylevel.level_session = self.context.current_session
2071        try:
2072            self.context.addStudentStudyLevel(
2073                self.context.certificate,studylevel)
2074        except KeyError:
2075            self.flash('This level exists.')
2076        self.redirect(self.url(self.context))
2077        return
[6808]2078
2079class StudyLevelEditActionButton(ManageActionButton):
2080    grok.order(1)
2081    grok.context(IStudentStudyLevel)
2082    grok.view(StudyLevelDisplayFormPage)
2083    grok.require('waeup.handleStudent')
2084    text = 'Add and remove courses'
2085    target = 'edit'
2086
2087    @property
2088    def target_url(self):
2089        student = self.view.context.getStudent()
2090        condition1 = student.state != 'school fee paid'
2091        condition2 = student[
2092            'studycourse'].current_level != self.view.context.level
2093        if condition1 or condition2:
2094            return ''
2095        return self.view.url(self.view.context, self.target)
2096
[7321]2097class StudyLevelEditFormPage(SIRPEditFormPage):
[6808]2098    """ Page to edit the student study level data by students
2099    """
2100    grok.context(IStudentStudyLevel)
2101    grok.name('edit')
2102    grok.require('waeup.handleStudent')
2103    grok.template('studyleveleditpage')
2104    form_fields = grok.AutoFields(IStudentStudyLevel).omit(
2105        'level_session', 'level_verdict')
2106    pnav = 4
2107
2108    def update(self):
2109        super(StudyLevelEditFormPage, self).update()
2110    #    tabs.need()
2111        datatable.need()
[7329]2112        warning.need()
[6808]2113        return
2114
2115    @property
2116    def title(self):
2117        return 'Study Level %s' % self.context.level_title
2118
2119    @property
2120    def label(self):
2121        return 'Add and remove course tickets of study level %s' % self.context.level_title
2122
2123    @property
2124    def total_credits(self):
2125        total_credits = 0
2126        for key, val in self.context.items():
2127            total_credits += val.credits
2128        return total_credits
2129
2130    @grok.action('Add course ticket')
2131    def addCourseTicket(self, **data):
2132        self.redirect(self.url(self.context, 'ctadd'))
2133
[7329]2134    @jsaction('Remove selected tickets')
[6808]2135    def delCourseTicket(self, **data):
2136        form = self.request.form
2137        if form.has_key('val_id'):
2138            child_id = form['val_id']
2139        else:
2140            self.flash('No ticket selected.')
2141            self.redirect(self.url(self.context, '@@edit'))
2142            return
2143        if not isinstance(child_id, list):
2144            child_id = [child_id]
2145        deleted = []
2146        for id in child_id:
[6940]2147            # Students are not allowed to remove core tickets
[6808]2148            if not self.context[id].core_or_elective:
2149                try:
2150                    del self.context[id]
2151                    deleted.append(id)
2152                except:
2153                    self.flash('Could not delete %s: %s: %s' % (
2154                            id, sys.exc_info()[0], sys.exc_info()[1]))
2155        if len(deleted):
2156            self.flash('Successfully removed: %s' % ', '.join(deleted))
2157        self.redirect(self.url(self.context, u'@@edit'))
2158        return
2159
[6810]2160    @grok.action('Register course list')
[7186]2161    def RegisterCourses(self, **data):
[6810]2162        IWorkflowInfo(self.context.getStudent()).fireTransition('register_courses')
2163        self.flash('Course list has been registered.')
2164        self.redirect(self.url(self.context))
2165        return
2166
[6808]2167class CourseTicketAddFormPage2(CourseTicketAddFormPage):
2168    """Add a course ticket by student.
2169    """
2170    grok.name('ctadd')
2171    grok.require('waeup.handleStudent')
2172    form_fields = grok.AutoFields(ICourseTicketAdd).omit(
2173        'grade', 'score', 'core_or_elective', 'automatic')
2174
2175    @grok.action('Add course ticket')
2176    def addCourseTicket(self, **data):
2177        ticket = CourseTicket()
2178        course = data['course']
2179        ticket.automatic = False
2180        ticket.code = course.code
2181        ticket.title = course.title
2182        ticket.faculty = course.__parent__.__parent__.__parent__.title
2183        ticket.department = course.__parent__.__parent__.title
2184        ticket.credits = course.credits
2185        ticket.passmark = course.passmark
2186        ticket.semester = course.semester
2187        try:
2188            self.context.addCourseTicket(ticket)
2189        except KeyError:
2190            self.flash('The ticket exists.')
2191            return
2192        self.flash('Successfully added %s.' % ticket.code)
2193        self.redirect(self.url(self.context, u'@@edit'))
2194        return
[7369]2195
2196class ChangePasswordRequestPage(SIRPForm):
2197    """Captcha'd page for students to request a password change.
2198    """
2199    grok.context(IUniversity)
2200    grok.name('changepw')
2201    grok.require('waeup.Anonymous')
2202    grok.template('changepw')
2203    title = 'Change Password Request'
2204    label = 'Change my password'
2205    form_fields = grok.AutoFields(IStudentChangePassword)
2206
2207    def update(self):
2208        # Handle captcha
2209        self.captcha = getUtility(ICaptchaManager).getCaptcha()
2210        self.captcha_result = self.captcha.verify(self.request)
2211        self.captcha_code = self.captcha.display(self.captcha_result.error_code)
2212        return
2213
2214    @grok.action('Send request')
2215    def request(self, **data):
2216        if not self.captcha_result.is_valid:
2217            # Captcha will display error messages automatically.
2218            # No need to flash something.
2219            return
2220        # Search student
2221        cat = queryUtility(ICatalog, name='students_catalog')
2222        reg_number = data['reg_number']
2223        email = data['email']
2224        results = cat.searchResults(
2225            reg_number=(reg_number, reg_number),
2226            email=(email,email))
2227        if len(results) == 0:
2228            self.flash('No student record found.')
2229            return
2230        student = list(results)[0]
2231        # Change password
2232        sirp_utils = getUtility(ISIRPUtils)
2233        pwd = sirp_utils.genPassword()
2234        IUserAccount(student).setPassword(pwd)
2235        # Send email with new redentials
2236        msg = 'You have successfully changed your password for the'
2237        login_url = self.url(grok.getSite(), 'login')
[7407]2238        success = sirp_utils.sendCredentials(
2239            IUserAccount(student),pwd,login_url,msg)
[7369]2240        if success:
2241            self.flash('An email with your user name and password ' +
[7407]2242                'has been sent to %s.' % email)
[7369]2243        else:
2244            self.flash('An smtp server error occurred.')
2245        return
Note: See TracBrowser for help on using the repository browser.