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

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

Implement releaseBed method and releaseBeds action (test will follow).

  • Property svn:keywords set to Id
File size: 64.3 KB
RevLine 
[6621]1## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
2## This program is free software; you can redistribute it and/or modify
3## it under the terms of the GNU General Public License as published by
4## the Free Software Foundation; either version 2 of the License, or
5## (at your option) any later version.
6##
7## This program is distributed in the hope that it will be useful,
8## but WITHOUT ANY WARRANTY; without even the implied warranty of
9## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10## GNU General Public License for more details.
11##
12## You should have received a copy of the GNU General Public License
13## along with this program; if not, write to the Free Software
14## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15##
16"""UI components for students and related components.
17"""
[7006]18import sys
[6621]19import grok
[6869]20from time import time
21from datetime import date, datetime
[7015]22from zope.event import notify
[6996]23from zope.catalog.interfaces import ICatalog
24from zope.component import queryUtility
[6621]25from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState
[6760]26from zope.component import createObject
[6936]27from waeup.sirp.accesscodes import (
[6937]28    invalidate_accesscode, get_access_code, create_accesscode)
[6621]29from waeup.sirp.accesscodes.workflow import USED
30from waeup.sirp.browser import (
31    WAeUPPage, WAeUPEditFormPage, WAeUPAddFormPage, WAeUPDisplayFormPage)
32from waeup.sirp.browser.breadcrumbs import Breadcrumb
[6775]33from waeup.sirp.browser.resources import datepicker, datatable, tabs
[6621]34from waeup.sirp.browser.viewlets import (
[6760]35    ManageActionButton, PrimaryNavTab, AddActionButton)
36from waeup.sirp.interfaces import IWAeUPObject, IUserAccount
[6621]37from waeup.sirp.widgets.datewidget import (
[6869]38    FriendlyDateWidget, FriendlyDateDisplayWidget,
39    FriendlyDatetimeDisplayWidget)
[6912]40from waeup.sirp.university.vocabularies import study_modes
[6621]41from waeup.sirp.students.interfaces import (
[6756]42    IStudentsContainer, IStudent, IStudentClearance, IStudentPasswordSetting,
[6859]43    IStudentPersonal, IStudentBase, IStudentStudyCourse,
[6774]44    IStudentAccommodation, IStudentClearanceEdit, IStudentStudyLevel,
[6877]45    ICourseTicket, ICourseTicketAdd, IStudentPaymentsContainer,
[6992]46    IStudentOnlinePayment, IBedTicket
[6621]47    )
[6626]48from waeup.sirp.students.catalog import search
[6992]49from waeup.sirp.students.workflow import (
50    CLEARANCE, RETURNING, CLEARED, REGISTERED, VALIDATED)
[6795]51from waeup.sirp.students.studylevel import StudentStudyLevel, CourseTicket
[6775]52from waeup.sirp.students.vocabularies import StudyLevelSource
[6996]53from waeup.sirp.students.utils import (
[7019]54    getPaymentDetails, getAccommodationDetails, selectBed, renderPDF)
[6820]55from waeup.sirp.browser.resources import toggleall
[6940]56from waeup.sirp.authentication import get_principal_role_manager
[6997]57from waeup.sirp.hostels.hostel import NOT_OCCUPIED
[6621]58
[6943]59def write_log_message(view, message):
60    ob_class = view.__implemented__.__name__.replace('waeup.sirp.','')
61    view.context.getStudent().loggerInfo(ob_class, message)
62    return
63
[6762]64# Save function used for save methods in manager pages
65def msave(view, **data):
66    form = view.request.form
67    changed_fields = view.applyData(view.context, **data)
[6771]68    # Turn list of lists into single list
69    if changed_fields:
70        changed_fields = reduce(lambda x,y: x+y, changed_fields.values())
71    fields_string = ' + '.join(changed_fields)
[6762]72    view.context._p_changed = True
73    view.flash('Form has been saved.')
74    if fields_string:
[6943]75        write_log_message(view, 'saved: % s' % fields_string)
[6762]76    return
77
[6621]78class StudentsTab(PrimaryNavTab):
79    """Students tab in primary navigation.
80    """
81
82    grok.context(IWAeUPObject)
[6953]83    grok.order(4)
[6660]84    grok.require('waeup.viewStudent')
[6621]85    grok.template('primarynavtab')
86
87    pnav = 4
88    tab_title = u'Students'
89
90    @property
91    def link_target(self):
92        return self.view.application_url('students')
93
[6629]94class StudentsBreadcrumb(Breadcrumb):
95    """A breadcrumb for the students container.
96    """
97    grok.context(IStudentsContainer)
98    title = u'Students'
99
[6818]100class StudentBreadcrumb(Breadcrumb):
101    """A breadcrumb for the student container.
102    """
103    grok.context(IStudent)
104
105    def title(self):
106        return self.context.fullname
107
[6635]108class SudyCourseBreadcrumb(Breadcrumb):
109    """A breadcrumb for the student study course.
110    """
111    grok.context(IStudentStudyCourse)
112    title = u'Study Course'
113
114class PaymentsBreadcrumb(Breadcrumb):
115    """A breadcrumb for the student payments folder.
116    """
[6859]117    grok.context(IStudentPaymentsContainer)
[6635]118    title = u'Payments'
119
[6870]120class OnlinePaymentBreadcrumb(Breadcrumb):
121    """A breadcrumb for course lists.
122    """
[6877]123    grok.context(IStudentOnlinePayment)
[6870]124
125    @property
126    def title(self):
127        return self.context.p_id
128
[6635]129class AccommodationBreadcrumb(Breadcrumb):
130    """A breadcrumb for the student accommodation folder.
131    """
132    grok.context(IStudentAccommodation)
133    title = u'Accommodation'
134
[7009]135    #@property
136    #def target(self):
137    #    prm = get_principal_role_manager()
138    #    principal = get_current_principal()
139    #    roles = [x[0] for x in prm.getRolesForPrincipal(principal.id)]
140    #    if 'waeup.Student' in roles:
141    #        return 'index'
142    #    else:
143    #        return 'manage'
144
[6994]145class BedTicketBreadcrumb(Breadcrumb):
146    """A breadcrumb for bed tickets.
147    """
148    grok.context(IBedTicket)
[7009]149
[6994]150    @property
151    def title(self):
152        return 'Bed Ticket %s' % self.context.getSessionString()
153
[6776]154class StudyLevelBreadcrumb(Breadcrumb):
155    """A breadcrumb for course lists.
156    """
157    grok.context(IStudentStudyLevel)
158
159    @property
160    def title(self):
161        return self.context.level_title
162
[6626]163class StudentsContainerPage(WAeUPPage):
164    """The standard view for student containers.
[6621]165    """
166    grok.context(IStudentsContainer)
167    grok.name('index')
[6660]168    grok.require('waeup.viewStudent')
[6695]169    grok.template('containerpage')
[6654]170    label = 'Student Section'
171    title = 'Students'
[6642]172    pnav = 4
[6621]173
[6626]174    def update(self, *args, **kw):
175        datatable.need()
176        form = self.request.form
177        self.hitlist = []
178        if 'searchterm' in form and form['searchterm']:
179            self.searchterm = form['searchterm']
180            self.searchtype = form['searchtype']
181        elif 'old_searchterm' in form:
182            self.searchterm = form['old_searchterm']
183            self.searchtype = form['old_searchtype']
184        else:
185            if 'search' in form:
186                self.flash('Empty search string.')
187            return
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
[6773]194class SetPasswordPage(WAeUPPage):
[6699]195    grok.context(IWAeUPObject)
196    grok.name('setpassword')
197    grok.require('waeup.Public')
[6774]198    grok.template('setpassword')
[6699]199    title = ''
200    label = 'Set password for first-time login'
[6758]201    ac_prefix = 'PWD'
[6715]202    pnav = 0
[6699]203
204    def update(self, SUBMIT=None):
[6758]205        self.reg_number = self.request.form.get('reg_number', None)
206        self.ac_series = self.request.form.get('ac_series', None)
207        self.ac_number = self.request.form.get('ac_number', None)
[6756]208
[6699]209        if SUBMIT is None:
210            return
211        hitlist = search(query=self.reg_number,
212            searchtype='reg_number', view=self)
213        if not hitlist:
214            self.flash('No student found.')
215            return
216        if len(hitlist) != 1:   # Cannot happen but anyway
217            self.flash('More than one student found.')
218            return
[6704]219        student = hitlist[0].context
220        self.student_id = student.student_id
221        student_pw = student.password
[6758]222        pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number)
[6699]223        code = get_access_code(pin)
224        if not code:
225            self.flash('Access code is invalid.')
226            return
[6704]227        if student_pw and pin == student.adm_code:
228            self.flash('Password has already been set. Your Student Id is %s'
229                % self.student_id)
230            return
231        elif student_pw:
[6800]232            self.flash('Password has already been set. You are using the wrong Access Code.')
[6704]233            return
[6699]234        # Mark pin as used (this also fires a pin related transition)
235        # and set student password
236        if code.state == USED:
237            self.flash('Access code has already been used.')
238            return
239        else:
[6704]240            comment = u"AC invalidated for %s" % self.student_id
[6699]241            # Here we know that the ac is in state initialized so we do not
242            # expect an exception
243            #import pdb; pdb.set_trace()
244            invalidate_accesscode(pin,comment)
[6758]245            IUserAccount(student).setPassword(self.ac_number)
[6769]246            student.adm_code = pin
[6704]247        self.flash('Password has been set. Your Student Id is %s'
248            % self.student_id)
[6699]249        return
250
[6626]251class StudentsContainerManageActionButton(ManageActionButton):
[6622]252    grok.order(1)
253    grok.context(IStudentsContainer)
254    grok.view(StudentsContainerPage)
255    grok.require('waeup.manageStudents')
[6647]256    text = 'Manage student section'
[6622]257
[6626]258
259class StudentsContainerManagePage(WAeUPPage):
260    """The manage page for student containers.
[6622]261    """
262    grok.context(IStudentsContainer)
263    grok.name('manage')
264    grok.require('waeup.manageStudents')
[6695]265    grok.template('containermanagepage')
[6642]266    pnav = 4
[6647]267    title = 'Manage student section'
[6622]268
269    @property
270    def label(self):
271        return self.title
272
[6626]273    def update(self, *args, **kw):
274        datatable.need()
[6820]275        toggleall.need()
[6626]276        form = self.request.form
277        self.hitlist = []
278        if 'searchterm' in form and form['searchterm']:
279            self.searchterm = form['searchterm']
280            self.searchtype = form['searchtype']
281        elif 'old_searchterm' in form:
282            self.searchterm = form['old_searchterm']
283            self.searchtype = form['old_searchtype']
284        else:
285            if 'search' in form:
286                self.flash('Empty search string.')
287            return
288        if not 'entries' in form:
289            self.hitlist = search(query=self.searchterm,
290                searchtype=self.searchtype, view=self)
291            if not self.hitlist:
292                self.flash('No student found.')
293            return
294        entries = form['entries']
295        if isinstance(entries, basestring):
296            entries = [entries]
297        deleted = []
298        for entry in entries:
299            if 'remove' in form:
300                del self.context[entry]
301                deleted.append(entry)
302        self.hitlist = search(query=self.searchterm,
303            searchtype=self.searchtype, view=self)
304        if len(deleted):
305            self.flash('Successfully removed: %s' % ', '.join(deleted))
[6622]306        return
307
[6626]308class StudentsContainerAddActionButton(AddActionButton):
309    grok.order(1)
310    grok.context(IStudentsContainer)
311    grok.view(StudentsContainerManagePage)
312    grok.require('waeup.manageStudents')
313    text = 'Add student'
314    target = 'addstudent'
315
[6622]316class StudentAddFormPage(WAeUPAddFormPage):
317    """Add-form to add a student.
318    """
319    grok.context(IStudentsContainer)
320    grok.require('waeup.manageStudents')
321    grok.name('addstudent')
322    grok.template('studentaddpage')
323    form_fields = grok.AutoFields(IStudent)
324    title = 'Students'
325    label = 'Add student'
[6642]326    pnav = 4
[6622]327
328    @grok.action('Create student record')
329    def addStudent(self, **data):
330        student = createObject(u'waeup.Student')
331        self.applyData(student, **data)
[6652]332        self.context.addStudent(student)
[6626]333        self.flash('Student record created.')
[6651]334        self.redirect(self.url(self.context[student.student_id], 'index'))
[6622]335        return
336
[6631]337class StudentBaseDisplayFormPage(WAeUPDisplayFormPage):
338    """ Page to display student base data
339    """
[6622]340    grok.context(IStudent)
341    grok.name('index')
[6660]342    grok.require('waeup.viewStudent')
[6695]343    grok.template('basepage')
[6756]344    form_fields = grok.AutoFields(IStudentBase).omit('password')
[6642]345    pnav = 4
346    title = 'Base Data'
[6622]347
348    @property
349    def label(self):
[6818]350        return '%s: Base Data' % self.context.fullname
[6631]351
[6699]352    @property
353    def hasPassword(self):
354        if self.context.password:
355            return 'set'
356        return 'unset'
357
[6631]358class StudentBaseManageActionButton(ManageActionButton):
359    grok.order(1)
360    grok.context(IStudent)
361    grok.view(StudentBaseDisplayFormPage)
362    grok.require('waeup.manageStudents')
[6695]363    text = 'Manage'
[6631]364    target = 'edit_base'
365
366class StudentBaseManageFormPage(WAeUPEditFormPage):
367    """ View to edit student base data
368    """
369    grok.context(IStudent)
370    grok.name('edit_base')
371    grok.require('waeup.manageStudents')
372    form_fields = grok.AutoFields(IStudentBase).omit('student_id')
[6695]373    grok.template('basemanagepage')
374    label = 'Manage base data'
[6642]375    title = 'Base Data'
376    pnav = 4
[6631]377
[6638]378    def update(self):
379        datepicker.need() # Enable jQuery datepicker in date fields.
380        super(StudentBaseManageFormPage, self).update()
381        self.wf_info = IWorkflowInfo(self.context)
382        return
383
384    def getTransitions(self):
385        """Return a list of dicts of allowed transition ids and titles.
386
387        Each list entry provides keys ``name`` and ``title`` for
388        internal name and (human readable) title of a single
389        transition.
390        """
391        allowed_transitions = self.wf_info.getManualTransitions()
392        return [dict(name='', title='No transition')] +[
393            dict(name=x, title=y) for x, y in allowed_transitions]
394
395    @grok.action('Save')
396    def save(self, **data):
[6701]397        form = self.request.form
[6790]398        password = form.get('password', None)
399        password_ctl = form.get('control_password', None)
400        if password:
401            if (password != password_ctl):
[6701]402                self.flash('Passwords do not match.')
[6790]403            else:
404                # XXX: This is too early. PW should only be saved if there
405                #      are no (other) errors left in form.
406                IUserAccount(self.context).setPassword(password)
[6943]407                write_log_message(self, 'password changed')
[6790]408
[6789]409        #self.reg_number = form.get('form.reg_number', None)
410        #if self.reg_number:
411        #    hitlist = search(query=self.reg_number,searchtype='reg_number', view=self)
412        #    if hitlist and hitlist[0].student_id != self.context.student_id:
413        #        self.flash('Registration number exists.')
414        #        return
415        #self.matric_number = form.get('form.matric_number', None)
416        #if self.matric_number:
417        #    hitlist = search(query=self.matric_number,
418        #        searchtype='matric_number', view=self)
419        #    if hitlist and hitlist[0].student_id != self.context.student_id:
420        #        self.flash('Matriculation number exists.')
421        #        return
422
[6771]423        # Turn list of lists into single list
[6638]424        changed_fields = self.applyData(self.context, **data)
[6771]425        if changed_fields:
426            changed_fields = reduce(lambda x,y: x+y, changed_fields.values())
427        fields_string = ' + '.join(changed_fields)
[6638]428        self.context._p_changed = True
429        if form.has_key('transition') and form['transition']:
430            transition_id = form['transition']
431            self.wf_info.fireTransition(transition_id)
432        self.flash('Form has been saved.')
[6644]433        if fields_string:
[6943]434            write_log_message(self, 'saved: % s' % fields_string)
[6638]435        return
436
[6631]437class StudentClearanceDisplayFormPage(WAeUPDisplayFormPage):
438    """ Page to display student clearance data
439    """
440    grok.context(IStudent)
441    grok.name('view_clearance')
[6660]442    grok.require('waeup.viewStudent')
[6695]443    form_fields = grok.AutoFields(IStudentClearance).omit('clearance_locked')
[6650]444    form_fields['date_of_birth'].custom_widget = FriendlyDateDisplayWidget('le')
[6642]445    title = 'Clearance Data'
446    pnav = 4
[6631]447
448    @property
449    def label(self):
[6818]450        return '%s: Clearance Data' % self.context.fullname
[6631]451
452class StudentClearanceManageActionButton(ManageActionButton):
453    grok.order(1)
454    grok.context(IStudent)
455    grok.view(StudentClearanceDisplayFormPage)
456    grok.require('waeup.manageStudents')
[6695]457    text = 'Manage'
[6631]458    target = 'edit_clearance'
459
460class StudentClearanceManageFormPage(WAeUPEditFormPage):
461    """ Page to edit student clearance data
462    """
463    grok.context(IStudent)
464    grok.name('edit_clearance')
[6649]465    grok.require('waeup.manageStudents')
[6631]466    form_fields = grok.AutoFields(IStudentClearance)
[6695]467    label = 'Manage clearance data'
[6642]468    title = 'Clearance Data'
469    pnav = 4
[6631]470
[6650]471    form_fields['date_of_birth'].custom_widget = FriendlyDateWidget('le-year')
472
473    def update(self):
474        datepicker.need() # Enable jQuery datepicker in date fields.
475        return super(StudentClearanceManageFormPage, self).update()
476
[6695]477    @grok.action('Save')
478    def save(self, **data):
[6762]479        msave(self, **data)
[6695]480        return
481
[6631]482class StudentPersonalDisplayFormPage(WAeUPDisplayFormPage):
483    """ Page to display student personal data
484    """
485    grok.context(IStudent)
486    grok.name('view_personal')
[6660]487    grok.require('waeup.viewStudent')
[6631]488    form_fields = grok.AutoFields(IStudentPersonal)
[6642]489    title = 'Personal Data'
490    pnav = 4
[6631]491
492    @property
493    def label(self):
[6818]494        return '%s: Personal Data' % self.context.fullname
[6631]495
496class StudentPersonalManageActionButton(ManageActionButton):
497    grok.order(1)
498    grok.context(IStudent)
499    grok.view(StudentPersonalDisplayFormPage)
500    grok.require('waeup.manageStudents')
[6695]501    text = 'Manage'
[6631]502    target = 'edit_personal'
503
504class StudentPersonalManageFormPage(WAeUPEditFormPage):
505    """ Page to edit student clearance data
506    """
507    grok.context(IStudent)
508    grok.name('edit_personal')
[6660]509    grok.require('waeup.viewStudent')
[6631]510    form_fields = grok.AutoFields(IStudentPersonal)
[6695]511    label = 'Manage personal data'
[6642]512    title = 'Personal Data'
513    pnav = 4
[6631]514
[6762]515    @grok.action('Save')
516    def save(self, **data):
517        msave(self, **data)
518        return
519
[6635]520class StudyCourseDisplayFormPage(WAeUPDisplayFormPage):
521    """ Page to display the student study course data
522    """
523    grok.context(IStudentStudyCourse)
524    grok.name('index')
[6660]525    grok.require('waeup.viewStudent')
[6635]526    form_fields = grok.AutoFields(IStudentStudyCourse)
[6775]527    grok.template('studycoursepage')
[6642]528    title = 'Study Course'
529    pnav = 4
[6635]530
531    @property
532    def label(self):
[6818]533        return '%s: Study Course' % self.context.__parent__.fullname
[6635]534
[6912]535    @property
536    def current_mode(self):
537        return study_modes.getTermByToken(
538            self.context.certificate.study_mode).title
539
[6649]540class StudyCourseManageActionButton(ManageActionButton):
541    grok.order(1)
542    grok.context(IStudentStudyCourse)
543    grok.view(StudyCourseDisplayFormPage)
544    grok.require('waeup.manageStudents')
[6695]545    text = 'Manage'
[6775]546    target = 'manage'
[6649]547
548class StudyCourseManageFormPage(WAeUPEditFormPage):
549    """ Page to edit the student study course data
550    """
551    grok.context(IStudentStudyCourse)
[6775]552    grok.name('manage')
[6649]553    grok.require('waeup.manageStudents')
[6775]554    grok.template('studycoursemanagepage')
[6649]555    form_fields = grok.AutoFields(IStudentStudyCourse)
556    title = 'Study Course'
[6695]557    label = 'Manage study course'
[6649]558    pnav = 4
[6775]559    taboneactions = ['Save','Cancel']
560    tabtwoactions = ['Remove selected levels','Cancel']
561    tabthreeactions = ['Add study level']
[6649]562
[6775]563    def update(self):
564        super(StudyCourseManageFormPage, self).update()
565        tabs.need()
566        datatable.need()
567        return
568
[6761]569    @grok.action('Save')
570    def save(self, **data):
[6762]571        msave(self, **data)
[6761]572        return
573
[6775]574    @property
575    def level_dict(self):
576        studylevelsource = StudyLevelSource().factory
577        for code in studylevelsource.getValues(self.context):
578            title = studylevelsource.getTitle(self.context, code)
579            yield(dict(code=code, title=title))
580
581    @grok.action('Add study level')
[6774]582    def addStudyLevel(self, **data):
[6775]583        level_code = self.request.form.get('addlevel', None)
[6774]584        studylevel = StudentStudyLevel()
[6775]585        studylevel.level = int(level_code)
586        try:
[6782]587            self.context.addStudentStudyLevel(
588                self.context.certificate,studylevel)
[6775]589        except KeyError:
590            self.flash('This level exists.')
591        self.redirect(self.url(self.context, u'@@manage')+'#tab-2')
[6774]592        return
593
[6775]594    @grok.action('Remove selected levels')
595    def delStudyLevels(self, **data):
596        form = self.request.form
597        if form.has_key('val_id'):
598            child_id = form['val_id']
599        else:
600            self.flash('No study level selected.')
601            self.redirect(self.url(self.context, '@@manage')+'#tab-2')
602            return
603        if not isinstance(child_id, list):
604            child_id = [child_id]
605        deleted = []
606        for id in child_id:
607            try:
608                del self.context[id]
609                deleted.append(id)
610            except:
611                self.flash('Could not delete %s: %s: %s' % (
612                        id, sys.exc_info()[0], sys.exc_info()[1]))
613        if len(deleted):
614            self.flash('Successfully removed: %s' % ', '.join(deleted))
615        self.redirect(self.url(self.context, u'@@manage')+'#tab-2')
616        return
[6774]617
618class StudyLevelDisplayFormPage(WAeUPDisplayFormPage):
619    """ Page to display student study levels
620    """
621    grok.context(IStudentStudyLevel)
622    grok.name('index')
623    grok.require('waeup.viewStudent')
[6775]624    form_fields = grok.AutoFields(IStudentStudyLevel)
[6783]625    grok.template('studylevelpage')
[6774]626    pnav = 4
627
628    @property
[6792]629    def title(self):
630        return 'Study Level %s' % self.context.level_title
631
632    @property
[6774]633    def label(self):
[6776]634        return '%s: Study Level %s' % (
[6818]635            self.context.getStudent().fullname,self.context.level_title)
[6774]636
[6803]637    @property
638    def total_credits(self):
639        total_credits = 0
640        for key, val in self.context.items():
641            total_credits += val.credits
642        return total_credits
643
[7028]644class CourseRegistrationSlipActionButton(ManageActionButton):
[6792]645    grok.order(1)
646    grok.context(IStudentStudyLevel)
[7029]647    grok.view(StudyLevelDisplayFormPage)
[7028]648    grok.require('waeup.viewStudent')
649    icon = 'actionicon_pdf.png'
650    text = 'Download course registration slip'
651    target = 'course_registration.pdf'
652
653class ExportPDFCourseRegistrationSlipPage(grok.View):
654    """Deliver a PDF slip of the context.
655    """
656    grok.context(IStudentStudyLevel)
657    grok.name('course_registration.pdf')
658    grok.require('waeup.viewStudent')
659    form_fields = grok.AutoFields(IStudentStudyLevel)
660    prefix = 'form'
661
662    @property
663    def label(self):
664        return 'Course Registration Slip %s' % self.context.level_title
665
666    def render(self):
667        studentview = StudentBaseDisplayFormPage(self.context.getStudent(),
668            self.request)
669        return renderPDF(self,'Course Registration', 'course_registration.pdf',
670            self.context.getStudent, studentview)
671
672class StudyLevelManageActionButton(ManageActionButton):
673    grok.order(2)
674    grok.context(IStudentStudyLevel)
[6792]675    grok.view(StudyLevelDisplayFormPage)
676    grok.require('waeup.manageStudents')
677    text = 'Manage'
678    target = 'manage'
679
680class StudyLevelManageFormPage(WAeUPEditFormPage):
681    """ Page to edit the student study level data
682    """
683    grok.context(IStudentStudyLevel)
684    grok.name('manage')
685    grok.require('waeup.manageStudents')
686    grok.template('studylevelmanagepage')
687    form_fields = grok.AutoFields(IStudentStudyLevel)
688    pnav = 4
689    taboneactions = ['Save','Cancel']
[6795]690    tabtwoactions = ['Add course ticket','Remove selected tickets','Cancel']
[6792]691
692    def update(self):
693        super(StudyLevelManageFormPage, self).update()
694        tabs.need()
695        datatable.need()
696        return
697
698    @property
699    def title(self):
700        return 'Study Level %s' % self.context.level_title
701
702    @property
703    def label(self):
704        return 'Manage study level %s' % self.context.level_title
705
706    @grok.action('Save')
707    def save(self, **data):
708        msave(self, **data)
709        return
710
711    @grok.action('Add course ticket')
[6795]712    def addCourseTicket(self, **data):
713        self.redirect(self.url(self.context, '@@add'))
[6792]714
715    @grok.action('Remove selected tickets')
716    def delCourseTicket(self, **data):
717        form = self.request.form
718        if form.has_key('val_id'):
719            child_id = form['val_id']
720        else:
721            self.flash('No ticket selected.')
722            self.redirect(self.url(self.context, '@@manage')+'#tab-2')
723            return
724        if not isinstance(child_id, list):
725            child_id = [child_id]
726        deleted = []
727        for id in child_id:
728            try:
729                del self.context[id]
730                deleted.append(id)
731            except:
732                self.flash('Could not delete %s: %s: %s' % (
733                        id, sys.exc_info()[0], sys.exc_info()[1]))
734        if len(deleted):
735            self.flash('Successfully removed: %s' % ', '.join(deleted))
736        self.redirect(self.url(self.context, u'@@manage')+'#tab-2')
737        return
738
[6795]739class CourseTicketAddFormPage(WAeUPAddFormPage):
[6808]740    """Add a course ticket.
[6795]741    """
742    grok.context(IStudentStudyLevel)
743    grok.name('add')
744    grok.require('waeup.manageStudents')
745    label = 'Add course ticket'
[6808]746    form_fields = grok.AutoFields(ICourseTicketAdd).omit(
747        'grade', 'score', 'automatic')
[6795]748    pnav = 4
749
750    @property
751    def title(self):
752        return 'Study Level %s' % self.context.level_title
753
754    @grok.action('Add course ticket')
755    def addCourseTicket(self, **data):
756        ticket = CourseTicket()
757        course = data['course']
758        ticket.core_or_elective = data['core_or_elective']
[6802]759        ticket.automatic = False
[6795]760        ticket.code = course.code
761        ticket.title = course.title
762        ticket.faculty = course.__parent__.__parent__.__parent__.title
763        ticket.department = course.__parent__.__parent__.title
764        ticket.credits = course.credits
765        ticket.passmark = course.passmark
766        ticket.semester = course.semester
767        try:
768            self.context.addCourseTicket(ticket)
769        except KeyError:
770            self.flash('The ticket exists.')
771            return
[6799]772        self.flash('Successfully added %s.' % ticket.code)
[6795]773        self.redirect(self.url(self.context, u'@@manage')+'#tab-2')
774        return
775
776    @grok.action('Cancel')
777    def cancel(self, **data):
778        self.redirect(self.url(self.context))
779
[6796]780class CourseTicketDisplayFormPage(WAeUPDisplayFormPage):
781    """ Page to display course tickets
782    """
783    grok.context(ICourseTicket)
784    grok.name('index')
785    grok.require('waeup.viewStudent')
786    form_fields = grok.AutoFields(ICourseTicket)
787    grok.template('courseticketpage')
788    pnav = 4
789
790    @property
791    def title(self):
792        return 'Course Ticket %s' % self.context.code
793
794    @property
795    def label(self):
796        return '%s: Course Ticket %s' % (
[6818]797            self.context.getStudent().fullname,self.context.code)
[6796]798
799class CourseTicketManageActionButton(ManageActionButton):
800    grok.order(1)
801    grok.context(ICourseTicket)
802    grok.view(CourseTicketDisplayFormPage)
803    grok.require('waeup.manageStudents')
804    text = 'Manage'
805    target = 'manage'
806
807class CourseTicketManageFormPage(WAeUPEditFormPage):
808    """ Page to manage course tickets
809    """
810    grok.context(ICourseTicket)
811    grok.name('manage')
812    grok.require('waeup.manageStudents')
813    form_fields = grok.AutoFields(ICourseTicket)
814    grok.template('courseticketmanagepage')
815    pnav = 4
816
817    @property
818    def title(self):
819        return 'Course Ticket %s' % self.context.code
820
821    @property
822    def label(self):
823        return 'Manage course ticket %s' % self.context.code
824
825    @grok.action('Save')
826    def save(self, **data):
827        msave(self, **data)
828        return
829
[6940]830# We don't need the display form page yet
[6943]831#class PaymentsDisplayFormPage(WAeUPDisplayFormPage):
832#    """ Page to display the student payments
833#    """
834#    grok.context(IStudentPaymentsContainer)
835#    grok.name('view')
836#    grok.require('waeup.viewStudent')
837#    form_fields = grok.AutoFields(IStudentPaymentsContainer)
838#    grok.template('paymentspage')
839#    title = 'Payments'
840#    pnav = 4
[6635]841
[6943]842#    def formatDatetime(self,datetimeobj):
843#        if isinstance(datetimeobj, datetime):
844#            return datetimeobj.strftime("%Y-%m-%d %H:%M:%S")
845#        else:
846#            return None
[6869]847
[6943]848#    @property
849#    def label(self):
850#        return '%s: Payments' % self.context.__parent__.fullname
[6635]851
[6943]852#    def update(self):
853#        super(PaymentsDisplayFormPage, self).update()
854#        datatable.need()
855#        return
[6869]856
[6940]857# This manage form page is for both students and students officers.
[6869]858class PaymentsManageFormPage(WAeUPEditFormPage):
859    """ Page to manage the student payments
860    """
861    grok.context(IStudentPaymentsContainer)
[6940]862    grok.name('index')
863    grok.require('waeup.handleStudent')
[6869]864    form_fields = grok.AutoFields(IStudentPaymentsContainer)
865    grok.template('paymentsmanagepage')
866    title = 'Payments'
867    pnav = 4
868
[6940]869    def unremovable(self, ticket):
870        prm = get_principal_role_manager()
871        roles = [x[0] for x in prm.getRolesForPrincipal(self.request.principal.id)]
872        return ('waeup.Student' in roles and ticket.r_code)
873
[6869]874    def formatDatetime(self,datetimeobj):
875        if isinstance(datetimeobj, datetime):
876            return datetimeobj.strftime("%Y-%m-%d %H:%M:%S")
877        else:
878            return None
879
880    @property
881    def label(self):
882        return '%s: Payments' % self.context.__parent__.fullname
883
884    def update(self):
885        super(PaymentsManageFormPage, self).update()
886        datatable.need()
887        return
888
889    @grok.action('Remove selected tickets')
890    def delPaymentTicket(self, **data):
891        form = self.request.form
892        if form.has_key('val_id'):
893            child_id = form['val_id']
894        else:
895            self.flash('No payment selected.')
[6940]896            self.redirect(self.url(self.context))
[6869]897            return
898        if not isinstance(child_id, list):
899            child_id = [child_id]
900        deleted = []
901        for id in child_id:
[6992]902            # Students are not allowed to remove used payment tickets
[6940]903            if not self.unremovable(self.context[id]):
904                try:
905                    del self.context[id]
906                    deleted.append(id)
907                except:
908                    self.flash('Could not delete %s: %s: %s' % (
909                            id, sys.exc_info()[0], sys.exc_info()[1]))
[6869]910        if len(deleted):
911            self.flash('Successfully removed: %s' % ', '.join(deleted))
[6943]912            write_log_message(self,'removed: % s' % ', '.join(deleted))
[6940]913        self.redirect(self.url(self.context))
[6869]914        return
915
916    @grok.action('Add online payment ticket')
917    def addPaymentTicket(self, **data):
918        self.redirect(self.url(self.context, '@@addop'))
919
[6940]920#class OnlinePaymentManageActionButton(ManageActionButton):
921#    grok.order(1)
922#    grok.context(IStudentPaymentsContainer)
923#    grok.view(PaymentsDisplayFormPage)
924#    grok.require('waeup.manageStudents')
925#    text = 'Manage payments'
926#    target = 'manage'
[6869]927
928class OnlinePaymentAddFormPage(WAeUPAddFormPage):
929    """ Page to add an online payment ticket
930    """
931    grok.context(IStudentPaymentsContainer)
932    grok.name('addop')
933    grok.require('waeup.handleStudent')
[6877]934    form_fields = grok.AutoFields(IStudentOnlinePayment).select(
[6869]935        'p_category')
[6906]936    #zzgrok.template('addpaymentpage')
[6869]937    label = 'Add online payment'
938    title = 'Payments'
939    pnav = 4
[6947]940   
941    # To be sepezified in customization packages
942    def getPaymentDetails(self, category, student):
943        return getPaymentDetails(category, student)
[6869]944
945    @grok.action('Create ticket')
946    def createTicket(self, **data):
[7024]947        p_category = data['p_category']
948        student = self.context.__parent__
949        if p_category == 'bed_allocation' and student[
950            'studycourse'].current_session != grok.getSite()[
951            'configuration'].accommodation_session:
952                self.flash(
953                    'Your current session does not match accommodation session.')
954                self.redirect(self.url(self.context))
955                return
[6994]956        pay_details  = self.getPaymentDetails(
[7024]957            p_category,student)
[6994]958        if pay_details['error']:
959            self.flash(pay_details['error'])
[6940]960            self.redirect(self.url(self.context))
[6920]961            return
[7025]962        p_item = pay_details['p_item']
963        p_session = pay_details['p_session']
964        for key in self.context.keys():
965            ticket = self.context[key]
966            if ticket.p_category == p_category and \
967               ticket.p_item == p_item and \
968               ticket.p_session == p_session:
969                  self.flash(
970                      'This payment ticket already exists.')
971                  self.redirect(self.url(self.context))
972                  return
973        payment = createObject(u'waeup.StudentOnlinePayment')
974        self.applyData(payment, **data)
975        timestamp = "%d" % int(time()*1000)
976        #order_id = "%s%s" % (student_id[1:],timestamp)
977        payment.p_id = "p%s" % timestamp
978        payment.p_item = p_item
979        payment.p_session = p_session
[6994]980        payment.amount_auth = pay_details['amount']
981        payment.surcharge_1 = pay_details['surcharge_1']
982        payment.surcharge_2 = pay_details['surcharge_2']
983        payment.surcharge_3 = pay_details['surcharge_3']
[6869]984        self.context[payment.p_id] = payment
985        self.flash('Payment ticket created.')
[6940]986        self.redirect(self.url(self.context))
[6869]987        return
988
989class OnlinePaymentDisplayFormPage(WAeUPDisplayFormPage):
990    """ Page to view an online payment ticket
991    """
[6877]992    grok.context(IStudentOnlinePayment)
[6869]993    grok.name('index')
994    grok.require('waeup.viewStudent')
[6877]995    form_fields = grok.AutoFields(IStudentOnlinePayment)
[6869]996    form_fields['creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
997    form_fields['payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
998    pnav = 4
999
1000    @property
1001    def title(self):
1002        return 'Online Payment Ticket %s' % self.context.p_id
1003
1004    @property
1005    def label(self):
1006        return '%s: Online Payment Ticket %s' % (
[7019]1007            self.context.getStudent().fullname,self.context.p_id)
[6869]1008
[7019]1009class PaymentReceiptActionButton(ManageActionButton):
[7028]1010    grok.order(1)
[7019]1011    grok.context(IStudentOnlinePayment)
[7029]1012    grok.view(OnlinePaymentDisplayFormPage)
[7019]1013    grok.require('waeup.viewStudent')
1014    icon = 'actionicon_pdf.png'
1015    text = 'Download payment receipt'
1016    target = 'payment_receipt.pdf'
1017
[7028]1018    @property
1019    def target_url(self):
1020        if self.context.p_state != 'paid':
1021            return ''
1022        return self.view.url(self.view.context, self.target)
1023
[6930]1024class OnlinePaymentCallbackPage(grok.View):
1025    """ Callback view
1026    """
1027    grok.context(IStudentOnlinePayment)
1028    grok.name('callback')
1029    grok.require('waeup.payStudent')
1030
1031    # This update method simulates a valid callback und must be
1032    # specified in the customization package. The parameters must be taken
1033    # from the incoming request.
1034    def update(self):
[7026]1035        if self.context.p_state == 'paid':
1036            self.flash('This ticket has already been paid.')
1037            return
[6936]1038        student = self.context.getStudent()
[6943]1039        write_log_message(self,'valid callback: %s' % self.context.p_id)
[6930]1040        self.context.r_amount_approved = self.context.amount_auth
1041        self.context.r_card_num = u'0000'
1042        self.context.r_code = u'00'
1043        self.context.p_state = 'paid'
1044        self.context.payment_date = datetime.now()
1045        if self.context.p_category == 'clearance':
[6936]1046            # Create CLR access code
[6937]1047            pin, error = create_accesscode('CLR',0,student.student_id)
[6936]1048            if error:
[6940]1049                self.flash('Valid callback received. ' + error)
[6936]1050                return
1051            self.context.ac = pin
[6930]1052        elif self.context.p_category == 'schoolfee':
[6936]1053            # Create SFE access code
[6940]1054            pin, error = create_accesscode('SFE',0,student.student_id)
[6936]1055            if error:
[6940]1056                self.flash('Valid callback received. ' + error)
[6936]1057                return
1058            self.context.ac = pin
[6994]1059        elif self.context.p_category == 'bed_allocation':
1060            # Create HOS access code
1061            pin, error = create_accesscode('HOS',0,student.student_id)
1062            if error:
1063                self.flash('Valid callback received. ' + error)
1064                return
1065            self.context.ac = pin
[6940]1066        self.flash('Valid callback received.')
1067        return
[6930]1068
1069    def render(self):
[6940]1070        self.redirect(self.url(self.context, '@@index'))
[6930]1071        return
1072
[7019]1073class ExportPDFPaymentSlipPage(grok.View):
1074    """Deliver a PDF slip of the context.
1075    """
1076    grok.context(IStudentOnlinePayment)
1077    grok.name('payment_receipt.pdf')
1078    grok.require('waeup.viewStudent')
1079    form_fields = grok.AutoFields(IStudentOnlinePayment)
1080    form_fields['creation_date'].custom_widget = FriendlyDateDisplayWidget('le')
1081    form_fields['payment_date'].custom_widget = FriendlyDateDisplayWidget('le')
1082    prefix = 'form'
1083
1084    @property
1085    def label(self):
1086        return 'Online Payment Receipt %s' % self.context.p_id
1087
1088    def render(self):
[7028]1089        if self.context.p_state != 'paid':
1090            self.flash('Ticket not yet paid.')
1091            self.redirect(self.url(self.context))
1092            return
[7019]1093        studentview = StudentBaseDisplayFormPage(self.context.getStudent(),
1094            self.request)
1095        return renderPDF(self,'Payment', 'payment_receipt.pdf',
1096            self.context.getStudent, studentview)
1097
[6992]1098# We don't need the display form page yet
1099#class AccommodationDisplayFormPage(WAeUPDisplayFormPage):
1100#    """ Page to display the student accommodation data
1101#    """
1102#    grok.context(IStudentAccommodation)
1103#    grok.name('xxx')
1104#    grok.require('waeup.viewStudent')
1105#    form_fields = grok.AutoFields(IStudentAccommodation)
1106#    #grok.template('accommodationpage')
1107#    title = 'Accommodation'
1108#    pnav = 4
1109
1110#    @property
1111#    def label(self):
1112#        return '%s: Accommodation Data' % self.context.__parent__.fullname
1113
1114# This manage form page is for both students and students officers.
1115class AccommodationManageFormPage(WAeUPEditFormPage):
[7009]1116    """ Page to manage bed tickets.
[6635]1117    """
1118    grok.context(IStudentAccommodation)
1119    grok.name('index')
[6992]1120    grok.require('waeup.handleStudent')
[6635]1121    form_fields = grok.AutoFields(IStudentAccommodation)
[6992]1122    grok.template('accommodationmanagepage')
[6642]1123    title = 'Accommodation'
1124    pnav = 4
[7017]1125    officers_only_actions = ['Remove selected']
[6635]1126
[6992]1127    def formatDatetime(self,datetimeobj):
1128        if isinstance(datetimeobj, datetime):
1129            return datetimeobj.strftime("%Y-%m-%d %H:%M:%S")
1130        else:
1131            return None
1132
[6635]1133    @property
1134    def label(self):
[6992]1135        return '%s: Accommodation' % self.context.__parent__.fullname
[6637]1136
[6992]1137    def update(self):
1138        super(AccommodationManageFormPage, self).update()
1139        datatable.need()
1140        return
1141
[7017]1142    @property
1143    def is_student(self):
1144        prm = get_principal_role_manager()
1145        roles = [x[0] for x in prm.getRolesForPrincipal(self.request.principal.id)]
1146        return 'waeup.Student' in roles
1147
[7009]1148    @grok.action('Remove selected')
1149    def delBedTickets(self, **data):
[7017]1150        if self.is_student:
1151            self.flash('You are not allowed to remove bed tickets.')
1152            self.redirect(self.url(self.context))
1153            return
[6992]1154        form = self.request.form
1155        if form.has_key('val_id'):
1156            child_id = form['val_id']
1157        else:
1158            self.flash('No bed ticket selected.')
1159            self.redirect(self.url(self.context))
1160            return
1161        if not isinstance(child_id, list):
1162            child_id = [child_id]
1163        deleted = []
1164        for id in child_id:
[7009]1165            try:
1166                del self.context[id]
1167                deleted.append(id)
1168            except:
1169                self.flash('Could not delete %s: %s: %s' % (
1170                        id, sys.exc_info()[0], sys.exc_info()[1]))
[6992]1171        if len(deleted):
1172            self.flash('Successfully removed: %s' % ', '.join(deleted))
1173            write_log_message(self,'removed: % s' % ', '.join(deleted))
1174        self.redirect(self.url(self.context))
1175        return
1176
[7009]1177    @property
1178    def selected_actions(self):
1179        sa = self.actions
[7017]1180        if self.is_student:
[7009]1181            sa = [action for action in self.actions
[7017]1182                  if not action.label in self.officers_only_actions]
[7009]1183        return sa
1184
[7015]1185class AddBedTicketActionButton(ManageActionButton):
1186    grok.order(1)
1187    grok.context(IStudentAccommodation)
1188    grok.view(AccommodationManageFormPage)
1189    grok.require('waeup.handleStudent')
1190    icon = 'actionicon_home.png'
1191    text = 'Book accommodation'
1192    target = 'add'
1193
[6992]1194class BedTicketAddPage(WAeUPPage):
1195    """ Page to add an online payment ticket
1196    """
1197    grok.context(IStudentAccommodation)
1198    grok.name('add')
1199    grok.require('waeup.handleStudent')
1200    grok.template('enterpin')
[6993]1201    ac_prefix = 'HOS'
[6992]1202    label = 'Add bed ticket'
1203    title = 'Add bed ticket'
1204    pnav = 4
1205    buttonname = 'Create bed ticket'
[6993]1206    notice = ''
[6992]1207
1208    # To be sepezified in customization packages
1209    def getAccommodationDetails(self, student):
1210        return getAccommodationDetails(student)
1211
[7004]1212    # To be sepezified in customization packages
[7015]1213    def selectBed(self, available_beds):
1214        return selectBed(available_beds)
[7004]1215
[6992]1216    def update(self, SUBMIT=None):
[6996]1217        student = self.context.getStudent()
1218        acc_details  = self.getAccommodationDetails(student)
1219        if not student.state in acc_details['allowed_states']:
[7015]1220            self.flash("You are in the wrong registration state.")
[6992]1221            self.redirect(self.url(self.context))
1222            return
[7023]1223        if str(grok.getSite()[
1224                'configuration'].accommodation_session) in self.context.keys():
[7015]1225            self.flash('You already booked a bed space for the current accommodation session.')
[7004]1226            self.redirect(self.url(self.context))
1227            return
[6992]1228        self.ac_series = self.request.form.get('ac_series', None)
1229        self.ac_number = self.request.form.get('ac_number', None)
1230        if SUBMIT is None:
1231            return
1232        pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number)
1233        code = get_access_code(pin)
1234        if not code:
1235            self.flash('Activation code is invalid.')
1236            return
[7003]1237        # Search a bed and exit if no bed is found or if the student
1238        # has already booked a bed
[6997]1239        cat = queryUtility(ICatalog, name='beds_catalog', default=None)
1240        entries = cat.searchResults(
1241            bed_type=(acc_details['bt'],acc_details['bt']))
1242        available_beds = [
1243            entry for entry in entries if entry.owner == NOT_OCCUPIED]
1244        if not available_beds:
1245            self.flash('There is no free bed in your category %s.'
1246                        % acc_details['bt'])
1247            return
[7003]1248        entries = cat.searchResults(
1249            owner=(student.student_id,student.student_id))
[7004]1250        # Cannot happen but anyway ...
[7003]1251        if len(entries):
1252            bed = [entry for entry in entries][0]
1253            self.flash('You already booked bed %s.'
1254                        % bed.bed_id)
1255            return
[6992]1256        # Mark pin as used (this also fires a pin related transition)
1257        if code.state == USED:
1258            self.flash('Activation code has already been used.')
1259            return
1260        else:
1261            comment = u"AC invalidated for %s" % self.context.getStudent().student_id
1262            # Here we know that the ac is in state initialized so we do not
1263            # expect an exception, but the owner might be different
1264            if not invalidate_accesscode(
1265                pin,comment,self.context.getStudent().student_id):
1266                self.flash('You are not the owner of this access code.')
1267                return
[6997]1268        # Create bed ticket and book bed
[6992]1269        bedticket = createObject(u'waeup.BedTicket')
1270        bedticket.booking_code = pin
[6994]1271        bedticket.booking_session = acc_details['booking_session']
[6996]1272        bedticket.bed_type = acc_details['bt']
[7015]1273        bed = self.selectBed(available_beds)
[6996]1274        bed.bookBed(student.student_id)
[7006]1275        bedticket.bed = bed
[6996]1276        hall_title = bed.__parent__.hostel_name
1277        coordinates = bed.getBedCoordinates()[1:]
1278        block, room_nr, bed_nr = coordinates
1279        bedticket.bed_coordinates = '%s, Block %s, Room %s, Bed %s' % (
1280            hall_title, block, room_nr, bed_nr)
1281        key = str(acc_details['booking_session'])
1282        self.context[key] = bedticket
1283        self.flash('Bed ticket created and bed booked: %s'
1284            % bedticket.bed_coordinates)
[6992]1285        self.redirect(self.url(self.context))
1286        return
1287
[6994]1288class BedTicketDisplayFormPage(WAeUPDisplayFormPage):
1289    """ Page to display bed tickets
1290    """
1291    grok.context(IBedTicket)
1292    grok.name('index')
1293    grok.require('waeup.viewStudent')
1294    form_fields = grok.AutoFields(IBedTicket)
1295    form_fields[
1296        'booking_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
1297    pnav = 4
1298
1299    @property
1300    def label(self):
1301        return 'Bed Ticket for Session %s' % self.context.getSessionString()
1302
1303    @property
1304    def title(self):
1305        return 'Bed Ticket %s' % self.context.getSessionString()
1306
[7027]1307class BedTicketSlipActionButton(ManageActionButton):
[7028]1308    grok.order(1)
[7027]1309    grok.context(IBedTicket)
[7029]1310    grok.view(BedTicketDisplayFormPage)
[7027]1311    grok.require('waeup.viewStudent')
1312    icon = 'actionicon_pdf.png'
1313    text = 'Download bed allocation slip'
1314    target = 'bed_allocation.pdf'
1315
1316class ExportPDFBedTicketSlipPage(grok.View):
1317    """Deliver a PDF slip of the context.
1318    """
1319    grok.context(IBedTicket)
1320    grok.name('bed_allocation.pdf')
1321    grok.require('waeup.viewStudent')
1322    form_fields = grok.AutoFields(IBedTicket)
1323    form_fields['booking_date'].custom_widget = FriendlyDateDisplayWidget('le')
1324    prefix = 'form'
1325
1326    @property
1327    def label(self):
1328        return 'Bed Allocation %s' % self.context.bed_coordinates
1329
1330    def render(self):
1331        studentview = StudentBaseDisplayFormPage(self.context.getStudent(),
1332            self.request)
1333        return renderPDF(self,'Bed Allocation', 'bed_allocation.pdf',
1334            self.context.getStudent, studentview)
1335
[7015]1336class RelocateStudentActionButton(ManageActionButton):
[7027]1337    grok.order(2)
[7015]1338    grok.context(IBedTicket)
1339    grok.view(BedTicketDisplayFormPage)
1340    grok.require('waeup.manageHostels')
1341    icon = 'actionicon_reload.png'
1342    text = 'Relocate student'
1343    target = 'relocate'
1344
1345class BedTicketRelocationPage(grok.View):
1346    """ Callback view
1347    """
1348    grok.context(IBedTicket)
1349    grok.name('relocate')
1350    grok.require('waeup.manageHostels')
1351
1352    # To be sepezified in customization packages
1353    def getAccommodationDetails(self, student):
1354        return getAccommodationDetails(student)
1355
1356    # To be sepezified in customization packages
1357    def selectBed(self, available_beds):
1358        return selectBed(available_beds)
1359
1360    # This update method simulates a valid callback und must be
1361    # specified in the customization package. The parameters must be taken
1362    # from the incoming request.
1363    def update(self):
1364        student = self.context.getStudent()
1365        acc_details  = self.getAccommodationDetails(student)
1366        if acc_details['bt'] == self.context.bed_type:
1367            self.flash("Bed category hasn't changed. Student can't be relocated.")
1368            return
1369        # Search a bed and exit if no bed is found or if the student
1370        # has already booked a bed
1371        cat = queryUtility(ICatalog, name='beds_catalog', default=None)
1372        entries = cat.searchResults(
1373            bed_type=(acc_details['bt'],acc_details['bt']))
1374        available_beds = [
1375            entry for entry in entries if entry.owner == NOT_OCCUPIED]
1376        if not available_beds:
1377            self.flash('There is no free bed in your category %s.'
1378                        % acc_details['bt'])
1379            return
1380        entries = cat.searchResults(
1381            owner=(student.student_id,student.student_id))
1382        # Rlease old bed
[7042]1383        try:
1384            allocated_bed = self.context.bed
1385            allocated_bed.owner = NOT_OCCUPIED
1386            notify(grok.ObjectModifiedEvent(allocated_bed))
1387        except AttributeError:
1388            # Cannot happen but anyway...
1389            pass
[7015]1390        # Alocate new bed
1391        self.context.bed_type = acc_details['bt']
1392        bed = self.selectBed(available_beds)
1393        bed.bookBed(student.student_id)
1394        self.context.bed = bed
1395        hall_title = bed.__parent__.hostel_name
1396        coordinates = bed.getBedCoordinates()[1:]
1397        block, room_nr, bed_nr = coordinates
1398        self.context.bed_coordinates = '%s, Block %s, Room %s, Bed %s' % (
1399            hall_title, block, room_nr, bed_nr)
1400        self.flash('Student relocated and new bed booked: %s'
1401            % self.context.bed_coordinates)
1402        self.redirect(self.url(self.context))
1403        return
1404
1405    def render(self):
1406        self.redirect(self.url(self.context, '@@index'))
1407        return
1408
[6637]1409class StudentHistoryPage(WAeUPPage):
1410    """ Page to display student clearance data
1411    """
1412    grok.context(IStudent)
1413    grok.name('history')
[6660]1414    grok.require('waeup.viewStudent')
[6637]1415    grok.template('studenthistory')
[6642]1416    title = 'History'
1417    pnav = 4
[6637]1418
1419    @property
1420    def label(self):
[6818]1421        return '%s: History' % self.context.fullname
[6694]1422
1423# Pages for students only
1424
1425class StudentBaseEditActionButton(ManageActionButton):
1426    grok.order(1)
1427    grok.context(IStudent)
1428    grok.view(StudentBaseDisplayFormPage)
1429    grok.require('waeup.handleStudent')
1430    text = 'Change password'
1431    target = 'bedit'
1432
[6756]1433class StudentPasswordSetting(grok.Adapter):
1434    """Adapt IStudent to data needed for password settings.
1435
1436    We provide password getters/setters for the attached context (an
1437    IStudent object) that cooperate seamless with the usual
1438    formlib/form techniques.
1439    """
1440    grok.context(IStudent)
1441    grok.provides(IStudentPasswordSetting)
1442
1443    def __init__(self, context):
[6818]1444        self.name = context.fullname
[6756]1445        self.password_repeat = context.password
1446        self.context = context
1447        return
1448
1449    def getPassword(self):
1450        return self.context.password
1451
1452    def setPassword(self, password):
1453        IUserAccount(self.context).setPassword(password)
1454        return
1455
1456    password = property(getPassword, setPassword)
1457
[6694]1458class StudentBaseEditFormPage(WAeUPEditFormPage):
1459    """ View to edit student base data by student
1460    """
1461    grok.context(IStudent)
1462    grok.name('bedit')
1463    grok.require('waeup.handleStudent')
[6756]1464    #form_fields = grok.AutoFields(IStudentBaseEdit).omit(
1465    #    'student_id', 'reg_number', 'matric_number')
1466    form_fields = grok.AutoFields(IStudentPasswordSetting)
[6695]1467    grok.template('baseeditpage')
[6694]1468    label = 'Change password'
1469    title = 'Base Data'
1470    pnav = 4
1471
1472    def update(self):
1473        super(StudentBaseEditFormPage, self).update()
1474        self.wf_info = IWorkflowInfo(self.context)
1475        return
1476
[6756]1477    def onFailure(self, action, data, errors):
1478        new_status = []
1479        other_errors = False
1480        for error in errors:
1481            msg = getattr(error, 'message', '')
1482            if isinstance(msg, basestring) and msg != '':
1483                new_status.append(msg)
1484            else:
1485                other_errors = True
1486        if other_errors:
1487            if new_status:
1488                new_status.append('see below for further errors')
1489            else:
1490                new_status.append('See below for details.')
1491        if new_status:
1492            self.status = u'There were errors: %s' % ', '.join(new_status)
1493        return
1494
1495    @grok.action('Save', failure=onFailure)
[6694]1496    def save(self, **data):
[6771]1497        self.applyData(self.context, **data)
1498        self.flash('Form has been saved.')
[6694]1499        return
[6695]1500
[6719]1501class StudentClearanceStartActionButton(ManageActionButton):
1502    grok.order(1)
1503    grok.context(IStudent)
1504    grok.view(StudentClearanceDisplayFormPage)
1505    grok.require('waeup.handleStudent')
1506    icon = 'actionicon_start.png'
1507    text = 'Start clearance'
1508    target = 'start_clearance'
1509
1510    @property
1511    def target_url(self):
1512        if self.context.state != 'admitted':
1513            return ''
1514        return self.view.url(self.view.context, self.target)
1515
[6773]1516class StartClearancePage(WAeUPPage):
[6770]1517    grok.context(IStudent)
1518    grok.name('start_clearance')
1519    grok.require('waeup.handleStudent')
1520    grok.template('enterpin')
1521    title = 'Start clearance'
1522    label = 'Start clearance'
1523    ac_prefix = 'CLR'
1524    notice = ''
1525    pnav = 4
1526    buttonname = 'Start clearance now'
1527
1528    def update(self, SUBMIT=None):
[6936]1529        if not self.context.state == 'admitted':
1530            self.flash("Wrong state.")
1531            self.redirect(self.url(self.context))
1532            return
[6770]1533        self.ac_series = self.request.form.get('ac_series', None)
1534        self.ac_number = self.request.form.get('ac_number', None)
1535
1536        if SUBMIT is None:
1537            return
1538        pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number)
1539        code = get_access_code(pin)
1540        if not code:
[6936]1541            self.flash('Activation code is invalid.')
[6770]1542            return
1543        # Mark pin as used (this also fires a pin related transition)
1544        # and fire transition start_clearance
1545        if code.state == USED:
[6936]1546            self.flash('Activation code has already been used.')
[6770]1547            return
1548        else:
1549            comment = u"AC invalidated for %s" % self.context.student_id
1550            # Here we know that the ac is in state initialized so we do not
[6927]1551            # expect an exception, but the owner might be different
1552            if not invalidate_accesscode(pin,comment,self.context.student_id):
1553                self.flash('You are not the owner of this access code.')
1554                return
[6770]1555            self.context.clr_code = pin
1556        IWorkflowInfo(self.context).fireTransition('start_clearance')
[6771]1557        self.flash('Clearance process has been started.')
[6770]1558        self.redirect(self.url(self.context,'cedit'))
1559        return
1560
[6695]1561class StudentClearanceEditActionButton(ManageActionButton):
1562    grok.order(1)
1563    grok.context(IStudent)
1564    grok.view(StudentClearanceDisplayFormPage)
1565    grok.require('waeup.handleStudent')
[6722]1566    text = 'Edit'
[6695]1567    target = 'cedit'
1568
[6717]1569    @property
1570    def target_url(self):
1571        if self.context.clearance_locked:
1572            return ''
1573        return self.view.url(self.view.context, self.target)
1574
[6695]1575class StudentClearanceEditFormPage(StudentClearanceManageFormPage):
1576    """ View to edit student clearance data by student
1577    """
1578    grok.context(IStudent)
1579    grok.name('cedit')
1580    grok.require('waeup.handleStudent')
[6756]1581    form_fields = grok.AutoFields(
1582        IStudentClearanceEdit).omit('clearance_locked')
[6722]1583    label = 'Edit clearance data'
[6695]1584    title = 'Clearance Data'
1585    pnav = 4
1586    form_fields['date_of_birth'].custom_widget = FriendlyDateWidget('le-year')
[6718]1587
1588    def emitLockMessage(self):
1589        self.flash('The requested form is locked (read-only).')
1590        self.redirect(self.url(self.context))
1591        return
1592
1593    def update(self):
1594        if self.context.clearance_locked:
1595            self.emitLockMessage()
1596            return
1597        datepicker.need()
1598        return super(StudentClearanceEditFormPage, self).update()
[6719]1599
[6722]1600    @grok.action('Save')
1601    def save(self, **data):
1602        self.applyData(self.context, **data)
[6771]1603        self.flash('Clearance form has been saved.')
[6722]1604        return
1605
1606    @grok.action('Save and request clearance')
1607    def requestclearance(self, **data):
1608        self.applyData(self.context, **data)
1609        self.context._p_changed = True
1610        #if self.dataNotComplete():
1611        #    self.flash(self.dataNotComplete())
1612        #    return
[6771]1613        self.flash('Clearance form has been saved.')
[6769]1614        self.redirect(self.url(self.context,'request_clearance'))
[6722]1615        return
1616
[6773]1617class RequestClearancePage(WAeUPPage):
[6769]1618    grok.context(IStudent)
1619    grok.name('request_clearance')
1620    grok.require('waeup.handleStudent')
1621    grok.template('enterpin')
1622    title = 'Request clearance'
1623    label = 'Request clearance'
1624    notice = 'Enter the CLR access code used for starting clearance.'
1625    ac_prefix = 'CLR'
1626    pnav = 4
1627    buttonname = 'Request clearance now'
1628
1629    def update(self, SUBMIT=None):
1630        self.ac_series = self.request.form.get('ac_series', None)
1631        self.ac_number = self.request.form.get('ac_number', None)
1632        if SUBMIT is None:
1633            return
1634        pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number)
1635        if self.context.clr_code != pin:
1636            self.flash("This isn't your CLR access code.")
1637            return
1638        state = IWorkflowState(self.context).getState()
1639        # This shouldn't happen, but the application officer
1640        # might have forgotten to lock the form after changing the state
1641        if state != CLEARANCE:
1642            self.flash('This form cannot be submitted. Wrong state!')
1643            return
1644        IWorkflowInfo(self.context).fireTransition('request_clearance')
1645        self.flash('Clearance has been requested.')
1646        self.redirect(self.url(self.context))
[6789]1647        return
[6806]1648
[6944]1649class CourseRegistrationStartActionButton(ManageActionButton):
1650    grok.order(1)
1651    grok.context(IStudentStudyCourse)
1652    grok.view(StudyCourseDisplayFormPage)
1653    grok.require('waeup.handleStudent')
1654    icon = 'actionicon_start.png'
1655    text = 'Start course registration'
1656    target = 'start_course_registration'
1657
1658    @property
1659    def target_url(self):
1660        if not self.context.getStudent().state in (CLEARED,RETURNING):
1661            return ''
1662        return self.view.url(self.view.context, self.target)
1663
1664class StartCourseRegistrationPage(WAeUPPage):
1665    grok.context(IStudentStudyCourse)
1666    grok.name('start_course_registration')
1667    grok.require('waeup.handleStudent')
1668    grok.template('enterpin')
1669    title = 'Start course registration'
1670    label = 'Start course registration'
1671    ac_prefix = 'SFE'
1672    notice = ''
1673    pnav = 4
1674    buttonname = 'Start course registration now'
1675
1676    def update(self, SUBMIT=None):
1677        if not self.context.getStudent().state in (CLEARED,RETURNING):
1678            self.flash("Wrong state.")
1679            self.redirect(self.url(self.context))
1680            return
1681        self.ac_series = self.request.form.get('ac_series', None)
1682        self.ac_number = self.request.form.get('ac_number', None)
1683
1684        if SUBMIT is None:
1685            return
1686        pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number)
1687        code = get_access_code(pin)
1688        if not code:
1689            self.flash('Activation code is invalid.')
1690            return
1691        # Mark pin as used (this also fires a pin related transition)
1692        # and fire transition start_clearance
1693        if code.state == USED:
1694            self.flash('Activation code has already been used.')
1695            return
1696        else:
1697            comment = u"AC invalidated for %s" % self.context.getStudent().student_id
1698            # Here we know that the ac is in state initialized so we do not
1699            # expect an exception, but the owner might be different
1700            if not invalidate_accesscode(
1701                pin,comment,self.context.getStudent().student_id):
1702                self.flash('You are not the owner of this access code.')
1703                return
1704        if self.context.getStudent().state == CLEARED:
1705            IWorkflowInfo(self.context.getStudent()).fireTransition(
1706                'pay_first_school_fee')
1707        elif self.context.getStudent().state == RETURNING:
1708            IWorkflowInfo(self.context.getStudent()).fireTransition(
1709                'pay_school_fee')
1710        self.flash('Course registration has been started.')
1711        self.redirect(self.url(self.context))
1712        return
1713
1714
[6808]1715class AddStudyLevelActionButton(AddActionButton):
1716    grok.order(1)
1717    grok.context(IStudentStudyCourse)
1718    grok.view(StudyCourseDisplayFormPage)
1719    grok.require('waeup.handleStudent')
1720    text = 'Add course list'
1721    target = 'add'
1722
1723    @property
1724    def target_url(self):
1725        student = self.view.context.getStudent()
1726        condition1 = student.state != 'school fee paid'
1727        condition2 = str(student['studycourse'].current_level) in \
1728            self.view.context.keys()
1729        if condition1 or condition2:
1730            return ''
1731        return self.view.url(self.view.context, self.target)
1732
[6806]1733class AddStudyLevelFormPage(WAeUPEditFormPage):
1734    """ Page for students to add current study levels
1735    """
1736    grok.context(IStudentStudyCourse)
1737    grok.name('add')
1738    grok.require('waeup.handleStudent')
1739    grok.template('studyleveladdpage')
1740    form_fields = grok.AutoFields(IStudentStudyCourse)
1741    title = 'Study Course'
1742    pnav = 4
1743
1744    @property
1745    def label(self):
1746        studylevelsource = StudyLevelSource().factory
1747        code = self.context.current_level
1748        title = studylevelsource.getTitle(self.context, code)
1749        return 'Add current level %s' % title
1750
1751    def emitLockMessage(self):
1752        self.flash('The requested form is locked (read-only).')
1753        self.redirect(self.url(self.context))
1754        return
1755
1756    def update(self):
1757        if self.context.getStudent().state != 'school fee paid':
1758            self.emitLockMessage()
1759            return
1760        super(AddStudyLevelFormPage, self).update()
1761        return
1762
1763    @grok.action('Create course list now')
1764    def addStudyLevel(self, **data):
1765        studylevel = StudentStudyLevel()
1766        studylevel.level = self.context.current_level
1767        studylevel.level_session = self.context.current_session
1768        try:
1769            self.context.addStudentStudyLevel(
1770                self.context.certificate,studylevel)
1771        except KeyError:
1772            self.flash('This level exists.')
1773        self.redirect(self.url(self.context))
1774        return
[6808]1775
1776class StudyLevelEditActionButton(ManageActionButton):
1777    grok.order(1)
1778    grok.context(IStudentStudyLevel)
1779    grok.view(StudyLevelDisplayFormPage)
1780    grok.require('waeup.handleStudent')
1781    text = 'Add and remove courses'
1782    target = 'edit'
1783
1784    @property
1785    def target_url(self):
1786        student = self.view.context.getStudent()
1787        condition1 = student.state != 'school fee paid'
1788        condition2 = student[
1789            'studycourse'].current_level != self.view.context.level
1790        if condition1 or condition2:
1791            return ''
1792        return self.view.url(self.view.context, self.target)
1793
1794class StudyLevelEditFormPage(WAeUPEditFormPage):
1795    """ Page to edit the student study level data by students
1796    """
1797    grok.context(IStudentStudyLevel)
1798    grok.name('edit')
1799    grok.require('waeup.handleStudent')
1800    grok.template('studyleveleditpage')
1801    form_fields = grok.AutoFields(IStudentStudyLevel).omit(
1802        'level_session', 'level_verdict')
1803    pnav = 4
1804
1805    def update(self):
1806        super(StudyLevelEditFormPage, self).update()
1807    #    tabs.need()
1808        datatable.need()
1809        return
1810
1811    @property
1812    def title(self):
1813        return 'Study Level %s' % self.context.level_title
1814
1815    @property
1816    def label(self):
1817        return 'Add and remove course tickets of study level %s' % self.context.level_title
1818
1819    @property
1820    def total_credits(self):
1821        total_credits = 0
1822        for key, val in self.context.items():
1823            total_credits += val.credits
1824        return total_credits
1825
1826    @grok.action('Add course ticket')
1827    def addCourseTicket(self, **data):
1828        self.redirect(self.url(self.context, 'ctadd'))
1829
1830    @grok.action('Remove selected tickets')
1831    def delCourseTicket(self, **data):
1832        form = self.request.form
1833        if form.has_key('val_id'):
1834            child_id = form['val_id']
1835        else:
1836            self.flash('No ticket selected.')
1837            self.redirect(self.url(self.context, '@@edit'))
1838            return
1839        if not isinstance(child_id, list):
1840            child_id = [child_id]
1841        deleted = []
1842        for id in child_id:
[6940]1843            # Students are not allowed to remove core tickets
[6808]1844            if not self.context[id].core_or_elective:
1845                try:
1846                    del self.context[id]
1847                    deleted.append(id)
1848                except:
1849                    self.flash('Could not delete %s: %s: %s' % (
1850                            id, sys.exc_info()[0], sys.exc_info()[1]))
1851        if len(deleted):
1852            self.flash('Successfully removed: %s' % ', '.join(deleted))
1853        self.redirect(self.url(self.context, u'@@edit'))
1854        return
1855
[6810]1856    @grok.action('Register course list')
1857    def register_courses(self, **data):
1858        state = IWorkflowState(self.context.getStudent()).getState()
1859        IWorkflowInfo(self.context.getStudent()).fireTransition('register_courses')
1860        self.flash('Course list has been registered.')
1861        self.redirect(self.url(self.context))
1862        return
1863
[6808]1864class CourseTicketAddFormPage2(CourseTicketAddFormPage):
1865    """Add a course ticket by student.
1866    """
1867    grok.name('ctadd')
1868    grok.require('waeup.handleStudent')
1869    form_fields = grok.AutoFields(ICourseTicketAdd).omit(
1870        'grade', 'score', 'core_or_elective', 'automatic')
1871
1872    @grok.action('Add course ticket')
1873    def addCourseTicket(self, **data):
1874        ticket = CourseTicket()
1875        course = data['course']
1876        ticket.automatic = False
1877        ticket.code = course.code
1878        ticket.title = course.title
1879        ticket.faculty = course.__parent__.__parent__.__parent__.title
1880        ticket.department = course.__parent__.__parent__.title
1881        ticket.credits = course.credits
1882        ticket.passmark = course.passmark
1883        ticket.semester = course.semester
1884        try:
1885            self.context.addCourseTicket(ticket)
1886        except KeyError:
1887            self.flash('The ticket exists.')
1888            return
1889        self.flash('Successfully added %s.' % ticket.code)
1890        self.redirect(self.url(self.context, u'@@edit'))
1891        return
Note: See TracBrowser for help on using the repository browser.