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

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

Minor changes.

  • Property svn:keywords set to Id
File size: 77.0 KB
RevLine 
[7191]1## $Id: browser.py 7424 2011-12-21 11:38:39Z 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')
1198    label = 'Add online payment'
1199    title = 'Payments'
1200    pnav = 4
[6947]1201   
[6869]1202    @grok.action('Create ticket')
1203    def createTicket(self, **data):
[7024]1204        p_category = data['p_category']
1205        student = self.context.__parent__
1206        if p_category == 'bed_allocation' and student[
1207            'studycourse'].current_session != grok.getSite()[
1208            'configuration'].accommodation_session:
1209                self.flash(
1210                    'Your current session does not match accommodation session.')
1211                self.redirect(self.url(self.context))
1212                return
[7150]1213        students_utils = getUtility(IStudentsUtils)
[7186]1214        pay_details  = students_utils.getPaymentDetails(
[7024]1215            p_category,student)
[6994]1216        if pay_details['error']:
1217            self.flash(pay_details['error'])
[6940]1218            self.redirect(self.url(self.context))
[6920]1219            return
[7025]1220        p_item = pay_details['p_item']
1221        p_session = pay_details['p_session']
1222        for key in self.context.keys():
1223            ticket = self.context[key]
[7248]1224            if ticket.p_state == 'paid' and\
1225               ticket.p_category == p_category and \
[7025]1226               ticket.p_item == p_item and \
1227               ticket.p_session == p_session:
1228                  self.flash(
[7251]1229                      'This type of payment has already been made.')
[7025]1230                  self.redirect(self.url(self.context))
1231                  return
1232        payment = createObject(u'waeup.StudentOnlinePayment')
1233        self.applyData(payment, **data)
1234        timestamp = "%d" % int(time()*1000)
1235        #order_id = "%s%s" % (student_id[1:],timestamp)
1236        payment.p_id = "p%s" % timestamp
1237        payment.p_item = p_item
1238        payment.p_session = p_session
[6994]1239        payment.amount_auth = pay_details['amount']
1240        payment.surcharge_1 = pay_details['surcharge_1']
1241        payment.surcharge_2 = pay_details['surcharge_2']
1242        payment.surcharge_3 = pay_details['surcharge_3']
[6869]1243        self.context[payment.p_id] = payment
1244        self.flash('Payment ticket created.')
[6940]1245        self.redirect(self.url(self.context))
[6869]1246        return
1247
[7321]1248class OnlinePaymentDisplayFormPage(SIRPDisplayFormPage):
[6869]1249    """ Page to view an online payment ticket
1250    """
[6877]1251    grok.context(IStudentOnlinePayment)
[6869]1252    grok.name('index')
1253    grok.require('waeup.viewStudent')
[6877]1254    form_fields = grok.AutoFields(IStudentOnlinePayment)
[6869]1255    form_fields['creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
1256    form_fields['payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
1257    pnav = 4
1258
1259    @property
1260    def title(self):
1261        return 'Online Payment Ticket %s' % self.context.p_id
1262
1263    @property
1264    def label(self):
1265        return '%s: Online Payment Ticket %s' % (
[7364]1266            self.context.getStudent().display_fullname,self.context.p_id)
[6869]1267
[7019]1268class PaymentReceiptActionButton(ManageActionButton):
[7028]1269    grok.order(1)
[7019]1270    grok.context(IStudentOnlinePayment)
[7029]1271    grok.view(OnlinePaymentDisplayFormPage)
[7019]1272    grok.require('waeup.viewStudent')
1273    icon = 'actionicon_pdf.png'
1274    text = 'Download payment receipt'
1275    target = 'payment_receipt.pdf'
1276
[7028]1277    @property
1278    def target_url(self):
1279        if self.context.p_state != 'paid':
1280            return ''
1281        return self.view.url(self.view.context, self.target)
1282
[7056]1283class RequestCallbackActionButton(ManageActionButton):
1284    grok.order(2)
1285    grok.context(IStudentOnlinePayment)
1286    grok.view(OnlinePaymentDisplayFormPage)
[7181]1287    grok.require('waeup.payStudent')
[7056]1288    icon = 'actionicon_call.png'
1289    text = 'Request callback'
1290    target = 'callback'
1291
1292    @property
1293    def target_url(self):
1294        if self.context.p_state != 'unpaid':
1295            return ''
1296        return self.view.url(self.view.context, self.target)
1297
[6930]1298class OnlinePaymentCallbackPage(grok.View):
1299    """ Callback view
1300    """
1301    grok.context(IStudentOnlinePayment)
1302    grok.name('callback')
1303    grok.require('waeup.payStudent')
1304
1305    # This update method simulates a valid callback und must be
1306    # specified in the customization package. The parameters must be taken
1307    # from the incoming request.
1308    def update(self):
[7026]1309        if self.context.p_state == 'paid':
1310            self.flash('This ticket has already been paid.')
1311            return
[6936]1312        student = self.context.getStudent()
[6943]1313        write_log_message(self,'valid callback: %s' % self.context.p_id)
[6930]1314        self.context.r_amount_approved = self.context.amount_auth
1315        self.context.r_card_num = u'0000'
1316        self.context.r_code = u'00'
1317        self.context.p_state = 'paid'
1318        self.context.payment_date = datetime.now()
1319        if self.context.p_category == 'clearance':
[6936]1320            # Create CLR access code
[6937]1321            pin, error = create_accesscode('CLR',0,student.student_id)
[6936]1322            if error:
[6940]1323                self.flash('Valid callback received. ' + error)
[6936]1324                return
1325            self.context.ac = pin
[6930]1326        elif self.context.p_category == 'schoolfee':
[6936]1327            # Create SFE access code
[6940]1328            pin, error = create_accesscode('SFE',0,student.student_id)
[6936]1329            if error:
[6940]1330                self.flash('Valid callback received. ' + error)
[6936]1331                return
1332            self.context.ac = pin
[6994]1333        elif self.context.p_category == 'bed_allocation':
1334            # Create HOS access code
1335            pin, error = create_accesscode('HOS',0,student.student_id)
1336            if error:
1337                self.flash('Valid callback received. ' + error)
1338                return
1339            self.context.ac = pin
[6940]1340        self.flash('Valid callback received.')
1341        return
[6930]1342
1343    def render(self):
[6940]1344        self.redirect(self.url(self.context, '@@index'))
[6930]1345        return
1346
[7019]1347class ExportPDFPaymentSlipPage(grok.View):
1348    """Deliver a PDF slip of the context.
1349    """
1350    grok.context(IStudentOnlinePayment)
1351    grok.name('payment_receipt.pdf')
1352    grok.require('waeup.viewStudent')
1353    form_fields = grok.AutoFields(IStudentOnlinePayment)
1354    form_fields['creation_date'].custom_widget = FriendlyDateDisplayWidget('le')
1355    form_fields['payment_date'].custom_widget = FriendlyDateDisplayWidget('le')
1356    prefix = 'form'
[7318]1357    title = 'Payment Data'
[7019]1358
1359    @property
1360    def label(self):
1361        return 'Online Payment Receipt %s' % self.context.p_id
1362
1363    def render(self):
[7028]1364        if self.context.p_state != 'paid':
1365            self.flash('Ticket not yet paid.')
1366            self.redirect(self.url(self.context))
1367            return
[7019]1368        studentview = StudentBaseDisplayFormPage(self.context.getStudent(),
1369            self.request)
[7150]1370        students_utils = getUtility(IStudentsUtils)
[7318]1371        return students_utils.renderPDF(self, 'payment_receipt.pdf',
[7310]1372            self.context.getStudent(), studentview)
[7019]1373
[6992]1374# We don't need the display form page yet
[7321]1375#class AccommodationDisplayFormPage(SIRPDisplayFormPage):
[6992]1376#    """ Page to display the student accommodation data
1377#    """
1378#    grok.context(IStudentAccommodation)
1379#    grok.name('xxx')
1380#    grok.require('waeup.viewStudent')
1381#    form_fields = grok.AutoFields(IStudentAccommodation)
1382#    #grok.template('accommodationpage')
1383#    title = 'Accommodation'
1384#    pnav = 4
1385
1386#    @property
1387#    def label(self):
[7364]1388#        return '%s: Accommodation Data' % self.context.__parent__.display_fullname
[6992]1389
1390# This manage form page is for both students and students officers.
[7321]1391class AccommodationManageFormPage(SIRPEditFormPage):
[7009]1392    """ Page to manage bed tickets.
[6635]1393    """
1394    grok.context(IStudentAccommodation)
1395    grok.name('index')
[7181]1396    grok.require('waeup.handleAccommodation')
[6635]1397    form_fields = grok.AutoFields(IStudentAccommodation)
[6992]1398    grok.template('accommodationmanagepage')
[6642]1399    title = 'Accommodation'
1400    pnav = 4
[7017]1401    officers_only_actions = ['Remove selected']
[6635]1402
1403    @property
1404    def label(self):
[7364]1405        return '%s: Accommodation' % self.context.__parent__.display_fullname
[6637]1406
[6992]1407    def update(self):
1408        super(AccommodationManageFormPage, self).update()
1409        datatable.need()
[7329]1410        warning.need()
[6992]1411        return
1412
[7329]1413    @jsaction('Remove selected')
[7009]1414    def delBedTickets(self, **data):
[7240]1415        if getattr(self.request.principal, 'user_type', None) == 'student':
[7017]1416            self.flash('You are not allowed to remove bed tickets.')
1417            self.redirect(self.url(self.context))
1418            return
[6992]1419        form = self.request.form
1420        if form.has_key('val_id'):
1421            child_id = form['val_id']
1422        else:
1423            self.flash('No bed ticket selected.')
1424            self.redirect(self.url(self.context))
1425            return
1426        if not isinstance(child_id, list):
1427            child_id = [child_id]
1428        deleted = []
1429        for id in child_id:
[7068]1430            del self.context[id]
1431            deleted.append(id)
[6992]1432        if len(deleted):
1433            self.flash('Successfully removed: %s' % ', '.join(deleted))
1434            write_log_message(self,'removed: % s' % ', '.join(deleted))
1435        self.redirect(self.url(self.context))
1436        return
1437
[7009]1438    @property
1439    def selected_actions(self):
1440        sa = self.actions
[7240]1441        if getattr(self.request.principal, 'user_type', None) == 'student':
[7009]1442            sa = [action for action in self.actions
[7017]1443                  if not action.label in self.officers_only_actions]
[7009]1444        return sa
1445
[7015]1446class AddBedTicketActionButton(ManageActionButton):
1447    grok.order(1)
1448    grok.context(IStudentAccommodation)
1449    grok.view(AccommodationManageFormPage)
[7181]1450    grok.require('waeup.handleAccommodation')
[7015]1451    icon = 'actionicon_home.png'
1452    text = 'Book accommodation'
1453    target = 'add'
1454
[7321]1455class BedTicketAddPage(SIRPPage):
[6992]1456    """ Page to add an online payment ticket
1457    """
1458    grok.context(IStudentAccommodation)
1459    grok.name('add')
[7181]1460    grok.require('waeup.handleAccommodation')
[6992]1461    grok.template('enterpin')
[6993]1462    ac_prefix = 'HOS'
[6992]1463    label = 'Add bed ticket'
1464    title = 'Add bed ticket'
1465    pnav = 4
1466    buttonname = 'Create bed ticket'
[6993]1467    notice = ''
[6992]1468
1469    def update(self, SUBMIT=None):
[6996]1470        student = self.context.getStudent()
[7150]1471        students_utils = getUtility(IStudentsUtils)
[7186]1472        acc_details  = students_utils.getAccommodationDetails(student)
[7369]1473        if not acc_details:
1474            self.flash("Your data are incomplete.")
1475            self.redirect(self.url(self.context))
1476            return
[6996]1477        if not student.state in acc_details['allowed_states']:
[7015]1478            self.flash("You are in the wrong registration state.")
[6992]1479            self.redirect(self.url(self.context))
1480            return
[7061]1481        if student['studycourse'].current_session != acc_details['booking_session']:
1482            self.flash(
1483                'Your current session does not match accommodation session.')
1484            self.redirect(self.url(self.context))
1485            return
1486        if str(acc_details['booking_session']) in self.context.keys():
[7060]1487            self.flash('You already booked a bed space in current accommodation session.')
[7004]1488            self.redirect(self.url(self.context))
1489            return
[6992]1490        self.ac_series = self.request.form.get('ac_series', None)
1491        self.ac_number = self.request.form.get('ac_number', None)
1492        if SUBMIT is None:
1493            return
1494        pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number)
1495        code = get_access_code(pin)
1496        if not code:
1497            self.flash('Activation code is invalid.')
1498            return
[7060]1499        # Search and book bed
[6997]1500        cat = queryUtility(ICatalog, name='beds_catalog', default=None)
1501        entries = cat.searchResults(
[7003]1502            owner=(student.student_id,student.student_id))
1503        if len(entries):
[7060]1504            # If bed space has bee manually allocated use this bed
[7003]1505            bed = [entry for entry in entries][0]
[7060]1506        else:
1507            # else search for other available beds
1508            entries = cat.searchResults(
1509                bed_type=(acc_details['bt'],acc_details['bt']))
1510            available_beds = [
1511                entry for entry in entries if entry.owner == NOT_OCCUPIED]
1512            if available_beds:
[7150]1513                students_utils = getUtility(IStudentsUtils)
[7186]1514                bed = students_utils.selectBed(available_beds)
[7060]1515                bed.bookBed(student.student_id)
1516            else:
1517                self.flash('There is no free bed in your category %s.'
1518                            % acc_details['bt'])
1519                return
[6992]1520        # Mark pin as used (this also fires a pin related transition)
1521        if code.state == USED:
1522            self.flash('Activation code has already been used.')
1523            return
1524        else:
1525            comment = u"AC invalidated for %s" % self.context.getStudent().student_id
1526            # Here we know that the ac is in state initialized so we do not
1527            # expect an exception, but the owner might be different
1528            if not invalidate_accesscode(
1529                pin,comment,self.context.getStudent().student_id):
1530                self.flash('You are not the owner of this access code.')
1531                return
[7060]1532        # Create bed ticket
[6992]1533        bedticket = createObject(u'waeup.BedTicket')
1534        bedticket.booking_code = pin
[6994]1535        bedticket.booking_session = acc_details['booking_session']
[6996]1536        bedticket.bed_type = acc_details['bt']
[7006]1537        bedticket.bed = bed
[6996]1538        hall_title = bed.__parent__.hostel_name
1539        coordinates = bed.getBedCoordinates()[1:]
1540        block, room_nr, bed_nr = coordinates
[7068]1541        bedticket.bed_coordinates = '%s, Block %s, Room %s, Bed %s (%s)' % (
1542            hall_title, block, room_nr, bed_nr, bed.bed_type)
[6996]1543        key = str(acc_details['booking_session'])
1544        self.context[key] = bedticket
1545        self.flash('Bed ticket created and bed booked: %s'
1546            % bedticket.bed_coordinates)
[6992]1547        self.redirect(self.url(self.context))
1548        return
1549
[7321]1550class BedTicketDisplayFormPage(SIRPDisplayFormPage):
[6994]1551    """ Page to display bed tickets
1552    """
1553    grok.context(IBedTicket)
1554    grok.name('index')
[7181]1555    grok.require('waeup.handleAccommodation')
[6994]1556    form_fields = grok.AutoFields(IBedTicket)
1557    form_fields[
1558        'booking_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
1559    pnav = 4
1560
1561    @property
1562    def label(self):
1563        return 'Bed Ticket for Session %s' % self.context.getSessionString()
1564
1565    @property
1566    def title(self):
1567        return 'Bed Ticket %s' % self.context.getSessionString()
1568
[7027]1569class BedTicketSlipActionButton(ManageActionButton):
[7028]1570    grok.order(1)
[7027]1571    grok.context(IBedTicket)
[7029]1572    grok.view(BedTicketDisplayFormPage)
[7181]1573    grok.require('waeup.handleAccommodation')
[7027]1574    icon = 'actionicon_pdf.png'
1575    text = 'Download bed allocation slip'
1576    target = 'bed_allocation.pdf'
1577
1578class ExportPDFBedTicketSlipPage(grok.View):
1579    """Deliver a PDF slip of the context.
1580    """
1581    grok.context(IBedTicket)
1582    grok.name('bed_allocation.pdf')
[7181]1583    grok.require('waeup.handleAccommodation')
[7027]1584    form_fields = grok.AutoFields(IBedTicket)
1585    form_fields['booking_date'].custom_widget = FriendlyDateDisplayWidget('le')
1586    prefix = 'form'
[7318]1587    title = 'Bed Allocation Data'
[7027]1588
1589    @property
1590    def label(self):
[7318]1591        return 'Bed Allocation: %s' % self.context.bed_coordinates
[7027]1592
1593    def render(self):
1594        studentview = StudentBaseDisplayFormPage(self.context.getStudent(),
1595            self.request)
[7150]1596        students_utils = getUtility(IStudentsUtils)
[7186]1597        return students_utils.renderPDF(
[7318]1598            self, 'bed_allocation.pdf',
[7310]1599            self.context.getStudent(), studentview)
[7027]1600
[7015]1601class RelocateStudentActionButton(ManageActionButton):
[7027]1602    grok.order(2)
[7015]1603    grok.context(IBedTicket)
1604    grok.view(BedTicketDisplayFormPage)
1605    grok.require('waeup.manageHostels')
1606    icon = 'actionicon_reload.png'
1607    text = 'Relocate student'
1608    target = 'relocate'
1609
1610class BedTicketRelocationPage(grok.View):
1611    """ Callback view
1612    """
1613    grok.context(IBedTicket)
1614    grok.name('relocate')
1615    grok.require('waeup.manageHostels')
1616
[7059]1617    # Relocate student if student parameters have changed or the bed_type
1618    # of the bed has changed
[7015]1619    def update(self):
1620        student = self.context.getStudent()
[7150]1621        students_utils = getUtility(IStudentsUtils)
[7186]1622        acc_details  = students_utils.getAccommodationDetails(student)
[7068]1623        if self.context.bed != None and \
1624              'reserved' in self.context.bed.bed_type:
1625            self.flash("Students in reserved beds can't be relocated.")
1626            self.redirect(self.url(self.context))
1627            return
[7059]1628        if acc_details['bt'] == self.context.bed_type and \
[7068]1629                self.context.bed != None and \
[7059]1630                self.context.bed.bed_type == self.context.bed_type:
[7068]1631            self.flash("Student can't be relocated.")
1632            self.redirect(self.url(self.context))
[7015]1633            return
[7068]1634        # Search a bed
[7015]1635        cat = queryUtility(ICatalog, name='beds_catalog', default=None)
1636        entries = cat.searchResults(
[7068]1637            owner=(student.student_id,student.student_id))
1638        if len(entries) and self.context.bed == None:
1639            # If booking has been cancelled but other bed space has been
1640            # manually allocated after cancellation use this bed
1641            new_bed = [entry for entry in entries][0]
1642        else:
1643            # Search for other available beds
1644            entries = cat.searchResults(
1645                bed_type=(acc_details['bt'],acc_details['bt']))
1646            available_beds = [
1647                entry for entry in entries if entry.owner == NOT_OCCUPIED]
1648            if available_beds:
[7150]1649                students_utils = getUtility(IStudentsUtils)
[7186]1650                new_bed = students_utils.selectBed(available_beds)
[7068]1651                new_bed.bookBed(student.student_id)
1652            else:
1653                self.flash('There is no free bed in your category %s.'
1654                            % acc_details['bt'])
1655                self.redirect(self.url(self.context))
1656                return
1657        # Rlease old bed if exists
1658        if self.context.bed != None:
1659            self.context.bed.owner = NOT_OCCUPIED
1660            notify(grok.ObjectModifiedEvent(self.context.bed))
[7015]1661        # Alocate new bed
1662        self.context.bed_type = acc_details['bt']
[7068]1663        self.context.bed = new_bed
1664        hall_title = new_bed.__parent__.hostel_name
1665        coordinates = new_bed.getBedCoordinates()[1:]
[7015]1666        block, room_nr, bed_nr = coordinates
[7068]1667        self.context.bed_coordinates = '%s, Block %s, Room %s, Bed %s (%s)' % (
1668            hall_title, block, room_nr, bed_nr, new_bed.bed_type)
1669        self.flash('Student relocated: %s' % self.context.bed_coordinates)
[7015]1670        self.redirect(self.url(self.context))
1671        return
1672
1673    def render(self):
[7068]1674        #self.redirect(self.url(self.context, '@@index'))
[7015]1675        return
1676
[7321]1677class StudentHistoryPage(SIRPPage):
[6637]1678    """ Page to display student clearance data
1679    """
1680    grok.context(IStudent)
1681    grok.name('history')
[6660]1682    grok.require('waeup.viewStudent')
[6637]1683    grok.template('studenthistory')
[6642]1684    title = 'History'
1685    pnav = 4
[6637]1686
1687    @property
1688    def label(self):
[7364]1689        return '%s: History' % self.context.display_fullname
[6694]1690
1691# Pages for students only
1692
[7133]1693class StudentBaseActionButton(ManageActionButton):
[6694]1694    grok.order(1)
1695    grok.context(IStudent)
1696    grok.view(StudentBaseDisplayFormPage)
1697    grok.require('waeup.handleStudent')
[7133]1698    text = 'Edit base data'
1699    target = 'edit_base'
1700
1701class StudentPasswordActionButton(ManageActionButton):
1702    grok.order(2)
1703    grok.context(IStudent)
1704    grok.view(StudentBaseDisplayFormPage)
1705    grok.require('waeup.handleStudent')
[7114]1706    icon = 'actionicon_key.png'
[6694]1707    text = 'Change password'
[7114]1708    target = 'change_password'
[6694]1709
[7114]1710class StudentPassportActionButton(ManageActionButton):
[7133]1711    grok.order(3)
[7114]1712    grok.context(IStudent)
1713    grok.view(StudentBaseDisplayFormPage)
1714    grok.require('waeup.handleStudent')
1715    icon = 'actionicon_portrait.png'
1716    text = 'Change portrait'
1717    target = 'change_portrait'
1718
[7133]1719    @property
1720    def target_url(self):
1721        if self.context.state != 'admitted':
1722            return ''
1723        return self.view.url(self.view.context, self.target)
1724
[7321]1725class StudentBaseEditFormPage(SIRPEditFormPage):
[7133]1726    """ View to edit student base data
1727    """
1728    grok.context(IStudent)
1729    grok.name('edit_base')
1730    grok.require('waeup.handleStudent')
1731    form_fields = grok.AutoFields(IStudentBase).select(
1732        'email', 'phone')
1733    label = 'Edit base data'
1734    title = 'Base Data'
1735    pnav = 4
1736
1737    @grok.action('Save')
1738    def save(self, **data):
1739        msave(self, **data)
1740        return
1741
[7321]1742class StudentChangePasswordPage(SIRPEditFormPage):
[7144]1743    """ View to manage student base data
[6756]1744    """
1745    grok.context(IStudent)
[7114]1746    grok.name('change_password')
[6694]1747    grok.require('waeup.handleStudent')
[7144]1748    grok.template('change_password')
[6694]1749    label = 'Change password'
1750    title = 'Base Data'
1751    pnav = 4
1752
[7144]1753    @grok.action('Save')
1754    def save(self, **data):
1755        form = self.request.form
1756        password = form.get('change_password', None)
1757        password_ctl = form.get('change_password_repeat', None)
1758        if password:
[7147]1759            validator = getUtility(IPasswordValidator)
1760            errors = validator.validate_password(password, password_ctl)
1761            if not errors:
1762                IUserAccount(self.context).setPassword(password)
1763                write_log_message(self, 'saved: password')
1764                self.flash('Password changed.')
[6756]1765            else:
[7147]1766                self.flash( ' '.join(errors))
[6756]1767        return
1768
[7321]1769class StudentFilesUploadPage(SIRPPage):
[7114]1770    """ View to upload files by student
1771    """
1772    grok.context(IStudent)
1773    grok.name('change_portrait')
[7127]1774    grok.require('waeup.uploadStudentFile')
[7114]1775    grok.template('filesuploadpage')
1776    label = 'Upload portrait'
1777    title = 'Base Data'
1778    pnav = 4
1779
[7133]1780    def update(self):
1781        if self.context.getStudent().state != 'admitted':
[7145]1782            emit_lock_message(self)
[7133]1783            return
1784        super(StudentFilesUploadPage, self).update()
1785        return
1786
[6719]1787class StudentClearanceStartActionButton(ManageActionButton):
1788    grok.order(1)
1789    grok.context(IStudent)
1790    grok.view(StudentClearanceDisplayFormPage)
1791    grok.require('waeup.handleStudent')
[7116]1792    icon = 'actionicon_start.gif'
[6719]1793    text = 'Start clearance'
1794    target = 'start_clearance'
1795
1796    @property
1797    def target_url(self):
1798        if self.context.state != 'admitted':
1799            return ''
1800        return self.view.url(self.view.context, self.target)
1801
[7321]1802class StartClearancePage(SIRPPage):
[6770]1803    grok.context(IStudent)
1804    grok.name('start_clearance')
1805    grok.require('waeup.handleStudent')
1806    grok.template('enterpin')
1807    title = 'Start clearance'
1808    label = 'Start clearance'
1809    ac_prefix = 'CLR'
1810    notice = ''
1811    pnav = 4
1812    buttonname = 'Start clearance now'
1813
[7133]1814    @property
1815    def all_required_fields_filled(self):
1816        if self.context.email and self.context.phone:
1817            return True
1818        return False
1819
1820    @property
1821    def portrait_uploaded(self):
1822        store = getUtility(IExtFileStore)
1823        if store.getFileByContext(self.context, attr=u'passport.jpg'):
1824            return True
1825        return False
1826
[6770]1827    def update(self, SUBMIT=None):
[6936]1828        if not self.context.state == 'admitted':
1829            self.flash("Wrong state.")
1830            self.redirect(self.url(self.context))
1831            return
[7133]1832        if not self.portrait_uploaded:
1833            self.flash("No portrait uploaded.")
1834            self.redirect(self.url(self.context, 'change_portrait'))
1835            return
1836        if not self.all_required_fields_filled:
1837            self.flash("Not all required fields filled.")
1838            self.redirect(self.url(self.context, 'edit_base'))
1839            return
[6770]1840        self.ac_series = self.request.form.get('ac_series', None)
1841        self.ac_number = self.request.form.get('ac_number', None)
1842
1843        if SUBMIT is None:
1844            return
1845        pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number)
1846        code = get_access_code(pin)
1847        if not code:
[6936]1848            self.flash('Activation code is invalid.')
[6770]1849            return
1850        # Mark pin as used (this also fires a pin related transition)
1851        # and fire transition start_clearance
1852        if code.state == USED:
[6936]1853            self.flash('Activation code has already been used.')
[6770]1854            return
1855        else:
1856            comment = u"AC invalidated for %s" % self.context.student_id
1857            # Here we know that the ac is in state initialized so we do not
[6927]1858            # expect an exception, but the owner might be different
1859            if not invalidate_accesscode(pin,comment,self.context.student_id):
1860                self.flash('You are not the owner of this access code.')
1861                return
[6770]1862            self.context.clr_code = pin
1863        IWorkflowInfo(self.context).fireTransition('start_clearance')
[6771]1864        self.flash('Clearance process has been started.')
[6770]1865        self.redirect(self.url(self.context,'cedit'))
1866        return
1867
[6695]1868class StudentClearanceEditActionButton(ManageActionButton):
1869    grok.order(1)
1870    grok.context(IStudent)
1871    grok.view(StudentClearanceDisplayFormPage)
1872    grok.require('waeup.handleStudent')
[6722]1873    text = 'Edit'
[6695]1874    target = 'cedit'
1875
[6717]1876    @property
1877    def target_url(self):
1878        if self.context.clearance_locked:
1879            return ''
1880        return self.view.url(self.view.context, self.target)
1881
[6695]1882class StudentClearanceEditFormPage(StudentClearanceManageFormPage):
1883    """ View to edit student clearance data by student
1884    """
1885    grok.context(IStudent)
1886    grok.name('cedit')
1887    grok.require('waeup.handleStudent')
[6756]1888    form_fields = grok.AutoFields(
1889        IStudentClearanceEdit).omit('clearance_locked')
[6722]1890    label = 'Edit clearance data'
[6695]1891    title = 'Clearance Data'
1892    form_fields['date_of_birth'].custom_widget = FriendlyDateWidget('le-year')
[6718]1893
1894    def update(self):
1895        if self.context.clearance_locked:
[7145]1896            emit_lock_message(self)
[6718]1897            return
1898        return super(StudentClearanceEditFormPage, self).update()
[6719]1899
[6722]1900    @grok.action('Save')
1901    def save(self, **data):
1902        self.applyData(self.context, **data)
[6771]1903        self.flash('Clearance form has been saved.')
[6722]1904        return
1905
[7253]1906    # To be specified in the customisation package
1907    def dataNotComplete(self):
1908        #store = getUtility(IExtFileStore)
1909        #if not store.getFileByContext(self.context, attr=u'xyz.jpg'):
1910        #    return 'No xyz scan uploaded.'
1911        return False
1912
[6722]1913    @grok.action('Save and request clearance')
[7186]1914    def requestClearance(self, **data):
[6722]1915        self.applyData(self.context, **data)
[7275]1916        #self.context._p_changed = True
[7253]1917        if self.dataNotComplete():
1918            self.flash(self.dataNotComplete())
1919            return
[6771]1920        self.flash('Clearance form has been saved.')
[6769]1921        self.redirect(self.url(self.context,'request_clearance'))
[6722]1922        return
1923
[7321]1924class RequestClearancePage(SIRPPage):
[6769]1925    grok.context(IStudent)
1926    grok.name('request_clearance')
1927    grok.require('waeup.handleStudent')
1928    grok.template('enterpin')
1929    title = 'Request clearance'
1930    label = 'Request clearance'
1931    notice = 'Enter the CLR access code used for starting clearance.'
1932    ac_prefix = 'CLR'
1933    pnav = 4
1934    buttonname = 'Request clearance now'
1935
1936    def update(self, SUBMIT=None):
1937        self.ac_series = self.request.form.get('ac_series', None)
1938        self.ac_number = self.request.form.get('ac_number', None)
1939        if SUBMIT is None:
1940            return
1941        pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number)
1942        if self.context.clr_code != pin:
1943            self.flash("This isn't your CLR access code.")
1944            return
1945        state = IWorkflowState(self.context).getState()
1946        # This shouldn't happen, but the application officer
1947        # might have forgotten to lock the form after changing the state
1948        if state != CLEARANCE:
1949            self.flash('This form cannot be submitted. Wrong state!')
1950            return
1951        IWorkflowInfo(self.context).fireTransition('request_clearance')
1952        self.flash('Clearance has been requested.')
1953        self.redirect(self.url(self.context))
[6789]1954        return
[6806]1955
[6944]1956class CourseRegistrationStartActionButton(ManageActionButton):
1957    grok.order(1)
1958    grok.context(IStudentStudyCourse)
1959    grok.view(StudyCourseDisplayFormPage)
1960    grok.require('waeup.handleStudent')
[7116]1961    icon = 'actionicon_start.gif'
[6944]1962    text = 'Start course registration'
1963    target = 'start_course_registration'
1964
1965    @property
1966    def target_url(self):
1967        if not self.context.getStudent().state in (CLEARED,RETURNING):
1968            return ''
1969        return self.view.url(self.view.context, self.target)
1970
[7321]1971class StartCourseRegistrationPage(SIRPPage):
[6944]1972    grok.context(IStudentStudyCourse)
1973    grok.name('start_course_registration')
1974    grok.require('waeup.handleStudent')
1975    grok.template('enterpin')
1976    title = 'Start course registration'
1977    label = 'Start course registration'
1978    ac_prefix = 'SFE'
1979    notice = ''
1980    pnav = 4
1981    buttonname = 'Start course registration now'
1982
1983    def update(self, SUBMIT=None):
1984        if not self.context.getStudent().state in (CLEARED,RETURNING):
1985            self.flash("Wrong state.")
1986            self.redirect(self.url(self.context))
1987            return
1988        self.ac_series = self.request.form.get('ac_series', None)
1989        self.ac_number = self.request.form.get('ac_number', None)
1990
1991        if SUBMIT is None:
1992            return
1993        pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number)
1994        code = get_access_code(pin)
1995        if not code:
1996            self.flash('Activation code is invalid.')
1997            return
1998        # Mark pin as used (this also fires a pin related transition)
1999        # and fire transition start_clearance
2000        if code.state == USED:
2001            self.flash('Activation code has already been used.')
2002            return
2003        else:
2004            comment = u"AC invalidated for %s" % self.context.getStudent().student_id
2005            # Here we know that the ac is in state initialized so we do not
2006            # expect an exception, but the owner might be different
2007            if not invalidate_accesscode(
2008                pin,comment,self.context.getStudent().student_id):
2009                self.flash('You are not the owner of this access code.')
2010                return
2011        if self.context.getStudent().state == CLEARED:
2012            IWorkflowInfo(self.context.getStudent()).fireTransition(
2013                'pay_first_school_fee')
2014        elif self.context.getStudent().state == RETURNING:
2015            IWorkflowInfo(self.context.getStudent()).fireTransition(
2016                'pay_school_fee')
2017        self.flash('Course registration has been started.')
2018        self.redirect(self.url(self.context))
2019        return
2020
2021
[6808]2022class AddStudyLevelActionButton(AddActionButton):
2023    grok.order(1)
2024    grok.context(IStudentStudyCourse)
2025    grok.view(StudyCourseDisplayFormPage)
2026    grok.require('waeup.handleStudent')
2027    text = 'Add course list'
2028    target = 'add'
2029
2030    @property
2031    def target_url(self):
2032        student = self.view.context.getStudent()
2033        condition1 = student.state != 'school fee paid'
2034        condition2 = str(student['studycourse'].current_level) in \
2035            self.view.context.keys()
2036        if condition1 or condition2:
2037            return ''
2038        return self.view.url(self.view.context, self.target)
2039
[7321]2040class AddStudyLevelFormPage(SIRPEditFormPage):
[6806]2041    """ Page for students to add current study levels
2042    """
2043    grok.context(IStudentStudyCourse)
2044    grok.name('add')
2045    grok.require('waeup.handleStudent')
2046    grok.template('studyleveladdpage')
2047    form_fields = grok.AutoFields(IStudentStudyCourse)
2048    title = 'Study Course'
2049    pnav = 4
2050
2051    @property
2052    def label(self):
2053        studylevelsource = StudyLevelSource().factory
2054        code = self.context.current_level
2055        title = studylevelsource.getTitle(self.context, code)
2056        return 'Add current level %s' % title
2057
2058    def update(self):
2059        if self.context.getStudent().state != 'school fee paid':
[7145]2060            emit_lock_message(self)
[6806]2061            return
2062        super(AddStudyLevelFormPage, self).update()
2063        return
2064
2065    @grok.action('Create course list now')
2066    def addStudyLevel(self, **data):
2067        studylevel = StudentStudyLevel()
2068        studylevel.level = self.context.current_level
2069        studylevel.level_session = self.context.current_session
2070        try:
2071            self.context.addStudentStudyLevel(
2072                self.context.certificate,studylevel)
2073        except KeyError:
2074            self.flash('This level exists.')
2075        self.redirect(self.url(self.context))
2076        return
[6808]2077
2078class StudyLevelEditActionButton(ManageActionButton):
2079    grok.order(1)
2080    grok.context(IStudentStudyLevel)
2081    grok.view(StudyLevelDisplayFormPage)
2082    grok.require('waeup.handleStudent')
2083    text = 'Add and remove courses'
2084    target = 'edit'
2085
2086    @property
2087    def target_url(self):
2088        student = self.view.context.getStudent()
2089        condition1 = student.state != 'school fee paid'
2090        condition2 = student[
2091            'studycourse'].current_level != self.view.context.level
2092        if condition1 or condition2:
2093            return ''
2094        return self.view.url(self.view.context, self.target)
2095
[7321]2096class StudyLevelEditFormPage(SIRPEditFormPage):
[6808]2097    """ Page to edit the student study level data by students
2098    """
2099    grok.context(IStudentStudyLevel)
2100    grok.name('edit')
2101    grok.require('waeup.handleStudent')
2102    grok.template('studyleveleditpage')
2103    form_fields = grok.AutoFields(IStudentStudyLevel).omit(
2104        'level_session', 'level_verdict')
2105    pnav = 4
2106
2107    def update(self):
2108        super(StudyLevelEditFormPage, self).update()
2109    #    tabs.need()
2110        datatable.need()
[7329]2111        warning.need()
[6808]2112        return
2113
2114    @property
2115    def title(self):
2116        return 'Study Level %s' % self.context.level_title
2117
2118    @property
2119    def label(self):
2120        return 'Add and remove course tickets of study level %s' % self.context.level_title
2121
2122    @property
2123    def total_credits(self):
2124        total_credits = 0
2125        for key, val in self.context.items():
2126            total_credits += val.credits
2127        return total_credits
2128
2129    @grok.action('Add course ticket')
2130    def addCourseTicket(self, **data):
2131        self.redirect(self.url(self.context, 'ctadd'))
2132
[7329]2133    @jsaction('Remove selected tickets')
[6808]2134    def delCourseTicket(self, **data):
2135        form = self.request.form
2136        if form.has_key('val_id'):
2137            child_id = form['val_id']
2138        else:
2139            self.flash('No ticket selected.')
2140            self.redirect(self.url(self.context, '@@edit'))
2141            return
2142        if not isinstance(child_id, list):
2143            child_id = [child_id]
2144        deleted = []
2145        for id in child_id:
[6940]2146            # Students are not allowed to remove core tickets
[6808]2147            if not self.context[id].core_or_elective:
2148                try:
2149                    del self.context[id]
2150                    deleted.append(id)
2151                except:
2152                    self.flash('Could not delete %s: %s: %s' % (
2153                            id, sys.exc_info()[0], sys.exc_info()[1]))
2154        if len(deleted):
2155            self.flash('Successfully removed: %s' % ', '.join(deleted))
2156        self.redirect(self.url(self.context, u'@@edit'))
2157        return
2158
[6810]2159    @grok.action('Register course list')
[7186]2160    def RegisterCourses(self, **data):
[6810]2161        IWorkflowInfo(self.context.getStudent()).fireTransition('register_courses')
2162        self.flash('Course list has been registered.')
2163        self.redirect(self.url(self.context))
2164        return
2165
[6808]2166class CourseTicketAddFormPage2(CourseTicketAddFormPage):
2167    """Add a course ticket by student.
2168    """
2169    grok.name('ctadd')
2170    grok.require('waeup.handleStudent')
2171    form_fields = grok.AutoFields(ICourseTicketAdd).omit(
2172        'grade', 'score', 'core_or_elective', 'automatic')
2173
2174    @grok.action('Add course ticket')
2175    def addCourseTicket(self, **data):
2176        ticket = CourseTicket()
2177        course = data['course']
2178        ticket.automatic = False
2179        ticket.code = course.code
2180        ticket.title = course.title
2181        ticket.faculty = course.__parent__.__parent__.__parent__.title
2182        ticket.department = course.__parent__.__parent__.title
2183        ticket.credits = course.credits
2184        ticket.passmark = course.passmark
2185        ticket.semester = course.semester
2186        try:
2187            self.context.addCourseTicket(ticket)
2188        except KeyError:
2189            self.flash('The ticket exists.')
2190            return
2191        self.flash('Successfully added %s.' % ticket.code)
2192        self.redirect(self.url(self.context, u'@@edit'))
2193        return
[7369]2194
2195class ChangePasswordRequestPage(SIRPForm):
2196    """Captcha'd page for students to request a password change.
2197    """
2198    grok.context(IUniversity)
2199    grok.name('changepw')
2200    grok.require('waeup.Anonymous')
2201    grok.template('changepw')
2202    title = 'Change Password Request'
2203    label = 'Change my password'
2204    form_fields = grok.AutoFields(IStudentChangePassword)
2205
2206    def update(self):
2207        # Handle captcha
2208        self.captcha = getUtility(ICaptchaManager).getCaptcha()
2209        self.captcha_result = self.captcha.verify(self.request)
2210        self.captcha_code = self.captcha.display(self.captcha_result.error_code)
2211        return
2212
[7414]2213    @grok.action('Get login credentials')
[7369]2214    def request(self, **data):
2215        if not self.captcha_result.is_valid:
2216            # Captcha will display error messages automatically.
2217            # No need to flash something.
2218            return
2219        # Search student
2220        cat = queryUtility(ICatalog, name='students_catalog')
2221        reg_number = data['reg_number']
2222        email = data['email']
2223        results = cat.searchResults(
2224            reg_number=(reg_number, reg_number),
2225            email=(email,email))
2226        if len(results) == 0:
2227            self.flash('No student record found.')
2228            return
2229        student = list(results)[0]
2230        # Change password
2231        sirp_utils = getUtility(ISIRPUtils)
2232        pwd = sirp_utils.genPassword()
2233        IUserAccount(student).setPassword(pwd)
2234        # Send email with new redentials
2235        msg = 'You have successfully changed your password for the'
2236        login_url = self.url(grok.getSite(), 'login')
[7407]2237        success = sirp_utils.sendCredentials(
2238            IUserAccount(student),pwd,login_url,msg)
[7369]2239        if success:
2240            self.flash('An email with your user name and password ' +
[7407]2241                'has been sent to %s.' % email)
[7369]2242        else:
2243            self.flash('An smtp server error occurred.')
2244        return
Note: See TracBrowser for help on using the repository browser.