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

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

Payment methods do now all return a success flag, a flash message string and a log string. All these adjustments are necessary for waeup.uniben.

  • Property svn:keywords set to Id
File size: 68.8 KB
RevLine 
[7191]1## $Id: browser.py 8428 2012-05-12 07:01:04Z 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 (
[8420]34    invalidate_accesscode, get_access_code)
[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
[8420]1132class OnlinePaymentApprovePage(UtilityView, grok.View):
[6930]1133    """ Callback view
1134    """
1135    grok.context(IStudentOnlinePayment)
[8420]1136    grok.name('approve')
1137    grok.require('waeup.managePortal')
[6930]1138
1139    def update(self):
[8428]1140        success, msg, log = self.context.approveStudentPayment()
1141        if log is not None:
1142            write_log_message(self,log)
[8420]1143        self.flash(msg)
[6940]1144        return
[6930]1145
1146    def render(self):
[6940]1147        self.redirect(self.url(self.context, '@@index'))
[6930]1148        return
1149
[8420]1150class OnlinePaymentFakeApprovePage(OnlinePaymentApprovePage):
1151    """ Approval view for students.
1152
1153    This view is used for browser tests only and
1154    must be neutralized in custom pages!
1155    """
1156
1157    grok.name('fake_approve')
1158    grok.require('waeup.payStudent')
1159
[7459]1160class ExportPDFPaymentSlipPage(UtilityView, grok.View):
[7019]1161    """Deliver a PDF slip of the context.
1162    """
1163    grok.context(IStudentOnlinePayment)
[8262]1164    grok.name('payment_slip.pdf')
[7019]1165    grok.require('waeup.viewStudent')
1166    form_fields = grok.AutoFields(IStudentOnlinePayment)
[8173]1167    form_fields['creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
1168    form_fields['payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
[7019]1169    prefix = 'form'
[8258]1170    note = None
[7019]1171
1172    @property
[8262]1173    def title(self):
1174        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
1175        return translate(_('Payment Data'), 'waeup.kofa',
1176            target_language=portal_language)
1177
1178    @property
[7019]1179    def label(self):
[8262]1180        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
1181        return translate(_('Online Payment Slip'),
1182            'waeup.kofa', target_language=portal_language) \
1183            + ' %s' % self.context.p_id
[7019]1184
1185    def render(self):
[8262]1186        #if self.context.p_state != 'paid':
1187        #    self.flash('Ticket not yet paid.')
1188        #    self.redirect(self.url(self.context))
1189        #    return
[7019]1190        studentview = StudentBaseDisplayFormPage(self.context.getStudent(),
1191            self.request)
[7150]1192        students_utils = getUtility(IStudentsUtils)
[8262]1193        return students_utils.renderPDF(self, 'payment_slip.pdf',
[8258]1194            self.context.getStudent(), studentview, note=self.note)
[7019]1195
[6992]1196
[7819]1197class AccommodationManageFormPage(KofaEditFormPage):
[7009]1198    """ Page to manage bed tickets.
[7642]1199
1200    This manage form page is for both students and students officers.
[6635]1201    """
1202    grok.context(IStudentAccommodation)
1203    grok.name('index')
[7181]1204    grok.require('waeup.handleAccommodation')
[6635]1205    form_fields = grok.AutoFields(IStudentAccommodation)
[6992]1206    grok.template('accommodationmanagepage')
[6642]1207    pnav = 4
[7723]1208    officers_only_actions = [_('Remove selected')]
[6635]1209
1210    @property
1211    def label(self):
[7723]1212        return _('${a}: Accommodation',
1213            mapping = {'a':self.context.__parent__.display_fullname})
[6637]1214
[6992]1215    def update(self):
1216        super(AccommodationManageFormPage, self).update()
1217        datatable.need()
[7329]1218        warning.need()
[6992]1219        return
1220
[7723]1221    @jsaction(_('Remove selected'))
[7009]1222    def delBedTickets(self, **data):
[7240]1223        if getattr(self.request.principal, 'user_type', None) == 'student':
[7723]1224            self.flash(_('You are not allowed to remove bed tickets.'))
[7017]1225            self.redirect(self.url(self.context))
1226            return
[6992]1227        form = self.request.form
1228        if form.has_key('val_id'):
1229            child_id = form['val_id']
1230        else:
[7723]1231            self.flash(_('No bed ticket selected.'))
[6992]1232            self.redirect(self.url(self.context))
1233            return
1234        if not isinstance(child_id, list):
1235            child_id = [child_id]
1236        deleted = []
1237        for id in child_id:
[7068]1238            del self.context[id]
1239            deleted.append(id)
[6992]1240        if len(deleted):
[7723]1241            self.flash(_('Successfully removed: ${a}',
1242                mapping = {'a':', '.join(deleted)}))
[6992]1243            write_log_message(self,'removed: % s' % ', '.join(deleted))
1244        self.redirect(self.url(self.context))
1245        return
1246
[7009]1247    @property
1248    def selected_actions(self):
[7240]1249        if getattr(self.request.principal, 'user_type', None) == 'student':
[7642]1250            return [action for action in self.actions
1251                    if not action.label in self.officers_only_actions]
1252        return self.actions
[7009]1253
[7819]1254class BedTicketAddPage(KofaPage):
[6992]1255    """ Page to add an online payment ticket
1256    """
1257    grok.context(IStudentAccommodation)
1258    grok.name('add')
[7181]1259    grok.require('waeup.handleAccommodation')
[6992]1260    grok.template('enterpin')
[6993]1261    ac_prefix = 'HOS'
[7723]1262    label = _('Add bed ticket')
[6992]1263    pnav = 4
[7723]1264    buttonname = _('Create bed ticket')
[6993]1265    notice = ''
[6992]1266
1267    def update(self, SUBMIT=None):
[6996]1268        student = self.context.getStudent()
[7150]1269        students_utils = getUtility(IStudentsUtils)
[7186]1270        acc_details  = students_utils.getAccommodationDetails(student)
[7369]1271        if not acc_details:
[7723]1272            self.flash(_("Your data are incomplete."))
[7369]1273            self.redirect(self.url(self.context))
1274            return
[6996]1275        if not student.state in acc_details['allowed_states']:
[7723]1276            self.flash(_("You are in the wrong registration state."))
[6992]1277            self.redirect(self.url(self.context))
1278            return
[7642]1279        if student['studycourse'].current_session != acc_details[
1280            'booking_session']:
[7061]1281            self.flash(
[7723]1282                _('Your current session does not match accommodation session.'))
[7061]1283            self.redirect(self.url(self.context))
1284            return
1285        if str(acc_details['booking_session']) in self.context.keys():
[7642]1286            self.flash(
[7723]1287                _('You already booked a bed space in current ' \
1288                    + 'accommodation session.'))
[7004]1289            self.redirect(self.url(self.context))
1290            return
[6992]1291        self.ac_series = self.request.form.get('ac_series', None)
1292        self.ac_number = self.request.form.get('ac_number', None)
1293        if SUBMIT is None:
1294            return
1295        pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number)
1296        code = get_access_code(pin)
1297        if not code:
[7723]1298            self.flash(_('Activation code is invalid.'))
[6992]1299            return
[7060]1300        # Search and book bed
[6997]1301        cat = queryUtility(ICatalog, name='beds_catalog', default=None)
1302        entries = cat.searchResults(
[7003]1303            owner=(student.student_id,student.student_id))
1304        if len(entries):
[7060]1305            # If bed space has bee manually allocated use this bed
[7003]1306            bed = [entry for entry in entries][0]
[7060]1307        else:
1308            # else search for other available beds
1309            entries = cat.searchResults(
1310                bed_type=(acc_details['bt'],acc_details['bt']))
1311            available_beds = [
1312                entry for entry in entries if entry.owner == NOT_OCCUPIED]
1313            if available_beds:
[7150]1314                students_utils = getUtility(IStudentsUtils)
[7186]1315                bed = students_utils.selectBed(available_beds)
[7060]1316                bed.bookBed(student.student_id)
1317            else:
[7723]1318                self.flash(_('There is no free bed in your category ${a}.',
1319                    mapping = {'a':acc_details['bt']}))
[7060]1320                return
[6992]1321        # Mark pin as used (this also fires a pin related transition)
1322        if code.state == USED:
[7723]1323            self.flash(_('Activation code has already been used.'))
[6992]1324            return
1325        else:
[7723]1326            comment = _(u'invalidated')
[6992]1327            # Here we know that the ac is in state initialized so we do not
1328            # expect an exception, but the owner might be different
1329            if not invalidate_accesscode(
1330                pin,comment,self.context.getStudent().student_id):
[7723]1331                self.flash(_('You are not the owner of this access code.'))
[6992]1332                return
[7060]1333        # Create bed ticket
[6992]1334        bedticket = createObject(u'waeup.BedTicket')
1335        bedticket.booking_code = pin
[6994]1336        bedticket.booking_session = acc_details['booking_session']
[6996]1337        bedticket.bed_type = acc_details['bt']
[7006]1338        bedticket.bed = bed
[6996]1339        hall_title = bed.__parent__.hostel_name
1340        coordinates = bed.getBedCoordinates()[1:]
1341        block, room_nr, bed_nr = coordinates
[7723]1342        bc = _('${a}, Block ${b}, Room ${c}, Bed ${d} (${e})', mapping = {
1343            'a':hall_title, 'b':block,
1344            'c':room_nr, 'd':bed_nr,
1345            'e':bed.bed_type})
[7819]1346        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
[7723]1347        bedticket.bed_coordinates = translate(
[7811]1348            bc, 'waeup.kofa',target_language=portal_language)
[6996]1349        key = str(acc_details['booking_session'])
1350        self.context[key] = bedticket
[7723]1351        self.flash(_('Bed ticket created and bed booked: ${a}',
1352            mapping = {'a':bedticket.bed_coordinates}))
[6992]1353        self.redirect(self.url(self.context))
1354        return
1355
[7819]1356class BedTicketDisplayFormPage(KofaDisplayFormPage):
[6994]1357    """ Page to display bed tickets
1358    """
1359    grok.context(IBedTicket)
1360    grok.name('index')
[7181]1361    grok.require('waeup.handleAccommodation')
[6994]1362    form_fields = grok.AutoFields(IBedTicket)
1363    pnav = 4
1364
1365    @property
1366    def label(self):
[7723]1367        return _('Bed Ticket for Session ${a}',
1368            mapping = {'a':self.context.getSessionString()})
[6994]1369
[7459]1370class ExportPDFBedTicketSlipPage(UtilityView, grok.View):
[7027]1371    """Deliver a PDF slip of the context.
1372    """
1373    grok.context(IBedTicket)
1374    grok.name('bed_allocation.pdf')
[7181]1375    grok.require('waeup.handleAccommodation')
[7027]1376    form_fields = grok.AutoFields(IBedTicket)
[8173]1377    form_fields['booking_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
[7027]1378    prefix = 'form'
1379
1380    @property
[7723]1381    def title(self):
[7819]1382        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
[7811]1383        return translate(_('Bed Allocation Data'), 'waeup.kofa',
[7723]1384            target_language=portal_language)
1385
1386    @property
[7027]1387    def label(self):
[7819]1388        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
[7723]1389        return translate(_('Bed Allocation: '),
[7811]1390            'waeup.kofa', target_language=portal_language) \
[7723]1391            + ' %s' % self.context.bed_coordinates
[7027]1392
1393    def render(self):
1394        studentview = StudentBaseDisplayFormPage(self.context.getStudent(),
1395            self.request)
[7150]1396        students_utils = getUtility(IStudentsUtils)
[7186]1397        return students_utils.renderPDF(
[7318]1398            self, 'bed_allocation.pdf',
[7310]1399            self.context.getStudent(), studentview)
[7027]1400
[7459]1401class BedTicketRelocationPage(UtilityView, grok.View):
[7015]1402    """ Callback view
1403    """
1404    grok.context(IBedTicket)
1405    grok.name('relocate')
1406    grok.require('waeup.manageHostels')
1407
[7059]1408    # Relocate student if student parameters have changed or the bed_type
1409    # of the bed has changed
[7015]1410    def update(self):
1411        student = self.context.getStudent()
[7150]1412        students_utils = getUtility(IStudentsUtils)
[7186]1413        acc_details  = students_utils.getAccommodationDetails(student)
[7068]1414        if self.context.bed != None and \
1415              'reserved' in self.context.bed.bed_type:
[7723]1416            self.flash(_("Students in reserved beds can't be relocated."))
[7068]1417            self.redirect(self.url(self.context))
1418            return
[7059]1419        if acc_details['bt'] == self.context.bed_type and \
[7068]1420                self.context.bed != None and \
[7059]1421                self.context.bed.bed_type == self.context.bed_type:
[7723]1422            self.flash(_("Student can't be relocated."))
[7068]1423            self.redirect(self.url(self.context))
[7015]1424            return
[7068]1425        # Search a bed
[7015]1426        cat = queryUtility(ICatalog, name='beds_catalog', default=None)
1427        entries = cat.searchResults(
[7068]1428            owner=(student.student_id,student.student_id))
1429        if len(entries) and self.context.bed == None:
1430            # If booking has been cancelled but other bed space has been
1431            # manually allocated after cancellation use this bed
1432            new_bed = [entry for entry in entries][0]
1433        else:
1434            # Search for other available beds
1435            entries = cat.searchResults(
1436                bed_type=(acc_details['bt'],acc_details['bt']))
1437            available_beds = [
1438                entry for entry in entries if entry.owner == NOT_OCCUPIED]
1439            if available_beds:
[7150]1440                students_utils = getUtility(IStudentsUtils)
[7186]1441                new_bed = students_utils.selectBed(available_beds)
[7068]1442                new_bed.bookBed(student.student_id)
1443            else:
[7723]1444                self.flash(_('There is no free bed in your category ${a}.',
1445                    mapping = {'a':acc_details['bt']}))
[7068]1446                self.redirect(self.url(self.context))
1447                return
[7642]1448        # Release old bed if exists
[7068]1449        if self.context.bed != None:
1450            self.context.bed.owner = NOT_OCCUPIED
1451            notify(grok.ObjectModifiedEvent(self.context.bed))
[7015]1452        # Alocate new bed
1453        self.context.bed_type = acc_details['bt']
[7068]1454        self.context.bed = new_bed
1455        hall_title = new_bed.__parent__.hostel_name
1456        coordinates = new_bed.getBedCoordinates()[1:]
[7015]1457        block, room_nr, bed_nr = coordinates
[7723]1458        bc = _('${a}, Block ${b}, Room ${c}, Bed ${d} (${e})', mapping = {
1459            'a':hall_title, 'b':block,
1460            'c':room_nr, 'd':bed_nr,
1461            'e':new_bed.bed_type})
[7819]1462        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
[7723]1463        self.context.bed_coordinates = translate(
[7811]1464            bc, 'waeup.kofa',target_language=portal_language)
[7723]1465        self.flash(_('Student relocated: ${a}',
1466            mapping = {'a':self.context.bed_coordinates}))
[7015]1467        self.redirect(self.url(self.context))
1468        return
1469
1470    def render(self):
1471        return
1472
[7819]1473class StudentHistoryPage(KofaPage):
[6637]1474    """ Page to display student clearance data
1475    """
1476    grok.context(IStudent)
1477    grok.name('history')
[6660]1478    grok.require('waeup.viewStudent')
[6637]1479    grok.template('studenthistory')
[6642]1480    pnav = 4
[6637]1481
1482    @property
1483    def label(self):
[7723]1484        return _('${a}: History', mapping = {'a':self.context.display_fullname})
[6694]1485
1486# Pages for students only
1487
[7819]1488class StudentBaseEditFormPage(KofaEditFormPage):
[7133]1489    """ View to edit student base data
1490    """
1491    grok.context(IStudent)
1492    grok.name('edit_base')
1493    grok.require('waeup.handleStudent')
1494    form_fields = grok.AutoFields(IStudentBase).select(
1495        'email', 'phone')
[7723]1496    label = _('Edit base data')
[7133]1497    pnav = 4
1498
[7723]1499    @action(_('Save'), style='primary')
[7133]1500    def save(self, **data):
1501        msave(self, **data)
1502        return
1503
[7819]1504class StudentChangePasswordPage(KofaEditFormPage):
[7144]1505    """ View to manage student base data
[6756]1506    """
1507    grok.context(IStudent)
[7114]1508    grok.name('change_password')
[6694]1509    grok.require('waeup.handleStudent')
[7144]1510    grok.template('change_password')
[7723]1511    label = _('Change password')
[6694]1512    pnav = 4
1513
[7723]1514    @action(_('Save'), style='primary')
[7144]1515    def save(self, **data):
1516        form = self.request.form
1517        password = form.get('change_password', None)
1518        password_ctl = form.get('change_password_repeat', None)
1519        if password:
[7147]1520            validator = getUtility(IPasswordValidator)
1521            errors = validator.validate_password(password, password_ctl)
1522            if not errors:
1523                IUserAccount(self.context).setPassword(password)
1524                write_log_message(self, 'saved: password')
[7723]1525                self.flash(_('Password changed.'))
[6756]1526            else:
[7147]1527                self.flash( ' '.join(errors))
[6756]1528        return
1529
[7819]1530class StudentFilesUploadPage(KofaPage):
[7114]1531    """ View to upload files by student
1532    """
1533    grok.context(IStudent)
1534    grok.name('change_portrait')
[7127]1535    grok.require('waeup.uploadStudentFile')
[7114]1536    grok.template('filesuploadpage')
[7723]1537    label = _('Upload portrait')
[7114]1538    pnav = 4
1539
[7133]1540    def update(self):
[7539]1541        if self.context.getStudent().state != ADMITTED:
[7145]1542            emit_lock_message(self)
[7133]1543            return
1544        super(StudentFilesUploadPage, self).update()
1545        return
1546
[7819]1547class StartClearancePage(KofaPage):
[6770]1548    grok.context(IStudent)
1549    grok.name('start_clearance')
1550    grok.require('waeup.handleStudent')
1551    grok.template('enterpin')
[7723]1552    label = _('Start clearance')
[6770]1553    ac_prefix = 'CLR'
1554    notice = ''
1555    pnav = 4
[7723]1556    buttonname = _('Start clearance now')
[6770]1557
[7133]1558    @property
1559    def all_required_fields_filled(self):
1560        if self.context.email and self.context.phone:
1561            return True
1562        return False
1563
1564    @property
1565    def portrait_uploaded(self):
1566        store = getUtility(IExtFileStore)
1567        if store.getFileByContext(self.context, attr=u'passport.jpg'):
1568            return True
1569        return False
1570
[6770]1571    def update(self, SUBMIT=None):
[7671]1572        if not self.context.state == ADMITTED:
[7745]1573            self.flash(_("Wrong state"))
[6936]1574            self.redirect(self.url(self.context))
1575            return
[7133]1576        if not self.portrait_uploaded:
[7723]1577            self.flash(_("No portrait uploaded."))
[7133]1578            self.redirect(self.url(self.context, 'change_portrait'))
1579            return
1580        if not self.all_required_fields_filled:
[7723]1581            self.flash(_("Not all required fields filled."))
[7133]1582            self.redirect(self.url(self.context, 'edit_base'))
1583            return
[6770]1584        self.ac_series = self.request.form.get('ac_series', None)
1585        self.ac_number = self.request.form.get('ac_number', None)
1586
1587        if SUBMIT is None:
1588            return
1589        pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number)
1590        code = get_access_code(pin)
1591        if not code:
[7723]1592            self.flash(_('Activation code is invalid.'))
[6770]1593            return
1594        # Mark pin as used (this also fires a pin related transition)
1595        # and fire transition start_clearance
1596        if code.state == USED:
[7723]1597            self.flash(_('Activation code has already been used.'))
[6770]1598            return
1599        else:
[7723]1600            comment = _(u"invalidated")
[6770]1601            # Here we know that the ac is in state initialized so we do not
[6927]1602            # expect an exception, but the owner might be different
1603            if not invalidate_accesscode(pin,comment,self.context.student_id):
[7723]1604                self.flash(_('You are not the owner of this access code.'))
[6927]1605                return
[6770]1606            self.context.clr_code = pin
1607        IWorkflowInfo(self.context).fireTransition('start_clearance')
[7723]1608        self.flash(_('Clearance process has been started.'))
[6770]1609        self.redirect(self.url(self.context,'cedit'))
1610        return
1611
[6695]1612class StudentClearanceEditFormPage(StudentClearanceManageFormPage):
1613    """ View to edit student clearance data by student
1614    """
1615    grok.context(IStudent)
1616    grok.name('cedit')
1617    grok.require('waeup.handleStudent')
[7723]1618    label = _('Edit clearance data')
[6718]1619
[7993]1620    @property
1621    def form_fields(self):
1622        cm = getattr(self.context,'current_mode', None)
1623        if cm is not None and cm.startswith('pg'):
1624            form_fields = grok.AutoFields(IPGStudentClearance).omit('clearance_locked')
1625        else:
1626            form_fields = grok.AutoFields(IUGStudentClearance).omit('clearance_locked')
1627        return form_fields
1628
[6718]1629    def update(self):
1630        if self.context.clearance_locked:
[7145]1631            emit_lock_message(self)
[6718]1632            return
1633        return super(StudentClearanceEditFormPage, self).update()
[6719]1634
[7723]1635    @action(_('Save'), style='primary')
[6722]1636    def save(self, **data):
1637        self.applyData(self.context, **data)
[7723]1638        self.flash(_('Clearance form has been saved.'))
[6722]1639        return
1640
[7253]1641    def dataNotComplete(self):
[7642]1642        """To be implemented in the customization package.
1643        """
[7253]1644        return False
1645
[7723]1646    @action(_('Save and request clearance'), style='primary')
[7186]1647    def requestClearance(self, **data):
[6722]1648        self.applyData(self.context, **data)
[7253]1649        if self.dataNotComplete():
1650            self.flash(self.dataNotComplete())
1651            return
[7723]1652        self.flash(_('Clearance form has been saved.'))
[6769]1653        self.redirect(self.url(self.context,'request_clearance'))
[6722]1654        return
1655
[7819]1656class RequestClearancePage(KofaPage):
[6769]1657    grok.context(IStudent)
1658    grok.name('request_clearance')
1659    grok.require('waeup.handleStudent')
1660    grok.template('enterpin')
[7723]1661    label = _('Request clearance')
1662    notice = _('Enter the CLR access code used for starting clearance.')
[6769]1663    ac_prefix = 'CLR'
1664    pnav = 4
[7723]1665    buttonname = _('Request clearance now')
[6769]1666
1667    def update(self, SUBMIT=None):
1668        self.ac_series = self.request.form.get('ac_series', None)
1669        self.ac_number = self.request.form.get('ac_number', None)
1670        if SUBMIT is None:
1671            return
1672        pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number)
1673        if self.context.clr_code != pin:
[7723]1674            self.flash(_("This isn't your CLR access code."))
[6769]1675            return
1676        state = IWorkflowState(self.context).getState()
1677        # This shouldn't happen, but the application officer
1678        # might have forgotten to lock the form after changing the state
1679        if state != CLEARANCE:
[7723]1680            self.flash(_('This form cannot be submitted. Wrong state!'))
[6769]1681            return
1682        IWorkflowInfo(self.context).fireTransition('request_clearance')
[7723]1683        self.flash(_('Clearance has been requested.'))
[6769]1684        self.redirect(self.url(self.context))
[6789]1685        return
[6806]1686
[7819]1687class StartCourseRegistrationPage(KofaPage):
[6944]1688    grok.context(IStudentStudyCourse)
1689    grok.name('start_course_registration')
1690    grok.require('waeup.handleStudent')
1691    grok.template('enterpin')
[7723]1692    label = _('Start course registration')
[6944]1693    ac_prefix = 'SFE'
1694    notice = ''
1695    pnav = 4
[7723]1696    buttonname = _('Start course registration now')
[6944]1697
1698    def update(self, SUBMIT=None):
1699        if not self.context.getStudent().state in (CLEARED,RETURNING):
[8323]1700            self.flash(_("You are in wrong state."))
[6944]1701            self.redirect(self.url(self.context))
1702            return
[8323]1703        if not self.context.may_register:
1704            self.flash(_("You are not entitled to start course registration."))
1705            self.redirect(self.url(self.context))
1706            return
[6944]1707        self.ac_series = self.request.form.get('ac_series', None)
1708        self.ac_number = self.request.form.get('ac_number', None)
1709
1710        if SUBMIT is None:
1711            return
1712        pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number)
1713        code = get_access_code(pin)
1714        if not code:
[7723]1715            self.flash(_('Activation code is invalid.'))
[6944]1716            return
1717        # Mark pin as used (this also fires a pin related transition)
1718        # and fire transition start_clearance
1719        if code.state == USED:
[7723]1720            self.flash(_('Activation code has already been used.'))
[6944]1721            return
1722        else:
[7723]1723            comment = _(u"invalidated")
[6944]1724            # Here we know that the ac is in state initialized so we do not
1725            # expect an exception, but the owner might be different
1726            if not invalidate_accesscode(
1727                pin,comment,self.context.getStudent().student_id):
[7723]1728                self.flash(_('You are not the owner of this access code.'))
[6944]1729                return
1730        if self.context.getStudent().state == CLEARED:
1731            IWorkflowInfo(self.context.getStudent()).fireTransition(
1732                'pay_first_school_fee')
1733        elif self.context.getStudent().state == RETURNING:
1734            IWorkflowInfo(self.context.getStudent()).fireTransition(
1735                'pay_school_fee')
[7723]1736        self.flash(_('Course registration has been started.'))
[6944]1737        self.redirect(self.url(self.context))
1738        return
1739
[7819]1740class AddStudyLevelFormPage(KofaEditFormPage):
[6806]1741    """ Page for students to add current study levels
1742    """
1743    grok.context(IStudentStudyCourse)
1744    grok.name('add')
1745    grok.require('waeup.handleStudent')
1746    grok.template('studyleveladdpage')
1747    form_fields = grok.AutoFields(IStudentStudyCourse)
1748    pnav = 4
1749
1750    @property
1751    def label(self):
1752        studylevelsource = StudyLevelSource().factory
1753        code = self.context.current_level
1754        title = studylevelsource.getTitle(self.context, code)
[7723]1755        return _('Add current level ${a}', mapping = {'a':title})
[6806]1756
1757    def update(self):
[7539]1758        if self.context.getStudent().state != PAID:
[7145]1759            emit_lock_message(self)
[6806]1760            return
1761        super(AddStudyLevelFormPage, self).update()
1762        return
1763
[7723]1764    @action(_('Create course list now'), style='primary')
[6806]1765    def addStudyLevel(self, **data):
[8323]1766        studylevel = createObject(u'waeup.StudentStudyLevel')
[6806]1767        studylevel.level = self.context.current_level
1768        studylevel.level_session = self.context.current_session
1769        try:
1770            self.context.addStudentStudyLevel(
1771                self.context.certificate,studylevel)
1772        except KeyError:
[7723]1773            self.flash(_('This level exists.'))
[6806]1774        self.redirect(self.url(self.context))
1775        return
[6808]1776
[7819]1777class StudyLevelEditFormPage(KofaEditFormPage):
[6808]1778    """ Page to edit the student study level data by students
1779    """
1780    grok.context(IStudentStudyLevel)
1781    grok.name('edit')
1782    grok.require('waeup.handleStudent')
1783    grok.template('studyleveleditpage')
1784    form_fields = grok.AutoFields(IStudentStudyLevel).omit(
1785        'level_session', 'level_verdict')
1786    pnav = 4
1787
1788    def update(self):
[7539]1789        if self.context.getStudent().state != PAID:
1790            emit_lock_message(self)
1791            return
[6808]1792        super(StudyLevelEditFormPage, self).update()
1793        datatable.need()
[7329]1794        warning.need()
[6808]1795        return
1796
1797    @property
1798    def label(self):
[7833]1799        # Here we know that the cookie has been set
1800        lang = self.request.cookies.get('kofa.language')
[7811]1801        level_title = translate(self.context.level_title, 'waeup.kofa',
[7723]1802            target_language=lang)
1803        return _('Add and remove course tickets of study level ${a}',
1804            mapping = {'a':level_title})
[6808]1805
1806    @property
1807    def total_credits(self):
1808        total_credits = 0
1809        for key, val in self.context.items():
1810            total_credits += val.credits
1811        return total_credits
1812
[7723]1813    @action(_('Add course ticket'))
[6808]1814    def addCourseTicket(self, **data):
1815        self.redirect(self.url(self.context, 'ctadd'))
1816
[7723]1817    @jsaction(_('Remove selected tickets'))
[6808]1818    def delCourseTicket(self, **data):
1819        form = self.request.form
1820        if form.has_key('val_id'):
1821            child_id = form['val_id']
1822        else:
[7723]1823            self.flash(_('No ticket selected.'))
[6808]1824            self.redirect(self.url(self.context, '@@edit'))
1825            return
1826        if not isinstance(child_id, list):
1827            child_id = [child_id]
1828        deleted = []
1829        for id in child_id:
[6940]1830            # Students are not allowed to remove core tickets
[7665]1831            if not self.context[id].mandatory:
[7723]1832                del self.context[id]
1833                deleted.append(id)
[6808]1834        if len(deleted):
[7723]1835            self.flash(_('Successfully removed: ${a}',
1836                mapping = {'a':', '.join(deleted)}))
[6808]1837        self.redirect(self.url(self.context, u'@@edit'))
1838        return
1839
[7723]1840    @action(_('Register course list'), style='primary')
[7186]1841    def RegisterCourses(self, **data):
[7642]1842        IWorkflowInfo(self.context.getStudent()).fireTransition(
1843            'register_courses')
[7723]1844        self.flash(_('Course list has been registered.'))
[6810]1845        self.redirect(self.url(self.context))
1846        return
1847
[6808]1848class CourseTicketAddFormPage2(CourseTicketAddFormPage):
1849    """Add a course ticket by student.
1850    """
1851    grok.name('ctadd')
1852    grok.require('waeup.handleStudent')
1853    form_fields = grok.AutoFields(ICourseTicketAdd).omit(
[8141]1854        'score', 'mandatory', 'automatic', 'carry_over')
[6808]1855
[7539]1856    def update(self):
1857        if self.context.getStudent().state != PAID:
1858            emit_lock_message(self)
1859            return
1860        super(CourseTicketAddFormPage2, self).update()
1861        return
1862
[7723]1863    @action(_('Add course ticket'))
[6808]1864    def addCourseTicket(self, **data):
[7642]1865        # Safety belt
[7539]1866        if self.context.getStudent().state != PAID:
1867            return
[8325]1868        ticket = createObject(u'waeup.CourseTicket')
[6808]1869        course = data['course']
[7642]1870        for name in ['code', 'title', 'credits', 'passmark', 'semester']:
1871            setattr(ticket, name, getattr(course, name))
[6808]1872        ticket.automatic = False
1873        try:
1874            self.context.addCourseTicket(ticket)
1875        except KeyError:
[7723]1876            self.flash(_('The ticket exists.'))
[6808]1877            return
[7723]1878        self.flash(_('Successfully added ${a}.',
1879            mapping = {'a':ticket.code}))
[6808]1880        self.redirect(self.url(self.context, u'@@edit'))
1881        return
[7369]1882
1883
[7819]1884class SetPasswordPage(KofaPage):
1885    grok.context(IKofaObject)
[7660]1886    grok.name('setpassword')
1887    grok.require('waeup.Anonymous')
1888    grok.template('setpassword')
[7723]1889    label = _('Set password for first-time login')
[7660]1890    ac_prefix = 'PWD'
1891    pnav = 0
[7738]1892    set_button = _('Set')
[7660]1893
1894    def update(self, SUBMIT=None):
1895        self.reg_number = self.request.form.get('reg_number', None)
1896        self.ac_series = self.request.form.get('ac_series', None)
1897        self.ac_number = self.request.form.get('ac_number', None)
1898
1899        if SUBMIT is None:
1900            return
1901        hitlist = search(query=self.reg_number,
1902            searchtype='reg_number', view=self)
1903        if not hitlist:
[7723]1904            self.flash(_('No student found.'))
[7660]1905            return
1906        if len(hitlist) != 1:   # Cannot happen but anyway
[7723]1907            self.flash(_('More than one student found.'))
[7660]1908            return
1909        student = hitlist[0].context
1910        self.student_id = student.student_id
1911        student_pw = student.password
1912        pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number)
1913        code = get_access_code(pin)
1914        if not code:
[7723]1915            self.flash(_('Access code is invalid.'))
[7660]1916            return
1917        if student_pw and pin == student.adm_code:
[7723]1918            self.flash(_(
1919                'Password has already been set. Your Student Id is ${a}',
1920                mapping = {'a':self.student_id}))
[7660]1921            return
1922        elif student_pw:
1923            self.flash(
[7723]1924                _('Password has already been set. You are using the ' +
1925                'wrong Access Code.'))
[7660]1926            return
1927        # Mark pin as used (this also fires a pin related transition)
1928        # and set student password
1929        if code.state == USED:
[7723]1930            self.flash(_('Access code has already been used.'))
[7660]1931            return
1932        else:
[7723]1933            comment = _(u"invalidated")
[7660]1934            # Here we know that the ac is in state initialized so we do not
1935            # expect an exception
1936            invalidate_accesscode(pin,comment)
1937            IUserAccount(student).setPassword(self.ac_number)
1938            student.adm_code = pin
[7723]1939        self.flash(_('Password has been set. Your Student Id is ${a}',
1940            mapping = {'a':self.student_id}))
[7811]1941        return
Note: See TracBrowser for help on using the repository browser.