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

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

Do not store faculty and department titles in each course ticket.

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