source: main/waeup.kofa/trunk/src/waeup/kofa/students/browser.py @ 8410

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

Implement search page for applicants. Add fullname to applicants_catalog.
Plugins must be updated and /reindex?ctlg=applicants must be performed.

Tests will follow.

Rename ApplicantCatalog? to ApplicantsCatalog?. This does not affect persistent data.

Rename StudentIndexes? to StudentsCatalog?.

Add more localization.

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