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

Last change on this file since 9910 was 9906, checked in by Henrik Bettermann, 12 years ago

Split course result table. Reformat tables.

  • Property svn:keywords set to Id
File size: 105.5 KB
RevLine 
[7191]1## $Id: browser.py 9906 2013-01-23 09:27:25Z 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
[7256]23from datetime import datetime
[7015]24from zope.event import notify
[7723]25from zope.i18n import translate
[6996]26from zope.catalog.interfaces import ICatalog
[7386]27from zope.component import queryUtility, getUtility, createObject
[9467]28from zope.schema.interfaces import ConstraintNotSatisfied, RequiredMissing
[7386]29from zope.formlib.textwidgets import BytesDisplayWidget
[6621]30from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState
[7811]31from waeup.kofa.accesscodes import (
[8420]32    invalidate_accesscode, get_access_code)
[7811]33from waeup.kofa.accesscodes.workflow import USED
[9217]34from waeup.kofa.browser.layout import (
[7819]35    KofaPage, KofaEditFormPage, KofaAddFormPage, KofaDisplayFormPage,
[9217]36    KofaForm, NullValidator)
[9797]37from waeup.kofa.browser.breadcrumbs import Breadcrumb
[9822]38from waeup.kofa.browser.pages import ContactAdminForm, ExportCSVView, doll_up
[9797]39from waeup.kofa.browser.resources import (
40    datepicker, datatable, tabs, warning, toggleall)
[7811]41from waeup.kofa.browser.layout import jsaction, action, UtilityView
[8779]42from waeup.kofa.browser.interfaces import ICaptchaManager
[9797]43from waeup.kofa.hostels.hostel import NOT_OCCUPIED
[7811]44from waeup.kofa.interfaces import (
[7819]45    IKofaObject, IUserAccount, IExtFileStore, IPasswordValidator, IContactForm,
[9797]46    IKofaUtils, IUniversity, IObjectHistory, academic_sessions, ICSVExporter,
[9833]47    academic_sessions_vocab, IJobManager, IDataCenter)
[7811]48from waeup.kofa.interfaces import MessageFactory as _
[8170]49from waeup.kofa.widgets.datewidget import (
50    FriendlyDateWidget, FriendlyDateDisplayWidget,
51    FriendlyDatetimeDisplayWidget)
[9797]52from waeup.kofa.mandates.mandate import PasswordMandate
[9806]53from waeup.kofa.university.interfaces import (
54    IDepartment, ICertificate, ICourse)
[9797]55from waeup.kofa.university.department import (
56    VirtualDepartmentExportJobContainer,)
[9842]57from waeup.kofa.university.certificate import (
58    VirtualCertificateExportJobContainer,)
[9843]59from waeup.kofa.university.course import (
60    VirtualCourseExportJobContainer,)
[9797]61from waeup.kofa.university.vocabularies import course_levels
[9813]62from waeup.kofa.utils.batching import VirtualExportJobContainer
[9797]63from waeup.kofa.utils.helpers import get_current_principal, to_timezone
[7868]64from waeup.kofa.widgets.restwidget import ReSTDisplayWidget
[7811]65from waeup.kofa.students.interfaces import (
[7993]66    IStudentsContainer, IStudent,
67    IUGStudentClearance,IPGStudentClearance,
[9563]68    IStudentPersonal, IStudentPersonalEdit, IStudentBase, IStudentStudyCourse,
[9138]69    IStudentStudyCourseTransfer,
[7538]70    IStudentAccommodation, IStudentStudyLevel,
[6877]71    ICourseTicket, ICourseTicketAdd, IStudentPaymentsContainer,
[9864]72    IStudentOnlinePayment, IStudentPreviousPayment, IStudentBalancePayment,
[9148]73    IBedTicket, IStudentsUtils, IStudentRequestPW
[6621]74    )
[9806]75from waeup.kofa.students.catalog import search, StudentQueryResultItem
[9804]76from waeup.kofa.students.export import EXPORTER_NAMES
[9797]77from waeup.kofa.students.studylevel import StudentStudyLevel, CourseTicket
78from waeup.kofa.students.vocabularies import StudyLevelSource
[8779]79from waeup.kofa.students.workflow import (CREATED, ADMITTED, PAID,
[9028]80    CLEARANCE, REQUESTED, RETURNING, CLEARED, REGISTERED, VALIDATED,
81    FORBIDDEN_POSTGRAD_TRANS)
[6621]82
[9797]83
[8779]84grok.context(IKofaObject) # Make IKofaObject the default context
85
[8737]86# Save function used for save methods in pages
87def msave(view, **data):
88    changed_fields = view.applyData(view.context, **data)
89    # Turn list of lists into single list
90    if changed_fields:
91        changed_fields = reduce(lambda x,y: x+y, changed_fields.values())
92    # Inform catalog if certificate has changed
93    # (applyData does this only for the context)
94    if 'certificate' in changed_fields:
95        notify(grok.ObjectModifiedEvent(view.context.student))
96    fields_string = ' + '.join(changed_fields)
97    view.flash(_('Form has been saved.'))
98    if fields_string:
99        view.context.writeLogMessage(view, 'saved: %s' % fields_string)
100    return
101
[7145]102def emit_lock_message(view):
[7642]103    """Flash a lock message.
104    """
[7723]105    view.flash(_('The requested form is locked (read-only).'))
[7133]106    view.redirect(view.url(view.context))
107    return
108
[8921]109def translated_values(view):
[9685]110    """Translate course ticket attribute values to be displayed on
111    studylevel pages.
112    """
[8921]113    lang = view.request.cookies.get('kofa.language')
114    for value in view.context.values():
[9328]115        # We have to unghostify (according to Tres Seaver) the __dict__
116        # by activating the object, otherwise value_dict will be empty
117        # when calling the first time.
[9330]118        value._p_activate()
[8921]119        value_dict = dict([i for i in value.__dict__.items()])
[9698]120        value_dict['removable_by_student'] = value.removable_by_student
[8921]121        value_dict['mandatory'] = translate(str(value.mandatory), 'zope',
122            target_language=lang)
123        value_dict['carry_over'] = translate(str(value.carry_over), 'zope',
124            target_language=lang)
125        value_dict['automatic'] = translate(str(value.automatic), 'zope',
126            target_language=lang)
[9685]127        value_dict['grade'] = value.grade
128        value_dict['weight'] = value.weight
[8921]129        yield value_dict
130
[9814]131def clearance_disabled_message(student):
132    try:
133        session_config = grok.getSite()[
134            'configuration'][str(student.current_session)]
135    except KeyError:
136        return _('Session configuration object is not available.')
137    if not session_config.clearance_enabled:
138        return _('Clearance is disabled for this session.')
139    return None
140
[9895]141
142def addCourseTicket(view, course=None):
143    students_utils = getUtility(IStudentsUtils)
144    ticket = createObject(u'waeup.CourseTicket')
145    ticket.automatic = False
146    ticket.carry_over = False
147    max_credits = students_utils.maxCreditsExceeded(view.context, course)
148    if max_credits:
149        view.flash(_(
150            'Total credits exceed ${a}.',
151            mapping = {'a': max_credits}))
152        return False
153    try:
154        view.context.addCourseTicket(ticket, course)
155    except KeyError:
156        view.flash(_('The ticket exists.'))
157        return False
158    view.flash(_('Successfully added ${a}.',
159        mapping = {'a':ticket.code}))
160    return True
161
[6629]162class StudentsBreadcrumb(Breadcrumb):
163    """A breadcrumb for the students container.
164    """
165    grok.context(IStudentsContainer)
[7723]166    title = _('Students')
[6629]167
[7459]168    @property
169    def target(self):
170        user = get_current_principal()
171        if getattr(user, 'user_type', None) == 'student':
172            return None
173        return self.viewname
174
[6818]175class StudentBreadcrumb(Breadcrumb):
176    """A breadcrumb for the student container.
177    """
178    grok.context(IStudent)
179
180    def title(self):
[7364]181        return self.context.display_fullname
[6818]182
[6635]183class SudyCourseBreadcrumb(Breadcrumb):
184    """A breadcrumb for the student study course.
185    """
186    grok.context(IStudentStudyCourse)
187
[9140]188    def title(self):
189        if self.context.is_current:
190            return _('Study Course')
191        else:
192            return _('Previous Study Course')
193
[6635]194class PaymentsBreadcrumb(Breadcrumb):
195    """A breadcrumb for the student payments folder.
196    """
[6859]197    grok.context(IStudentPaymentsContainer)
[7723]198    title = _('Payments')
[6635]199
[6870]200class OnlinePaymentBreadcrumb(Breadcrumb):
[7251]201    """A breadcrumb for payments.
[6870]202    """
[6877]203    grok.context(IStudentOnlinePayment)
[6870]204
205    @property
206    def title(self):
207        return self.context.p_id
208
[6635]209class AccommodationBreadcrumb(Breadcrumb):
210    """A breadcrumb for the student accommodation folder.
211    """
212    grok.context(IStudentAccommodation)
[7723]213    title = _('Accommodation')
[6635]214
[6994]215class BedTicketBreadcrumb(Breadcrumb):
216    """A breadcrumb for bed tickets.
217    """
218    grok.context(IBedTicket)
[7009]219
[6994]220    @property
221    def title(self):
[7723]222        return _('Bed Ticket ${a}',
223            mapping = {'a':self.context.getSessionString()})
[6994]224
[6776]225class StudyLevelBreadcrumb(Breadcrumb):
226    """A breadcrumb for course lists.
227    """
228    grok.context(IStudentStudyLevel)
229
230    @property
231    def title(self):
[7834]232        return self.context.level_title
[6776]233
[7819]234class StudentsContainerPage(KofaPage):
[6626]235    """The standard view for student containers.
[6621]236    """
237    grok.context(IStudentsContainer)
238    grok.name('index')
[7240]239    grok.require('waeup.viewStudentsContainer')
[6695]240    grok.template('containerpage')
[7723]241    label = _('Student Section')
[7735]242    search_button = _('Search')
[6642]243    pnav = 4
[6621]244
[6626]245    def update(self, *args, **kw):
246        datatable.need()
247        form = self.request.form
248        self.hitlist = []
[9795]249        if form.get('searchtype', None) == 'suspended':
250            self.searchtype = form['searchtype']
251            self.searchterm = None
252        elif 'searchterm' in form and form['searchterm']:
[6626]253            self.searchterm = form['searchterm']
254            self.searchtype = form['searchtype']
255        elif 'old_searchterm' in form:
256            self.searchterm = form['old_searchterm']
257            self.searchtype = form['old_searchtype']
258        else:
259            if 'search' in form:
[7745]260                self.flash(_('Empty search string'))
[6626]261            return
[7068]262        if self.searchtype == 'current_session':
[8081]263            try:
264                self.searchterm = int(self.searchterm)
265            except ValueError:
[8404]266                self.flash(_('Only year dates allowed (e.g. 2011).'))
[8081]267                return
[6626]268        self.hitlist = search(query=self.searchterm,
269            searchtype=self.searchtype, view=self)
270        if not self.hitlist:
[8404]271            self.flash(_('No student found.'))
[6626]272        return
273
[7819]274class StudentsContainerManagePage(KofaPage):
[6626]275    """The manage page for student containers.
[6622]276    """
277    grok.context(IStudentsContainer)
278    grok.name('manage')
[7136]279    grok.require('waeup.manageStudent')
[6695]280    grok.template('containermanagepage')
[6642]281    pnav = 4
[7723]282    label = _('Manage student section')
[7735]283    search_button = _('Search')
284    remove_button = _('Remove selected')
[6622]285
[6626]286    def update(self, *args, **kw):
287        datatable.need()
[6820]288        toggleall.need()
[7329]289        warning.need()
[6626]290        form = self.request.form
291        self.hitlist = []
[9795]292        if form.get('searchtype', None) == 'suspended':
293            self.searchtype = form['searchtype']
294            self.searchterm = None
295        elif 'searchterm' in form and form['searchterm']:
[6626]296            self.searchterm = form['searchterm']
297            self.searchtype = form['searchtype']
298        elif 'old_searchterm' in form:
299            self.searchterm = form['old_searchterm']
300            self.searchtype = form['old_searchtype']
301        else:
302            if 'search' in form:
[7745]303                self.flash(_('Empty search string'))
[6626]304            return
[8082]305        if self.searchtype == 'current_session':
306            try:
307                self.searchterm = int(self.searchterm)
308            except ValueError:
309                self.flash('Only year dates allowed (e.g. 2011).')
310                return
[6626]311        if not 'entries' in form:
312            self.hitlist = search(query=self.searchterm,
313                searchtype=self.searchtype, view=self)
314            if not self.hitlist:
[7723]315                self.flash(_('No student found.'))
[7459]316            if 'remove' in form:
[7723]317                self.flash(_('No item selected.'))
[6626]318            return
319        entries = form['entries']
320        if isinstance(entries, basestring):
321            entries = [entries]
322        deleted = []
323        for entry in entries:
324            if 'remove' in form:
325                del self.context[entry]
326                deleted.append(entry)
327        self.hitlist = search(query=self.searchterm,
328            searchtype=self.searchtype, view=self)
329        if len(deleted):
[7723]330            self.flash(_('Successfully removed: ${a}',
331                mapping = {'a':', '.join(deleted)}))
[6622]332        return
333
[7819]334class StudentAddFormPage(KofaAddFormPage):
[6622]335    """Add-form to add a student.
336    """
337    grok.context(IStudentsContainer)
[7136]338    grok.require('waeup.manageStudent')
[6622]339    grok.name('addstudent')
[7357]340    form_fields = grok.AutoFields(IStudent).select(
[7520]341        'firstname', 'middlename', 'lastname', 'reg_number')
[7723]342    label = _('Add student')
[6642]343    pnav = 4
[6622]344
[7723]345    @action(_('Create student record'), style='primary')
[6622]346    def addStudent(self, **data):
347        student = createObject(u'waeup.Student')
348        self.applyData(student, **data)
[6652]349        self.context.addStudent(student)
[7723]350        self.flash(_('Student record created.'))
[6651]351        self.redirect(self.url(self.context[student.student_id], 'index'))
[6622]352        return
353
[9338]354class LoginAsStudentStep1(KofaEditFormPage):
355    """ View to temporarily set a student password.
356    """
357    grok.context(IStudent)
358    grok.name('loginasstep1')
359    grok.require('waeup.loginAsStudent')
360    grok.template('loginasstep1')
361    pnav = 4
362
363    def label(self):
364        return _(u'Set temporary password for ${a}',
365            mapping = {'a':self.context.display_fullname})
366
367    @action('Set password now', style='primary')
368    def setPassword(self, *args, **data):
369        kofa_utils = getUtility(IKofaUtils)
370        password = kofa_utils.genPassword()
371        self.context.setTempPassword(self.request.principal.id, password)
372        self.context.writeLogMessage(
373            self, 'temp_password generated: %s' % password)
374        args = {'password':password}
375        self.redirect(self.url(self.context) +
376            '/loginasstep2?%s' % urlencode(args))
377        return
378
379class LoginAsStudentStep2(KofaPage):
380    """ View to temporarily login as student with a temporary password.
381    """
382    grok.context(IStudent)
383    grok.name('loginasstep2')
384    grok.require('waeup.Public')
385    grok.template('loginasstep2')
386    login_button = _('Login now')
387    pnav = 4
388
389    def label(self):
390        return _(u'Login as ${a}',
391            mapping = {'a':self.context.student_id})
392
393    def update(self, SUBMIT=None, password=None):
394        self.password = password
395        if SUBMIT is not None:
396            self.flash(_('You successfully logged in as student.'))
397            self.redirect(self.url(self.context))
398        return
399
[7819]400class StudentBaseDisplayFormPage(KofaDisplayFormPage):
[6631]401    """ Page to display student base data
402    """
[6622]403    grok.context(IStudent)
404    grok.name('index')
[6660]405    grok.require('waeup.viewStudent')
[6695]406    grok.template('basepage')
[9702]407    form_fields = grok.AutoFields(IStudentBase).omit(
408        'password', 'suspended', 'suspended_comment')
[6642]409    pnav = 4
[6622]410
411    @property
412    def label(self):
[8983]413        if self.context.suspended:
[9124]414            return _('${a}: Base Data (account deactivated)',
[8983]415                mapping = {'a':self.context.display_fullname})
416        return  _('${a}: Base Data',
[7723]417            mapping = {'a':self.context.display_fullname})
[6631]418
[6699]419    @property
420    def hasPassword(self):
421        if self.context.password:
[7723]422            return _('set')
423        return _('unset')
[6699]424
[9141]425class StudentBasePDFFormPage(KofaDisplayFormPage):
426    """ Page to display student base data in pdf files.
427    """
428
[9374]429    def __init__(self, context, request, omit_fields):
430        self.omit_fields = omit_fields
431        super(StudentBasePDFFormPage, self).__init__(context, request)
432
433    @property
434    def form_fields(self):
435        form_fields = grok.AutoFields(IStudentBase)
436        for field in self.omit_fields:
437            form_fields = form_fields.omit(field)
438        return form_fields
439
[7229]440class ContactStudentForm(ContactAdminForm):
441    grok.context(IStudent)
[7230]442    grok.name('contactstudent')
[7275]443    grok.require('waeup.viewStudent')
[7229]444    pnav = 4
445    form_fields = grok.AutoFields(IContactForm).select('subject', 'body')
446
[9484]447    def update(self, subject=u'', body=u''):
[9857]448        super(ContactStudentForm, self).update()
[7275]449        self.form_fields.get('subject').field.default = subject
[9484]450        self.form_fields.get('body').field.default = body
[9857]451        return
[7275]452
[7229]453    def label(self):
[7723]454        return _(u'Send message to ${a}',
455            mapping = {'a':self.context.display_fullname})
[7229]456
[7459]457    @action('Send message now', style='primary')
[7229]458    def send(self, *args, **data):
[7234]459        try:
[7403]460            email = self.request.principal.email
[7234]461        except AttributeError:
[7403]462            email = self.config.email_admin
463        usertype = getattr(self.request.principal,
464                           'user_type', 'system').title()
[7819]465        kofa_utils = getUtility(IKofaUtils)
[7811]466        success = kofa_utils.sendContactForm(
[7403]467                self.request.principal.title,email,
468                self.context.display_fullname,self.context.email,
469                self.request.principal.id,usertype,
470                self.config.name,
471                data['body'],data['subject'])
[7229]472        if success:
[7723]473            self.flash(_('Your message has been sent.'))
[7229]474        else:
[7723]475            self.flash(_('An smtp server error occurred.'))
[7229]476        return
477
[9191]478class ExportPDFAdmissionSlipPage(UtilityView, grok.View):
479    """Deliver a PDF Admission slip.
480    """
481    grok.context(IStudent)
482    grok.name('admission_slip.pdf')
483    grok.require('waeup.viewStudent')
484    prefix = 'form'
485
486    form_fields = grok.AutoFields(IStudentBase).select('student_id', 'reg_number')
487
488    @property
489    def label(self):
490        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
491        return translate(_('Admission Letter of'),
492            'waeup.kofa', target_language=portal_language) \
493            + ' %s' % self.context.display_fullname
494
495    def render(self):
496        students_utils = getUtility(IStudentsUtils)
497        return students_utils.renderPDFAdmissionLetter(self,
498            self.context.student)
499
[7819]500class StudentBaseManageFormPage(KofaEditFormPage):
[7133]501    """ View to manage student base data
[6631]502    """
503    grok.context(IStudent)
[7133]504    grok.name('manage_base')
[7136]505    grok.require('waeup.manageStudent')
[9124]506    form_fields = grok.AutoFields(IStudentBase).omit(
507        'student_id', 'adm_code', 'suspended')
[6695]508    grok.template('basemanagepage')
[7723]509    label = _('Manage base data')
[6642]510    pnav = 4
[6631]511
[6638]512    def update(self):
513        datepicker.need() # Enable jQuery datepicker in date fields.
[7134]514        tabs.need()
[7490]515        self.tab1 = self.tab2 = ''
516        qs = self.request.get('QUERY_STRING', '')
517        if not qs:
518            qs = 'tab1'
519        setattr(self, qs, 'active')
[6638]520        super(StudentBaseManageFormPage, self).update()
521        self.wf_info = IWorkflowInfo(self.context)
522        return
523
[7723]524    @action(_('Save'), style='primary')
[6638]525    def save(self, **data):
[6701]526        form = self.request.form
[6790]527        password = form.get('password', None)
528        password_ctl = form.get('control_password', None)
529        if password:
[7147]530            validator = getUtility(IPasswordValidator)
531            errors = validator.validate_password(password, password_ctl)
532            if errors:
533                self.flash( ' '.join(errors))
534                return
535        changed_fields = self.applyData(self.context, **data)
[6771]536        # Turn list of lists into single list
537        if changed_fields:
538            changed_fields = reduce(lambda x,y: x+y, changed_fields.values())
[7147]539        else:
540            changed_fields = []
541        if password:
[9273]542            # Now we know that the form has no errors and can set password
[7147]543            IUserAccount(self.context).setPassword(password)
544            changed_fields.append('password')
545        fields_string = ' + '.join(changed_fields)
[7723]546        self.flash(_('Form has been saved.'))
[6644]547        if fields_string:
[8735]548            self.context.writeLogMessage(self, 'saved: % s' % fields_string)
[6638]549        return
550
[9273]551class StudentTriggerTransitionFormPage(KofaEditFormPage):
552    """ View to manage student base data
553    """
554    grok.context(IStudent)
555    grok.name('trigtrans')
556    grok.require('waeup.triggerTransition')
557    grok.template('trigtrans')
558    label = _('Trigger registration transition')
559    pnav = 4
560
561    def getTransitions(self):
562        """Return a list of dicts of allowed transition ids and titles.
563
564        Each list entry provides keys ``name`` and ``title`` for
565        internal name and (human readable) title of a single
566        transition.
567        """
568        wf_info = IWorkflowInfo(self.context)
569        allowed_transitions = [t for t in wf_info.getManualTransitions()
570            if not t[0].startswith('pay')]
571        if self.context.is_postgrad:
572            allowed_transitions = [t for t in allowed_transitions
573                if not t[0] in FORBIDDEN_POSTGRAD_TRANS]
574        return [dict(name='', title=_('No transition'))] +[
575            dict(name=x, title=y) for x, y in allowed_transitions]
576
577    @action(_('Save'), style='primary')
578    def save(self, **data):
579        form = self.request.form
[9701]580        if 'transition' in form and form['transition']:
[9273]581            transition_id = form['transition']
582            wf_info = IWorkflowInfo(self.context)
583            wf_info.fireTransition(transition_id)
584        return
585
[9124]586class StudentActivatePage(UtilityView, grok.View):
587    """ Activate student account
588    """
589    grok.context(IStudent)
590    grok.name('activate')
591    grok.require('waeup.manageStudent')
592
593    def update(self):
594        self.context.suspended = False
595        self.context.writeLogMessage(self, 'account activated')
596        history = IObjectHistory(self.context)
597        history.addMessage('Student account activated')
598        self.flash(_('Student account has been activated.'))
599        self.redirect(self.url(self.context))
600        return
601
602    def render(self):
603        return
604
605class StudentDeactivatePage(UtilityView, grok.View):
606    """ Deactivate student account
607    """
608    grok.context(IStudent)
609    grok.name('deactivate')
610    grok.require('waeup.manageStudent')
611
612    def update(self):
613        self.context.suspended = True
614        self.context.writeLogMessage(self, 'account deactivated')
615        history = IObjectHistory(self.context)
616        history.addMessage('Student account deactivated')
617        self.flash(_('Student account has been deactivated.'))
618        self.redirect(self.url(self.context))
619        return
620
621    def render(self):
622        return
623
[7819]624class StudentClearanceDisplayFormPage(KofaDisplayFormPage):
[6631]625    """ Page to display student clearance data
626    """
627    grok.context(IStudent)
628    grok.name('view_clearance')
[6660]629    grok.require('waeup.viewStudent')
[6642]630    pnav = 4
[6631]631
632    @property
[8099]633    def separators(self):
634        return getUtility(IStudentsUtils).SEPARATORS_DICT
635
636    @property
[7993]637    def form_fields(self):
[8472]638        if self.context.is_postgrad:
[8977]639            form_fields = grok.AutoFields(
640                IPGStudentClearance).omit('clearance_locked')
[7993]641        else:
[8977]642            form_fields = grok.AutoFields(
643                IUGStudentClearance).omit('clearance_locked')
[9486]644        if not getattr(self.context, 'officer_comment'):
645            form_fields = form_fields.omit('officer_comment')
[9484]646        else:
[9486]647            form_fields['officer_comment'].custom_widget = BytesDisplayWidget
[7993]648        return form_fields
649
650    @property
[6631]651    def label(self):
[7723]652        return _('${a}: Clearance Data',
653            mapping = {'a':self.context.display_fullname})
[6631]654
[7277]655class ExportPDFClearanceSlipPage(grok.View):
656    """Deliver a PDF slip of the context.
657    """
658    grok.context(IStudent)
[9452]659    grok.name('clearance_slip.pdf')
[7277]660    grok.require('waeup.viewStudent')
661    prefix = 'form'
[9702]662    omit_fields = (
663        'password', 'suspended', 'phone',
664        'adm_code', 'suspended_comment')
[7277]665
666    @property
[7993]667    def form_fields(self):
[8472]668        if self.context.is_postgrad:
[8977]669            form_fields = grok.AutoFields(
670                IPGStudentClearance).omit('clearance_locked')
[7993]671        else:
[8977]672            form_fields = grok.AutoFields(
673                IUGStudentClearance).omit('clearance_locked')
[9486]674        if not getattr(self.context, 'officer_comment'):
675            form_fields = form_fields.omit('officer_comment')
[7993]676        return form_fields
677
678    @property
[7723]679    def title(self):
[7819]680        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
[7811]681        return translate(_('Clearance Data'), 'waeup.kofa',
[7723]682            target_language=portal_language)
683
684    @property
[7277]685    def label(self):
[7819]686        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
[9026]687        return translate(_('Clearance Slip of'),
[7811]688            'waeup.kofa', target_language=portal_language) \
[7723]689            + ' %s' % self.context.display_fullname
[7277]690
[9010]691    def _signatures(self):
[9548]692        isStudent = getattr(
693            self.request.principal, 'user_type', None) == 'student'
694        if not isStudent and self.context.state in (CLEARED, ):
[9010]695            return (_('Student Signature'), _('Clearance Officer Signature'))
696        return
697
[9555]698    def _sigsInFooter(self):
[9548]699        isStudent = getattr(
700            self.request.principal, 'user_type', None) == 'student'
701        if not isStudent and self.context.state in (CLEARED, ):
[9555]702            return (_('Date, Student Signature'),
703                    _('Date, Clearance Officer Signature'),
704                    )
[9557]705        return ()
[9548]706
[7277]707    def render(self):
[9141]708        studentview = StudentBasePDFFormPage(self.context.student,
[9375]709            self.request, self.omit_fields)
[7277]710        students_utils = getUtility(IStudentsUtils)
711        return students_utils.renderPDF(
[9452]712            self, 'clearance_slip.pdf',
[9548]713            self.context.student, studentview, signatures=self._signatures(),
[9555]714            sigs_in_footer=self._sigsInFooter())
[7277]715
[7819]716class StudentClearanceManageFormPage(KofaEditFormPage):
[8120]717    """ Page to manage student clearance data
[6631]718    """
719    grok.context(IStudent)
[8119]720    grok.name('manage_clearance')
[7136]721    grok.require('waeup.manageStudent')
[7134]722    grok.template('clearanceeditpage')
[7723]723    label = _('Manage clearance data')
[6642]724    pnav = 4
[6650]725
[7993]726    @property
[8099]727    def separators(self):
728        return getUtility(IStudentsUtils).SEPARATORS_DICT
729
730    @property
[7993]731    def form_fields(self):
[8472]732        if self.context.is_postgrad:
[8977]733            form_fields = grok.AutoFields(IPGStudentClearance).omit('clr_code')
[7993]734        else:
[8977]735            form_fields = grok.AutoFields(IUGStudentClearance).omit('clr_code')
[7993]736        return form_fields
737
[6650]738    def update(self):
739        datepicker.need() # Enable jQuery datepicker in date fields.
[7134]740        tabs.need()
[7490]741        self.tab1 = self.tab2 = ''
742        qs = self.request.get('QUERY_STRING', '')
743        if not qs:
744            qs = 'tab1'
745        setattr(self, qs, 'active')
[6650]746        return super(StudentClearanceManageFormPage, self).update()
747
[7723]748    @action(_('Save'), style='primary')
[6695]749    def save(self, **data):
[6762]750        msave(self, **data)
[6695]751        return
752
[7459]753class StudentClearPage(UtilityView, grok.View):
[7158]754    """ Clear student by clearance officer
755    """
756    grok.context(IStudent)
757    grok.name('clear')
758    grok.require('waeup.clearStudent')
759
760    def update(self):
[9814]761        if clearance_disabled_message(self.context):
762            self.flash(clearance_disabled_message(self.context))
763            self.redirect(self.url(self.context,'view_clearance'))
764            return
[7158]765        if self.context.state == REQUESTED:
766            IWorkflowInfo(self.context).fireTransition('clear')
[7723]767            self.flash(_('Student has been cleared.'))
[7158]768        else:
[7723]769            self.flash(_('Student is in wrong state.'))
[7158]770        self.redirect(self.url(self.context,'view_clearance'))
771        return
772
773    def render(self):
774        return
775
[9484]776class StudentRejectClearancePage(KofaEditFormPage):
[7158]777    """ Reject clearance by clearance officers
778    """
779    grok.context(IStudent)
780    grok.name('reject_clearance')
[9484]781    label = _('Reject clearance')
[7158]782    grok.require('waeup.clearStudent')
[9484]783    form_fields = grok.AutoFields(
[9486]784        IUGStudentClearance).select('officer_comment')
[7158]785
[9814]786    def update(self):
787        if clearance_disabled_message(self.context):
788            self.flash(clearance_disabled_message(self.context))
789            self.redirect(self.url(self.context,'view_clearance'))
790            return
791        return super(StudentRejectClearancePage, self).update()
792
[9484]793    @action(_('Save comment and reject clearance now'), style='primary')
794    def reject(self, **data):
[7158]795        if self.context.state == CLEARED:
796            IWorkflowInfo(self.context).fireTransition('reset4')
[7723]797            message = _('Clearance has been annulled.')
[7275]798            self.flash(message)
[7158]799        elif self.context.state == REQUESTED:
800            IWorkflowInfo(self.context).fireTransition('reset3')
[7723]801            message = _('Clearance request has been rejected.')
[7275]802            self.flash(message)
[7158]803        else:
[7723]804            self.flash(_('Student is in wrong state.'))
[7334]805            self.redirect(self.url(self.context,'view_clearance'))
[7275]806            return
[9484]807        self.applyData(self.context, **data)
[9486]808        comment = data['officer_comment']
[9556]809        if comment:
810            self.context.writeLogMessage(
811                self, 'comment: %s' % comment.replace('\n', '<br>'))
812            args = {'subject':message, 'body':comment}
813        else:
814            args = {'subject':message,}
[7275]815        self.redirect(self.url(self.context) +
816            '/contactstudent?%s' % urlencode(args))
[7158]817        return
818
819
[7819]820class StudentPersonalDisplayFormPage(KofaDisplayFormPage):
[6631]821    """ Page to display student personal data
822    """
823    grok.context(IStudent)
824    grok.name('view_personal')
[6660]825    grok.require('waeup.viewStudent')
[6631]826    form_fields = grok.AutoFields(IStudentPersonal)
[7386]827    form_fields['perm_address'].custom_widget = BytesDisplayWidget
[9543]828    form_fields[
829        'personal_updated'].custom_widget = FriendlyDatetimeDisplayWidget('le')
[6642]830    pnav = 4
[6631]831
832    @property
833    def label(self):
[7723]834        return _('${a}: Personal Data',
835            mapping = {'a':self.context.display_fullname})
[6631]836
[8903]837class StudentPersonalManageFormPage(KofaEditFormPage):
838    """ Page to manage personal data
[6631]839    """
840    grok.context(IStudent)
[8903]841    grok.name('manage_personal')
842    grok.require('waeup.manageStudent')
[9553]843    form_fields = grok.AutoFields(IStudentPersonal)
844    form_fields['personal_updated'].for_display = True
[9571]845    form_fields[
846        'personal_updated'].custom_widget = FriendlyDatetimeDisplayWidget('le')
[8903]847    label = _('Manage personal data')
[6642]848    pnav = 4
[6631]849
[7723]850    @action(_('Save'), style='primary')
[6762]851    def save(self, **data):
852        msave(self, **data)
853        return
854
[9543]855class StudentPersonalEditFormPage(KofaEditFormPage):
[8903]856    """ Page to edit personal data
857    """
[9543]858    grok.context(IStudent)
[8903]859    grok.name('edit_personal')
860    grok.require('waeup.handleStudent')
[9563]861    form_fields = grok.AutoFields(IStudentPersonalEdit).omit('personal_updated')
[8903]862    label = _('Edit personal data')
863    pnav = 4
864
[9543]865    @action(_('Save/Confirm'), style='primary')
866    def save(self, **data):
867        msave(self, **data)
[9569]868        self.context.personal_updated = datetime.utcnow()
[9543]869        return
870
[7819]871class StudyCourseDisplayFormPage(KofaDisplayFormPage):
[6635]872    """ Page to display the student study course data
873    """
874    grok.context(IStudentStudyCourse)
875    grok.name('index')
[6660]876    grok.require('waeup.viewStudent')
[6775]877    grok.template('studycoursepage')
[6642]878    pnav = 4
[6635]879
880    @property
[8972]881    def form_fields(self):
882        if self.context.is_postgrad:
883            form_fields = grok.AutoFields(IStudentStudyCourse).omit(
[9723]884                'previous_verdict')
[8972]885        else:
886            form_fields = grok.AutoFields(IStudentStudyCourse)
887        return form_fields
888
889    @property
[6635]890    def label(self):
[9140]891        if self.context.is_current:
892            return _('${a}: Study Course',
893                mapping = {'a':self.context.__parent__.display_fullname})
894        else:
895            return _('${a}: Previous Study Course',
896                mapping = {'a':self.context.__parent__.display_fullname})
[6635]897
[6912]898    @property
899    def current_mode(self):
[7641]900        if self.context.certificate is not None:
[7841]901            studymodes_dict = getUtility(IKofaUtils).STUDY_MODES_DICT
[7681]902            return studymodes_dict[self.context.certificate.study_mode]
[7171]903        return
[7642]904
[7171]905    @property
906    def department(self):
[7205]907        if self.context.certificate is not None:
[7171]908            return self.context.certificate.__parent__.__parent__
909        return
[6912]910
[7171]911    @property
912    def faculty(self):
[7205]913        if self.context.certificate is not None:
[7171]914            return self.context.certificate.__parent__.__parent__.__parent__
915        return
916
[9140]917    @property
918    def prev_studycourses(self):
919        if self.context.is_current:
920            if self.context.__parent__.get('studycourse_2', None) is not None:
921                return (
922                        {'href':self.url(self.context.student) + '/studycourse_1',
923                        'title':_('First Study Course, ')},
924                        {'href':self.url(self.context.student) + '/studycourse_2',
925                        'title':_('Second Study Course')}
926                        )
927            if self.context.__parent__.get('studycourse_1', None) is not None:
928                return (
929                        {'href':self.url(self.context.student) + '/studycourse_1',
930                        'title':_('First Study Course')},
931                        )
932        return
933
[7819]934class StudyCourseManageFormPage(KofaEditFormPage):
[6649]935    """ Page to edit the student study course data
936    """
937    grok.context(IStudentStudyCourse)
[6775]938    grok.name('manage')
[7136]939    grok.require('waeup.manageStudent')
[6775]940    grok.template('studycoursemanagepage')
[7723]941    label = _('Manage study course')
[6649]942    pnav = 4
[7723]943    taboneactions = [_('Save'),_('Cancel')]
944    tabtwoactions = [_('Remove selected levels'),_('Cancel')]
945    tabthreeactions = [_('Add study level')]
[6649]946
[8972]947    @property
948    def form_fields(self):
949        if self.context.is_postgrad:
950            form_fields = grok.AutoFields(IStudentStudyCourse).omit(
[9723]951                'previous_verdict')
[8972]952        else:
953            form_fields = grok.AutoFields(IStudentStudyCourse)
954        return form_fields
955
[6775]956    def update(self):
[9139]957        if not self.context.is_current:
958            emit_lock_message(self)
959            return
[6775]960        super(StudyCourseManageFormPage, self).update()
961        tabs.need()
[7484]962        self.tab1 = self.tab2 = ''
963        qs = self.request.get('QUERY_STRING', '')
964        if not qs:
965            qs = 'tab1'
966        setattr(self, qs, 'active')
[7490]967        warning.need()
968        datatable.need()
969        return
[6775]970
[7723]971    @action(_('Save'), style='primary')
[6761]972    def save(self, **data):
[8099]973        try:
974            msave(self, **data)
975        except ConstraintNotSatisfied:
976            # The selected level might not exist in certificate
977            self.flash(_('Current level not available for certificate.'))
978            return
[8081]979        notify(grok.ObjectModifiedEvent(self.context.__parent__))
[6761]980        return
981
[6775]982    @property
983    def level_dict(self):
984        studylevelsource = StudyLevelSource().factory
985        for code in studylevelsource.getValues(self.context):
986            title = studylevelsource.getTitle(self.context, code)
987            yield(dict(code=code, title=title))
988
[9437]989    @property
990    def session_dict(self):
991        yield(dict(code='', title='--'))
992        for item in academic_sessions():
993            code = item[1]
994            title = item[0]
995            yield(dict(code=code, title=title))
996
[7723]997    @action(_('Add study level'))
[6774]998    def addStudyLevel(self, **data):
[6775]999        level_code = self.request.form.get('addlevel', None)
[9437]1000        level_session = self.request.form.get('level_session', None)
1001        if not level_session:
1002            self.flash(_('You must select a session for the level.'))
1003            self.redirect(self.url(self.context, u'@@manage')+'?tab2')
1004            return
[8323]1005        studylevel = createObject(u'waeup.StudentStudyLevel')
[6775]1006        studylevel.level = int(level_code)
[9437]1007        studylevel.level_session = int(level_session)
[6775]1008        try:
[6782]1009            self.context.addStudentStudyLevel(
1010                self.context.certificate,studylevel)
[7723]1011            self.flash(_('Study level has been added.'))
[6775]1012        except KeyError:
[7723]1013            self.flash(_('This level exists.'))
[7484]1014        self.redirect(self.url(self.context, u'@@manage')+'?tab2')
[6774]1015        return
1016
[7723]1017    @jsaction(_('Remove selected levels'))
[6775]1018    def delStudyLevels(self, **data):
1019        form = self.request.form
[9701]1020        if 'val_id' in form:
[6775]1021            child_id = form['val_id']
1022        else:
[7723]1023            self.flash(_('No study level selected.'))
[7484]1024            self.redirect(self.url(self.context, '@@manage')+'?tab2')
[6775]1025            return
1026        if not isinstance(child_id, list):
1027            child_id = [child_id]
1028        deleted = []
1029        for id in child_id:
[7723]1030            del self.context[id]
1031            deleted.append(id)
[6775]1032        if len(deleted):
[7723]1033            self.flash(_('Successfully removed: ${a}',
1034                mapping = {'a':', '.join(deleted)}))
[9332]1035            self.context.writeLogMessage(
1036                self,'removed: %s' % ', '.join(deleted))
[7484]1037        self.redirect(self.url(self.context, u'@@manage')+'?tab2')
[6775]1038        return
[6774]1039
[9138]1040class StudentTransferFormPage(KofaAddFormPage):
1041    """Page to transfer the student.
1042    """
1043    grok.context(IStudent)
1044    grok.name('transfer')
1045    grok.require('waeup.manageStudent')
1046    label = _('Transfer student')
1047    form_fields = grok.AutoFields(IStudentStudyCourseTransfer).omit(
1048        'entry_mode', 'entry_session')
1049    pnav = 4
1050
1051    def update(self):
1052        super(StudentTransferFormPage, self).update()
1053        warning.need()
1054        return
1055
1056    @jsaction(_('Transfer'))
1057    def transferStudent(self, **data):
1058        error = self.context.transfer(**data)
1059        if error == -1:
1060            self.flash(_('Current level does not match certificate levels.'))
1061        elif error == -2:
1062            self.flash(_('Former study course record incomplete.'))
1063        elif error == -3:
1064            self.flash(_('Maximum number of transfers exceeded.'))
1065        else:
1066            self.flash(_('Successfully transferred.'))
1067        return
1068
[7819]1069class StudyLevelDisplayFormPage(KofaDisplayFormPage):
[6774]1070    """ Page to display student study levels
1071    """
1072    grok.context(IStudentStudyLevel)
1073    grok.name('index')
1074    grok.require('waeup.viewStudent')
[6775]1075    form_fields = grok.AutoFields(IStudentStudyLevel)
[9161]1076    form_fields[
1077        'validation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
[6783]1078    grok.template('studylevelpage')
[6774]1079    pnav = 4
1080
[7310]1081    def update(self):
1082        super(StudyLevelDisplayFormPage, self).update()
1083        datatable.need()
1084        return
1085
[6774]1086    @property
[8141]1087    def translated_values(self):
[8921]1088        return translated_values(self)
[8141]1089
1090    @property
[6774]1091    def label(self):
[7833]1092        # Here we know that the cookie has been set
1093        lang = self.request.cookies.get('kofa.language')
[7811]1094        level_title = translate(self.context.level_title, 'waeup.kofa',
[7723]1095            target_language=lang)
1096        return _('${a}: Study Level ${b}', mapping = {
[8736]1097            'a':self.context.student.display_fullname,
[7723]1098            'b':level_title})
[6774]1099
[7459]1100class ExportPDFCourseRegistrationSlipPage(UtilityView, grok.View):
[7028]1101    """Deliver a PDF slip of the context.
1102    """
1103    grok.context(IStudentStudyLevel)
[9452]1104    grok.name('course_registration_slip.pdf')
[7028]1105    grok.require('waeup.viewStudent')
1106    form_fields = grok.AutoFields(IStudentStudyLevel)
[9683]1107    form_fields[
1108        'validation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
[7028]1109    prefix = 'form'
[9702]1110    omit_fields = (
1111        'password', 'suspended', 'phone',
1112        'adm_code', 'sex', 'suspended_comment')
[7028]1113
1114    @property
[7723]1115    def title(self):
[7819]1116        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
[7811]1117        return translate(_('Level Data'), 'waeup.kofa',
[7723]1118            target_language=portal_language)
1119
1120    @property
[9906]1121    def content_title_1(self):
[7819]1122        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
[9906]1123        return translate(_('1st Semester Courses'), 'waeup.kofa',
[7723]1124            target_language=portal_language)
1125
1126    @property
[9906]1127    def content_title_2(self):
1128        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
1129        return translate(_('2nd Semester Courses'), 'waeup.kofa',
1130            target_language=portal_language)
1131
1132    @property
[7028]1133    def label(self):
[7819]1134        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
[7811]1135        lang = self.request.cookies.get('kofa.language', portal_language)
1136        level_title = translate(self.context.level_title, 'waeup.kofa',
[7723]1137            target_language=lang)
[8141]1138        return translate(_('Course Registration Slip'),
[7811]1139            'waeup.kofa', target_language=portal_language) \
[7723]1140            + ' %s' % level_title
[7028]1141
1142    def render(self):
[7819]1143        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
[7811]1144        Sem = translate(_('Sem.'), 'waeup.kofa', target_language=portal_language)
1145        Code = translate(_('Code'), 'waeup.kofa', target_language=portal_language)
1146        Title = translate(_('Title'), 'waeup.kofa', target_language=portal_language)
1147        Dept = translate(_('Dept.'), 'waeup.kofa', target_language=portal_language)
1148        Faculty = translate(_('Faculty'), 'waeup.kofa', target_language=portal_language)
1149        Cred = translate(_('Cred.'), 'waeup.kofa', target_language=portal_language)
[9906]1150        #Mand = translate(_('Requ.'), 'waeup.kofa', target_language=portal_language)
[7811]1151        Score = translate(_('Score'), 'waeup.kofa', target_language=portal_language)
[9810]1152        Grade = translate(_('Grade'), 'waeup.kofa', target_language=portal_language)
[9141]1153        studentview = StudentBasePDFFormPage(self.context.student,
[9375]1154            self.request, self.omit_fields)
[7150]1155        students_utils = getUtility(IStudentsUtils)
[9906]1156        tabledata_1 = sorted(
1157            [value for value in self.context.values() if value.semester == 1],
[7318]1158            key=lambda value: str(value.semester) + value.code)
[9906]1159        tabledata_2 = sorted(
1160            [value for value in self.context.values() if value.semester == 2],
1161            key=lambda value: str(value.semester) + value.code)
1162        tableheader = [(Code,'code', 2.5),
[7723]1163                         (Title,'title', 5),
1164                         (Dept,'dcode', 1.5), (Faculty,'fcode', 1.5),
1165                         (Cred, 'credits', 1.5),
[9906]1166                         #(Mand, 'mandatory', 1.5),
[8141]1167                         (Score, 'score', 1.5),
[9810]1168                         (Grade, 'grade', 1.5),
[8141]1169                         #('Auto', 'automatic', 1.5)
[9906]1170                         ]
1171        return students_utils.renderPDF(
1172            self, 'course_registration_slip.pdf',
1173            self.context.student, studentview,
1174            tableheader_1=tableheader,
1175            tabledata_1=tabledata_1,
1176            tableheader_2=tableheader,
1177            tabledata_2=tabledata_2
1178            )
[7028]1179
[7819]1180class StudyLevelManageFormPage(KofaEditFormPage):
[6792]1181    """ Page to edit the student study level data
1182    """
1183    grok.context(IStudentStudyLevel)
1184    grok.name('manage')
[7136]1185    grok.require('waeup.manageStudent')
[6792]1186    grok.template('studylevelmanagepage')
[9161]1187    form_fields = grok.AutoFields(IStudentStudyLevel).omit(
[9690]1188        'validation_date', 'validated_by', 'total_credits', 'gpa')
[6792]1189    pnav = 4
[7723]1190    taboneactions = [_('Save'),_('Cancel')]
1191    tabtwoactions = [_('Add course ticket'),
1192        _('Remove selected tickets'),_('Cancel')]
[6792]1193
[9895]1194    def update(self, ADD=None, course=None):
[9139]1195        if not self.context.__parent__.is_current:
1196            emit_lock_message(self)
1197            return
[6792]1198        super(StudyLevelManageFormPage, self).update()
1199        tabs.need()
[7484]1200        self.tab1 = self.tab2 = ''
1201        qs = self.request.get('QUERY_STRING', '')
1202        if not qs:
1203            qs = 'tab1'
1204        setattr(self, qs, 'active')
[7490]1205        warning.need()
1206        datatable.need()
[9895]1207        if ADD is not None:
1208            if not course:
1209                self.flash(_('No valid course code entered.'))
1210                self.redirect(self.url(self.context, u'@@manage')+'?tab2')
1211                return
1212            cat = queryUtility(ICatalog, name='courses_catalog')
1213            result = cat.searchResults(code=(course, course))
1214            if len(result) != 1:
1215                self.flash(_('Course not found.'))
1216            else:
1217                course = list(result)[0]
1218                addCourseTicket(self, course)
1219            self.redirect(self.url(self.context, u'@@manage')+'?tab2')
[6792]1220        return
1221
1222    @property
[8921]1223    def translated_values(self):
1224        return translated_values(self)
1225
1226    @property
[6792]1227    def label(self):
[7833]1228        # Here we know that the cookie has been set
1229        lang = self.request.cookies.get('kofa.language')
[7811]1230        level_title = translate(self.context.level_title, 'waeup.kofa',
[7723]1231            target_language=lang)
1232        return _('Manage study level ${a}',
1233            mapping = {'a':level_title})
[6792]1234
[7723]1235    @action(_('Save'), style='primary')
[6792]1236    def save(self, **data):
1237        msave(self, **data)
1238        return
1239
[7723]1240    @jsaction(_('Remove selected tickets'))
[6792]1241    def delCourseTicket(self, **data):
1242        form = self.request.form
[9701]1243        if 'val_id' in form:
[6792]1244            child_id = form['val_id']
1245        else:
[7723]1246            self.flash(_('No ticket selected.'))
[7484]1247            self.redirect(self.url(self.context, '@@manage')+'?tab2')
[6792]1248            return
1249        if not isinstance(child_id, list):
1250            child_id = [child_id]
1251        deleted = []
1252        for id in child_id:
[7723]1253            del self.context[id]
1254            deleted.append(id)
[6792]1255        if len(deleted):
[7723]1256            self.flash(_('Successfully removed: ${a}',
1257                mapping = {'a':', '.join(deleted)}))
[9332]1258            self.context.writeLogMessage(
1259                self,'removed: %s' % ', '.join(deleted))
[7484]1260        self.redirect(self.url(self.context, u'@@manage')+'?tab2')
[6792]1261        return
1262
[7459]1263class ValidateCoursesPage(UtilityView, grok.View):
[7334]1264    """ Validate course list by course adviser
1265    """
1266    grok.context(IStudentStudyLevel)
1267    grok.name('validate_courses')
1268    grok.require('waeup.validateStudent')
1269
1270    def update(self):
[9139]1271        if not self.context.__parent__.is_current:
1272            emit_lock_message(self)
1273            return
[7334]1274        if str(self.context.__parent__.current_level) != self.context.__name__:
[7723]1275            self.flash(_('This level does not correspond current level.'))
[8736]1276        elif self.context.student.state == REGISTERED:
1277            IWorkflowInfo(self.context.student).fireTransition(
[7642]1278                'validate_courses')
[7723]1279            self.flash(_('Course list has been validated.'))
[7334]1280        else:
[7723]1281            self.flash(_('Student is in the wrong state.'))
[7334]1282        self.redirect(self.url(self.context))
1283        return
1284
1285    def render(self):
1286        return
1287
[7459]1288class RejectCoursesPage(UtilityView, grok.View):
[7334]1289    """ Reject course list by course adviser
1290    """
1291    grok.context(IStudentStudyLevel)
1292    grok.name('reject_courses')
1293    grok.require('waeup.validateStudent')
1294
1295    def update(self):
[9139]1296        if not self.context.__parent__.is_current:
1297            emit_lock_message(self)
1298            return
[7334]1299        if str(self.context.__parent__.current_level) != self.context.__name__:
[7723]1300            self.flash(_('This level does not correspond current level.'))
[7334]1301            self.redirect(self.url(self.context))
1302            return
[8736]1303        elif self.context.student.state == VALIDATED:
1304            IWorkflowInfo(self.context.student).fireTransition('reset8')
[7723]1305            message = _('Course list request has been annulled.')
[7334]1306            self.flash(message)
[8736]1307        elif self.context.student.state == REGISTERED:
1308            IWorkflowInfo(self.context.student).fireTransition('reset7')
[7723]1309            message = _('Course list request has been rejected:')
[7334]1310            self.flash(message)
1311        else:
[7723]1312            self.flash(_('Student is in the wrong state.'))
[7334]1313            self.redirect(self.url(self.context))
1314            return
1315        args = {'subject':message}
[8736]1316        self.redirect(self.url(self.context.student) +
[7334]1317            '/contactstudent?%s' % urlencode(args))
1318        return
1319
1320    def render(self):
1321        return
1322
[7819]1323class CourseTicketAddFormPage(KofaAddFormPage):
[6808]1324    """Add a course ticket.
[6795]1325    """
1326    grok.context(IStudentStudyLevel)
1327    grok.name('add')
[7136]1328    grok.require('waeup.manageStudent')
[7723]1329    label = _('Add course ticket')
[9420]1330    form_fields = grok.AutoFields(ICourseTicketAdd)
[6795]1331    pnav = 4
1332
[9139]1333    def update(self):
1334        if not self.context.__parent__.is_current:
1335            emit_lock_message(self)
1336            return
1337        super(CourseTicketAddFormPage, self).update()
1338        return
1339
[7723]1340    @action(_('Add course ticket'))
[6795]1341    def addCourseTicket(self, **data):
1342        course = data['course']
[9895]1343        success = addCourseTicket(self, course)
1344        if success:
1345            self.redirect(self.url(self.context, u'@@manage')+'?tab2')
[6795]1346        return
1347
[7834]1348    @action(_('Cancel'), validator=NullValidator)
[6795]1349    def cancel(self, **data):
1350        self.redirect(self.url(self.context))
1351
[7819]1352class CourseTicketDisplayFormPage(KofaDisplayFormPage):
[6796]1353    """ Page to display course tickets
1354    """
1355    grok.context(ICourseTicket)
1356    grok.name('index')
1357    grok.require('waeup.viewStudent')
1358    form_fields = grok.AutoFields(ICourseTicket)
[9684]1359    grok.template('courseticketpage')
[6796]1360    pnav = 4
1361
1362    @property
1363    def label(self):
[7723]1364        return _('${a}: Course Ticket ${b}', mapping = {
[8736]1365            'a':self.context.student.display_fullname,
[7723]1366            'b':self.context.code})
[6796]1367
[7819]1368class CourseTicketManageFormPage(KofaEditFormPage):
[6796]1369    """ Page to manage course tickets
1370    """
1371    grok.context(ICourseTicket)
1372    grok.name('manage')
[7136]1373    grok.require('waeup.manageStudent')
[9420]1374    form_fields = grok.AutoFields(ICourseTicket)
1375    form_fields['title'].for_display = True
1376    form_fields['fcode'].for_display = True
1377    form_fields['dcode'].for_display = True
1378    form_fields['semester'].for_display = True
1379    form_fields['passmark'].for_display = True
1380    form_fields['credits'].for_display = True
[9698]1381    form_fields['mandatory'].for_display = False
[9420]1382    form_fields['automatic'].for_display = True
[9422]1383    form_fields['carry_over'].for_display = True
[6796]1384    pnav = 4
[9697]1385    grok.template('courseticketmanagepage')
[6796]1386
1387    @property
1388    def label(self):
[7723]1389        return _('Manage course ticket ${a}', mapping = {'a':self.context.code})
[6796]1390
[7459]1391    @action('Save', style='primary')
[6796]1392    def save(self, **data):
1393        msave(self, **data)
1394        return
1395
[7819]1396class PaymentsManageFormPage(KofaEditFormPage):
[6869]1397    """ Page to manage the student payments
[7642]1398
1399    This manage form page is for both students and students officers.
[6869]1400    """
1401    grok.context(IStudentPaymentsContainer)
[6940]1402    grok.name('index')
[7181]1403    grok.require('waeup.payStudent')
[6869]1404    form_fields = grok.AutoFields(IStudentPaymentsContainer)
1405    grok.template('paymentsmanagepage')
1406    pnav = 4
1407
[6940]1408    def unremovable(self, ticket):
[7251]1409        usertype = getattr(self.request.principal, 'user_type', None)
1410        if not usertype:
1411            return False
1412        return (self.request.principal.user_type == 'student' and ticket.r_code)
[6940]1413
[6869]1414    @property
1415    def label(self):
[7723]1416        return _('${a}: Payments',
1417            mapping = {'a':self.context.__parent__.display_fullname})
[6869]1418
1419    def update(self):
1420        super(PaymentsManageFormPage, self).update()
1421        datatable.need()
[7329]1422        warning.need()
[6869]1423        return
1424
[7723]1425    @jsaction(_('Remove selected tickets'))
[6869]1426    def delPaymentTicket(self, **data):
1427        form = self.request.form
[9701]1428        if 'val_id' in form:
[6869]1429            child_id = form['val_id']
1430        else:
[7723]1431            self.flash(_('No payment selected.'))
[6940]1432            self.redirect(self.url(self.context))
[6869]1433            return
1434        if not isinstance(child_id, list):
1435            child_id = [child_id]
1436        deleted = []
1437        for id in child_id:
[6992]1438            # Students are not allowed to remove used payment tickets
[6940]1439            if not self.unremovable(self.context[id]):
[7723]1440                del self.context[id]
1441                deleted.append(id)
[6869]1442        if len(deleted):
[7723]1443            self.flash(_('Successfully removed: ${a}',
1444                mapping = {'a': ', '.join(deleted)}))
[8735]1445            self.context.writeLogMessage(
[8885]1446                self,'removed: %s' % ', '.join(deleted))
[6940]1447        self.redirect(self.url(self.context))
[6869]1448        return
1449
[9517]1450    #@action(_('Add online payment ticket'))
1451    #def addPaymentTicket(self, **data):
1452    #    self.redirect(self.url(self.context, '@@addop'))
[6869]1453
[7819]1454class OnlinePaymentAddFormPage(KofaAddFormPage):
[6869]1455    """ Page to add an online payment ticket
1456    """
1457    grok.context(IStudentPaymentsContainer)
1458    grok.name('addop')
[9729]1459    grok.template('onlinepaymentaddform')
[7181]1460    grok.require('waeup.payStudent')
[6877]1461    form_fields = grok.AutoFields(IStudentOnlinePayment).select(
[6869]1462        'p_category')
[7723]1463    label = _('Add online payment')
[6869]1464    pnav = 4
[7642]1465
[9729]1466    @property
1467    def selectable_categories(self):
1468        categories = getUtility(IKofaUtils).SELECTABLE_PAYMENT_CATEGORIES
1469        return sorted(categories.items())
1470
[7723]1471    @action(_('Create ticket'), style='primary')
[6869]1472    def createTicket(self, **data):
[7024]1473        p_category = data['p_category']
[9148]1474        previous_session = data.get('p_session', None)
1475        previous_level = data.get('p_level', None)
[7024]1476        student = self.context.__parent__
1477        if p_category == 'bed_allocation' and student[
1478            'studycourse'].current_session != grok.getSite()[
[8685]1479            'hostels'].accommodation_session:
[7024]1480                self.flash(
[7723]1481                    _('Your current session does not match ' + \
1482                    'accommodation session.'))
[7024]1483                return
[9423]1484        if 'maintenance' in p_category:
1485            current_session = str(student['studycourse'].current_session)
[9701]1486            if not current_session in student['accommodation']:
[9424]1487                self.flash(_('You have not yet booked accommodation.'))
[9423]1488                return
[7150]1489        students_utils = getUtility(IStudentsUtils)
[9148]1490        error, payment = students_utils.setPaymentDetails(
1491            p_category, student, previous_session, previous_level)
[8595]1492        if error is not None:
1493            self.flash(error)
[8081]1494            return
[6869]1495        self.context[payment.p_id] = payment
[7723]1496        self.flash(_('Payment ticket created.'))
[6940]1497        self.redirect(self.url(self.context))
[6869]1498        return
1499
[9383]1500    @action(_('Cancel'), validator=NullValidator)
1501    def cancel(self, **data):
1502        self.redirect(self.url(self.context))
1503
[9862]1504class PreviousPaymentAddFormPage(KofaAddFormPage):
[9148]1505    """ Page to add an online payment ticket for previous sessions
1506    """
1507    grok.context(IStudentPaymentsContainer)
1508    grok.name('addpp')
1509    grok.require('waeup.payStudent')
[9864]1510    form_fields = grok.AutoFields(IStudentPreviousPayment)
[9148]1511    label = _('Add previous session online payment')
1512    pnav = 4
1513
[9517]1514    def update(self):
[9521]1515        if self.context.student.before_payment:
1516            self.flash(_("No previous payment to be made."))
[9517]1517            self.redirect(self.url(self.context))
1518        super(PreviousPaymentAddFormPage, self).update()
1519        return
1520
[9862]1521    @action(_('Create ticket'), style='primary')
1522    def createTicket(self, **data):
1523        p_category = data['p_category']
1524        previous_session = data.get('p_session', None)
1525        previous_level = data.get('p_level', None)
1526        student = self.context.__parent__
1527        students_utils = getUtility(IStudentsUtils)
1528        error, payment = students_utils.setPaymentDetails(
1529            p_category, student, previous_session, previous_level)
1530        if error is not None:
1531            self.flash(error)
1532            return
1533        self.context[payment.p_id] = payment
1534        self.flash(_('Payment ticket created.'))
1535        self.redirect(self.url(self.context))
1536        return
1537
1538    @action(_('Cancel'), validator=NullValidator)
1539    def cancel(self, **data):
1540        self.redirect(self.url(self.context))
1541
[9864]1542class BalancePaymentAddFormPage(KofaAddFormPage):
1543    """ Page to add an online payment ticket for balance sessions
1544    """
1545    grok.context(IStudentPaymentsContainer)
1546    grok.name('addbp')
1547    grok.require('waeup.payStudent')
1548    form_fields = grok.AutoFields(IStudentBalancePayment)
1549    label = _('Add balance')
1550    pnav = 4
1551
1552    @action(_('Create ticket'), style='primary')
1553    def createTicket(self, **data):
[9868]1554        p_category = data['p_category']
[9864]1555        balance_session = data.get('balance_session', None)
1556        balance_level = data.get('balance_level', None)
1557        balance_amount = data.get('balance_amount', None)
1558        student = self.context.__parent__
1559        students_utils = getUtility(IStudentsUtils)
1560        error, payment = students_utils.setBalanceDetails(
[9868]1561            p_category, student, balance_session,
[9864]1562            balance_level, balance_amount)
1563        if error is not None:
1564            self.flash(error)
1565            return
1566        self.context[payment.p_id] = payment
1567        self.flash(_('Payment ticket created.'))
1568        self.redirect(self.url(self.context))
1569        return
1570
1571    @action(_('Cancel'), validator=NullValidator)
1572    def cancel(self, **data):
1573        self.redirect(self.url(self.context))
1574
[7819]1575class OnlinePaymentDisplayFormPage(KofaDisplayFormPage):
[6869]1576    """ Page to view an online payment ticket
1577    """
[6877]1578    grok.context(IStudentOnlinePayment)
[6869]1579    grok.name('index')
1580    grok.require('waeup.viewStudent')
[6877]1581    form_fields = grok.AutoFields(IStudentOnlinePayment)
[8170]1582    form_fields[
1583        'creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
1584    form_fields[
1585        'payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
[6869]1586    pnav = 4
1587
1588    @property
1589    def label(self):
[7723]1590        return _('${a}: Online Payment Ticket ${b}', mapping = {
[8736]1591            'a':self.context.student.display_fullname,
[7723]1592            'b':self.context.p_id})
[6869]1593
[8420]1594class OnlinePaymentApprovePage(UtilityView, grok.View):
[6930]1595    """ Callback view
1596    """
1597    grok.context(IStudentOnlinePayment)
[8420]1598    grok.name('approve')
1599    grok.require('waeup.managePortal')
[6930]1600
1601    def update(self):
[8428]1602        success, msg, log = self.context.approveStudentPayment()
1603        if log is not None:
[9770]1604            # Add log message to students.log
[8735]1605            self.context.writeLogMessage(self,log)
[9770]1606            # Add log message to payments.log
1607            self.context.logger.info(
[9779]1608                '%s,%s,%s,%s,%s,,,,,,' % (
[9770]1609                self.context.student.student_id,
1610                self.context.p_id, self.context.p_category,
1611                self.context.amount_auth, self.context.r_code))
[8420]1612        self.flash(msg)
[6940]1613        return
[6930]1614
1615    def render(self):
[6940]1616        self.redirect(self.url(self.context, '@@index'))
[6930]1617        return
1618
[8420]1619class OnlinePaymentFakeApprovePage(OnlinePaymentApprovePage):
1620    """ Approval view for students.
1621
1622    This view is used for browser tests only and
1623    must be neutralized in custom pages!
1624    """
1625
1626    grok.name('fake_approve')
1627    grok.require('waeup.payStudent')
1628
[7459]1629class ExportPDFPaymentSlipPage(UtilityView, grok.View):
[7019]1630    """Deliver a PDF slip of the context.
1631    """
1632    grok.context(IStudentOnlinePayment)
[8262]1633    grok.name('payment_slip.pdf')
[7019]1634    grok.require('waeup.viewStudent')
1635    form_fields = grok.AutoFields(IStudentOnlinePayment)
[8173]1636    form_fields['creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
1637    form_fields['payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
[7019]1638    prefix = 'form'
[8258]1639    note = None
[9702]1640    omit_fields = (
1641        'password', 'suspended', 'phone',
1642        'adm_code', 'sex', 'suspended_comment')
[7019]1643
1644    @property
[8262]1645    def title(self):
1646        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
1647        return translate(_('Payment Data'), 'waeup.kofa',
1648            target_language=portal_language)
1649
1650    @property
[7019]1651    def label(self):
[8262]1652        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
1653        return translate(_('Online Payment Slip'),
1654            'waeup.kofa', target_language=portal_language) \
1655            + ' %s' % self.context.p_id
[7019]1656
1657    def render(self):
[8262]1658        #if self.context.p_state != 'paid':
1659        #    self.flash('Ticket not yet paid.')
1660        #    self.redirect(self.url(self.context))
1661        #    return
[9141]1662        studentview = StudentBasePDFFormPage(self.context.student,
[9375]1663            self.request, self.omit_fields)
[7150]1664        students_utils = getUtility(IStudentsUtils)
[8262]1665        return students_utils.renderPDF(self, 'payment_slip.pdf',
[8736]1666            self.context.student, studentview, note=self.note)
[7019]1667
[6992]1668
[7819]1669class AccommodationManageFormPage(KofaEditFormPage):
[7009]1670    """ Page to manage bed tickets.
[7642]1671
1672    This manage form page is for both students and students officers.
[6635]1673    """
1674    grok.context(IStudentAccommodation)
1675    grok.name('index')
[7181]1676    grok.require('waeup.handleAccommodation')
[6635]1677    form_fields = grok.AutoFields(IStudentAccommodation)
[6992]1678    grok.template('accommodationmanagepage')
[6642]1679    pnav = 4
[7723]1680    officers_only_actions = [_('Remove selected')]
[6635]1681
1682    @property
1683    def label(self):
[7723]1684        return _('${a}: Accommodation',
1685            mapping = {'a':self.context.__parent__.display_fullname})
[6637]1686
[6992]1687    def update(self):
1688        super(AccommodationManageFormPage, self).update()
1689        datatable.need()
[7329]1690        warning.need()
[6992]1691        return
1692
[7723]1693    @jsaction(_('Remove selected'))
[7009]1694    def delBedTickets(self, **data):
[7240]1695        if getattr(self.request.principal, 'user_type', None) == 'student':
[7723]1696            self.flash(_('You are not allowed to remove bed tickets.'))
[7017]1697            self.redirect(self.url(self.context))
1698            return
[6992]1699        form = self.request.form
[9701]1700        if 'val_id' in form:
[6992]1701            child_id = form['val_id']
1702        else:
[7723]1703            self.flash(_('No bed ticket selected.'))
[6992]1704            self.redirect(self.url(self.context))
1705            return
1706        if not isinstance(child_id, list):
1707            child_id = [child_id]
1708        deleted = []
1709        for id in child_id:
[7068]1710            del self.context[id]
1711            deleted.append(id)
[6992]1712        if len(deleted):
[7723]1713            self.flash(_('Successfully removed: ${a}',
1714                mapping = {'a':', '.join(deleted)}))
[8735]1715            self.context.writeLogMessage(
1716                self,'removed: % s' % ', '.join(deleted))
[6992]1717        self.redirect(self.url(self.context))
1718        return
1719
[7009]1720    @property
1721    def selected_actions(self):
[7240]1722        if getattr(self.request.principal, 'user_type', None) == 'student':
[7642]1723            return [action for action in self.actions
1724                    if not action.label in self.officers_only_actions]
1725        return self.actions
[7009]1726
[7819]1727class BedTicketAddPage(KofaPage):
[6992]1728    """ Page to add an online payment ticket
1729    """
1730    grok.context(IStudentAccommodation)
1731    grok.name('add')
[7181]1732    grok.require('waeup.handleAccommodation')
[6992]1733    grok.template('enterpin')
[6993]1734    ac_prefix = 'HOS'
[7723]1735    label = _('Add bed ticket')
[6992]1736    pnav = 4
[7723]1737    buttonname = _('Create bed ticket')
[6993]1738    notice = ''
[9188]1739    with_ac = True
[6992]1740
1741    def update(self, SUBMIT=None):
[8736]1742        student = self.context.student
[7150]1743        students_utils = getUtility(IStudentsUtils)
[7186]1744        acc_details  = students_utils.getAccommodationDetails(student)
[8688]1745        if acc_details.get('expired', False):
1746            startdate = acc_details.get('startdate')
1747            enddate = acc_details.get('enddate')
1748            if startdate and enddate:
1749                tz = getUtility(IKofaUtils).tzinfo
1750                startdate = to_timezone(
1751                    startdate, tz).strftime("%d/%m/%Y %H:%M:%S")
1752                enddate = to_timezone(
1753                    enddate, tz).strftime("%d/%m/%Y %H:%M:%S")
1754                self.flash(_("Outside booking period: ${a} - ${b}",
1755                    mapping = {'a': startdate, 'b': enddate}))
1756            else:
1757                self.flash(_("Outside booking period."))
1758            self.redirect(self.url(self.context))
1759            return
[7369]1760        if not acc_details:
[7723]1761            self.flash(_("Your data are incomplete."))
[7369]1762            self.redirect(self.url(self.context))
1763            return
[6996]1764        if not student.state in acc_details['allowed_states']:
[7723]1765            self.flash(_("You are in the wrong registration state."))
[6992]1766            self.redirect(self.url(self.context))
1767            return
[7642]1768        if student['studycourse'].current_session != acc_details[
1769            'booking_session']:
[7061]1770            self.flash(
[7723]1771                _('Your current session does not match accommodation session.'))
[7061]1772            self.redirect(self.url(self.context))
1773            return
1774        if str(acc_details['booking_session']) in self.context.keys():
[7642]1775            self.flash(
[7723]1776                _('You already booked a bed space in current ' \
1777                    + 'accommodation session.'))
[7004]1778            self.redirect(self.url(self.context))
1779            return
[9188]1780        if self.with_ac:
1781            self.ac_series = self.request.form.get('ac_series', None)
1782            self.ac_number = self.request.form.get('ac_number', None)
[6992]1783        if SUBMIT is None:
1784            return
[9188]1785        if self.with_ac:
1786            pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number)
1787            code = get_access_code(pin)
1788            if not code:
1789                self.flash(_('Activation code is invalid.'))
1790                return
[7060]1791        # Search and book bed
[6997]1792        cat = queryUtility(ICatalog, name='beds_catalog', default=None)
1793        entries = cat.searchResults(
[7003]1794            owner=(student.student_id,student.student_id))
1795        if len(entries):
[9188]1796            # If bed space has been manually allocated use this bed
[7003]1797            bed = [entry for entry in entries][0]
[9424]1798            # Safety belt for paranoids: Does this bed really exist on portal?
1799            # XXX: Can be remove if nobody complains.
1800            if bed.__parent__.__parent__ is None:
1801                self.flash(_('System error: Please contact the adminsitrator.'))
[9428]1802                self.context.writeLogMessage(self, 'fatal error: %s' % bed.bed_id)
[9424]1803                return
[7060]1804        else:
1805            # else search for other available beds
1806            entries = cat.searchResults(
1807                bed_type=(acc_details['bt'],acc_details['bt']))
1808            available_beds = [
1809                entry for entry in entries if entry.owner == NOT_OCCUPIED]
1810            if available_beds:
[7150]1811                students_utils = getUtility(IStudentsUtils)
[7186]1812                bed = students_utils.selectBed(available_beds)
[9424]1813                # Safety belt for paranoids: Does this bed really exist in portal?
1814                # XXX: Can be remove if nobody complains.
1815                if bed.__parent__.__parent__ is None:
1816                    self.flash(_('System error: Please contact the adminsitrator.'))
[9428]1817                    self.context.writeLogMessage(self, 'fatal error: %s' % bed.bed_id)
[9424]1818                    return
[7060]1819                bed.bookBed(student.student_id)
1820            else:
[7723]1821                self.flash(_('There is no free bed in your category ${a}.',
1822                    mapping = {'a':acc_details['bt']}))
[7060]1823                return
[9188]1824        if self.with_ac:
1825            # Mark pin as used (this also fires a pin related transition)
1826            if code.state == USED:
1827                self.flash(_('Activation code has already been used.'))
[6992]1828                return
[9188]1829            else:
1830                comment = _(u'invalidated')
1831                # Here we know that the ac is in state initialized so we do not
1832                # expect an exception, but the owner might be different
1833                if not invalidate_accesscode(
1834                    pin,comment,self.context.student.student_id):
1835                    self.flash(_('You are not the owner of this access code.'))
1836                    return
[7060]1837        # Create bed ticket
[6992]1838        bedticket = createObject(u'waeup.BedTicket')
[9189]1839        if self.with_ac:
1840            bedticket.booking_code = pin
[6994]1841        bedticket.booking_session = acc_details['booking_session']
[6996]1842        bedticket.bed_type = acc_details['bt']
[7006]1843        bedticket.bed = bed
[6996]1844        hall_title = bed.__parent__.hostel_name
[9199]1845        coordinates = bed.coordinates[1:]
[6996]1846        block, room_nr, bed_nr = coordinates
[7723]1847        bc = _('${a}, Block ${b}, Room ${c}, Bed ${d} (${e})', mapping = {
1848            'a':hall_title, 'b':block,
1849            'c':room_nr, 'd':bed_nr,
1850            'e':bed.bed_type})
[7819]1851        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
[7723]1852        bedticket.bed_coordinates = translate(
[7811]1853            bc, 'waeup.kofa',target_language=portal_language)
[9423]1854        self.context.addBedTicket(bedticket)
[9411]1855        self.context.writeLogMessage(self, 'booked: %s' % bed.bed_id)
[7723]1856        self.flash(_('Bed ticket created and bed booked: ${a}',
1857            mapping = {'a':bedticket.bed_coordinates}))
[6992]1858        self.redirect(self.url(self.context))
1859        return
1860
[7819]1861class BedTicketDisplayFormPage(KofaDisplayFormPage):
[6994]1862    """ Page to display bed tickets
1863    """
1864    grok.context(IBedTicket)
1865    grok.name('index')
[7181]1866    grok.require('waeup.handleAccommodation')
[6994]1867    form_fields = grok.AutoFields(IBedTicket)
[9201]1868    form_fields['booking_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
[6994]1869    pnav = 4
1870
1871    @property
1872    def label(self):
[7723]1873        return _('Bed Ticket for Session ${a}',
1874            mapping = {'a':self.context.getSessionString()})
[6994]1875
[7459]1876class ExportPDFBedTicketSlipPage(UtilityView, grok.View):
[7027]1877    """Deliver a PDF slip of the context.
1878    """
1879    grok.context(IBedTicket)
[9452]1880    grok.name('bed_allocation_slip.pdf')
[7181]1881    grok.require('waeup.handleAccommodation')
[7027]1882    form_fields = grok.AutoFields(IBedTicket)
[8173]1883    form_fields['booking_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
[7027]1884    prefix = 'form'
[9702]1885    omit_fields = (
1886        'password', 'suspended', 'phone', 'adm_code', 'suspended_comment')
[7027]1887
1888    @property
[7723]1889    def title(self):
[7819]1890        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
[7811]1891        return translate(_('Bed Allocation Data'), 'waeup.kofa',
[7723]1892            target_language=portal_language)
1893
1894    @property
[7027]1895    def label(self):
[7819]1896        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
[9201]1897        #return translate(_('Bed Allocation: '),
1898        #    'waeup.kofa', target_language=portal_language) \
1899        #    + ' %s' % self.context.bed_coordinates
1900        return translate(_('Bed Allocation Slip'),
[7811]1901            'waeup.kofa', target_language=portal_language) \
[9201]1902            + ' %s' % self.context.getSessionString()
[7027]1903
1904    def render(self):
[9141]1905        studentview = StudentBasePDFFormPage(self.context.student,
[9375]1906            self.request, self.omit_fields)
[7150]1907        students_utils = getUtility(IStudentsUtils)
[7186]1908        return students_utils.renderPDF(
[9452]1909            self, 'bed_allocation_slip.pdf',
[8736]1910            self.context.student, studentview)
[7027]1911
[7459]1912class BedTicketRelocationPage(UtilityView, grok.View):
[7015]1913    """ Callback view
1914    """
1915    grok.context(IBedTicket)
1916    grok.name('relocate')
1917    grok.require('waeup.manageHostels')
1918
[7059]1919    # Relocate student if student parameters have changed or the bed_type
1920    # of the bed has changed
[7015]1921    def update(self):
[8736]1922        student = self.context.student
[7150]1923        students_utils = getUtility(IStudentsUtils)
[7186]1924        acc_details  = students_utils.getAccommodationDetails(student)
[7068]1925        if self.context.bed != None and \
1926              'reserved' in self.context.bed.bed_type:
[7723]1927            self.flash(_("Students in reserved beds can't be relocated."))
[7068]1928            self.redirect(self.url(self.context))
1929            return
[7059]1930        if acc_details['bt'] == self.context.bed_type and \
[7068]1931                self.context.bed != None and \
[7059]1932                self.context.bed.bed_type == self.context.bed_type:
[7723]1933            self.flash(_("Student can't be relocated."))
[7068]1934            self.redirect(self.url(self.context))
[7015]1935            return
[7068]1936        # Search a bed
[7015]1937        cat = queryUtility(ICatalog, name='beds_catalog', default=None)
1938        entries = cat.searchResults(
[7068]1939            owner=(student.student_id,student.student_id))
1940        if len(entries) and self.context.bed == None:
1941            # If booking has been cancelled but other bed space has been
1942            # manually allocated after cancellation use this bed
1943            new_bed = [entry for entry in entries][0]
1944        else:
1945            # Search for other available beds
1946            entries = cat.searchResults(
1947                bed_type=(acc_details['bt'],acc_details['bt']))
1948            available_beds = [
1949                entry for entry in entries if entry.owner == NOT_OCCUPIED]
1950            if available_beds:
[7150]1951                students_utils = getUtility(IStudentsUtils)
[7186]1952                new_bed = students_utils.selectBed(available_beds)
[7068]1953                new_bed.bookBed(student.student_id)
1954            else:
[7723]1955                self.flash(_('There is no free bed in your category ${a}.',
1956                    mapping = {'a':acc_details['bt']}))
[7068]1957                self.redirect(self.url(self.context))
1958                return
[7642]1959        # Release old bed if exists
[7068]1960        if self.context.bed != None:
1961            self.context.bed.owner = NOT_OCCUPIED
1962            notify(grok.ObjectModifiedEvent(self.context.bed))
[7015]1963        # Alocate new bed
1964        self.context.bed_type = acc_details['bt']
[7068]1965        self.context.bed = new_bed
1966        hall_title = new_bed.__parent__.hostel_name
[9199]1967        coordinates = new_bed.coordinates[1:]
[7015]1968        block, room_nr, bed_nr = coordinates
[7723]1969        bc = _('${a}, Block ${b}, Room ${c}, Bed ${d} (${e})', mapping = {
1970            'a':hall_title, 'b':block,
1971            'c':room_nr, 'd':bed_nr,
1972            'e':new_bed.bed_type})
[7819]1973        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
[7723]1974        self.context.bed_coordinates = translate(
[7811]1975            bc, 'waeup.kofa',target_language=portal_language)
[9411]1976        self.context.writeLogMessage(self, 'relocated: %s' % new_bed.bed_id)
[7723]1977        self.flash(_('Student relocated: ${a}',
1978            mapping = {'a':self.context.bed_coordinates}))
[7015]1979        self.redirect(self.url(self.context))
1980        return
1981
1982    def render(self):
1983        return
1984
[7819]1985class StudentHistoryPage(KofaPage):
[6637]1986    """ Page to display student clearance data
1987    """
1988    grok.context(IStudent)
1989    grok.name('history')
[6660]1990    grok.require('waeup.viewStudent')
[6637]1991    grok.template('studenthistory')
[6642]1992    pnav = 4
[6637]1993
1994    @property
1995    def label(self):
[7723]1996        return _('${a}: History', mapping = {'a':self.context.display_fullname})
[6694]1997
1998# Pages for students only
1999
[7819]2000class StudentBaseEditFormPage(KofaEditFormPage):
[7133]2001    """ View to edit student base data
2002    """
2003    grok.context(IStudent)
2004    grok.name('edit_base')
2005    grok.require('waeup.handleStudent')
2006    form_fields = grok.AutoFields(IStudentBase).select(
2007        'email', 'phone')
[7723]2008    label = _('Edit base data')
[7133]2009    pnav = 4
2010
[7723]2011    @action(_('Save'), style='primary')
[7133]2012    def save(self, **data):
2013        msave(self, **data)
2014        return
2015
[7819]2016class StudentChangePasswordPage(KofaEditFormPage):
[7144]2017    """ View to manage student base data
[6756]2018    """
2019    grok.context(IStudent)
[7114]2020    grok.name('change_password')
[6694]2021    grok.require('waeup.handleStudent')
[7144]2022    grok.template('change_password')
[7723]2023    label = _('Change password')
[6694]2024    pnav = 4
2025
[7723]2026    @action(_('Save'), style='primary')
[7144]2027    def save(self, **data):
2028        form = self.request.form
2029        password = form.get('change_password', None)
2030        password_ctl = form.get('change_password_repeat', None)
2031        if password:
[7147]2032            validator = getUtility(IPasswordValidator)
2033            errors = validator.validate_password(password, password_ctl)
2034            if not errors:
2035                IUserAccount(self.context).setPassword(password)
[8735]2036                self.context.writeLogMessage(self, 'saved: password')
[7723]2037                self.flash(_('Password changed.'))
[6756]2038            else:
[7147]2039                self.flash( ' '.join(errors))
[6756]2040        return
2041
[7819]2042class StudentFilesUploadPage(KofaPage):
[7114]2043    """ View to upload files by student
2044    """
2045    grok.context(IStudent)
2046    grok.name('change_portrait')
[7127]2047    grok.require('waeup.uploadStudentFile')
[7114]2048    grok.template('filesuploadpage')
[7723]2049    label = _('Upload portrait')
[7114]2050    pnav = 4
2051
[7133]2052    def update(self):
[8736]2053        if self.context.student.state != ADMITTED:
[7145]2054            emit_lock_message(self)
[7133]2055            return
2056        super(StudentFilesUploadPage, self).update()
2057        return
2058
[7819]2059class StartClearancePage(KofaPage):
[6770]2060    grok.context(IStudent)
2061    grok.name('start_clearance')
2062    grok.require('waeup.handleStudent')
2063    grok.template('enterpin')
[7723]2064    label = _('Start clearance')
[6770]2065    ac_prefix = 'CLR'
2066    notice = ''
2067    pnav = 4
[7723]2068    buttonname = _('Start clearance now')
[6770]2069
[7133]2070    @property
2071    def all_required_fields_filled(self):
2072        if self.context.email and self.context.phone:
2073            return True
2074        return False
2075
2076    @property
2077    def portrait_uploaded(self):
2078        store = getUtility(IExtFileStore)
2079        if store.getFileByContext(self.context, attr=u'passport.jpg'):
2080            return True
2081        return False
2082
[6770]2083    def update(self, SUBMIT=None):
[7671]2084        if not self.context.state == ADMITTED:
[7745]2085            self.flash(_("Wrong state"))
[6936]2086            self.redirect(self.url(self.context))
2087            return
[7133]2088        if not self.portrait_uploaded:
[7723]2089            self.flash(_("No portrait uploaded."))
[7133]2090            self.redirect(self.url(self.context, 'change_portrait'))
2091            return
2092        if not self.all_required_fields_filled:
[7723]2093            self.flash(_("Not all required fields filled."))
[7133]2094            self.redirect(self.url(self.context, 'edit_base'))
2095            return
[6770]2096        self.ac_series = self.request.form.get('ac_series', None)
2097        self.ac_number = self.request.form.get('ac_number', None)
2098
2099        if SUBMIT is None:
2100            return
2101        pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number)
2102        code = get_access_code(pin)
2103        if not code:
[7723]2104            self.flash(_('Activation code is invalid.'))
[6770]2105            return
2106        if code.state == USED:
[7723]2107            self.flash(_('Activation code has already been used.'))
[6770]2108            return
[8974]2109        # Mark pin as used (this also fires a pin related transition)
2110        # and fire transition start_clearance
2111        comment = _(u"invalidated")
2112        # Here we know that the ac is in state initialized so we do not
2113        # expect an exception, but the owner might be different
2114        if not invalidate_accesscode(pin, comment, self.context.student_id):
2115            self.flash(_('You are not the owner of this access code.'))
2116            return
2117        self.context.clr_code = pin
[6770]2118        IWorkflowInfo(self.context).fireTransition('start_clearance')
[7723]2119        self.flash(_('Clearance process has been started.'))
[6770]2120        self.redirect(self.url(self.context,'cedit'))
2121        return
2122
[6695]2123class StudentClearanceEditFormPage(StudentClearanceManageFormPage):
2124    """ View to edit student clearance data by student
2125    """
2126    grok.context(IStudent)
2127    grok.name('cedit')
2128    grok.require('waeup.handleStudent')
[7723]2129    label = _('Edit clearance data')
[6718]2130
[7993]2131    @property
2132    def form_fields(self):
[8472]2133        if self.context.is_postgrad:
[8974]2134            form_fields = grok.AutoFields(IPGStudentClearance).omit(
[9486]2135                'clearance_locked', 'clr_code', 'officer_comment')
[7993]2136        else:
[8974]2137            form_fields = grok.AutoFields(IUGStudentClearance).omit(
[9486]2138                'clearance_locked', 'clr_code', 'officer_comment')
[7993]2139        return form_fields
2140
[6718]2141    def update(self):
2142        if self.context.clearance_locked:
[7145]2143            emit_lock_message(self)
[6718]2144            return
2145        return super(StudentClearanceEditFormPage, self).update()
[6719]2146
[7723]2147    @action(_('Save'), style='primary')
[6722]2148    def save(self, **data):
2149        self.applyData(self.context, **data)
[7723]2150        self.flash(_('Clearance form has been saved.'))
[6722]2151        return
2152
[7253]2153    def dataNotComplete(self):
[7642]2154        """To be implemented in the customization package.
2155        """
[7253]2156        return False
2157
[7723]2158    @action(_('Save and request clearance'), style='primary')
[7186]2159    def requestClearance(self, **data):
[6722]2160        self.applyData(self.context, **data)
[7253]2161        if self.dataNotComplete():
2162            self.flash(self.dataNotComplete())
2163            return
[7723]2164        self.flash(_('Clearance form has been saved.'))
[9021]2165        if self.context.clr_code:
2166            self.redirect(self.url(self.context, 'request_clearance'))
2167        else:
2168            # We bypass the request_clearance page if student
2169            # has been imported in state 'clearance started' and
2170            # no clr_code was entered before.
2171            state = IWorkflowState(self.context).getState()
2172            if state != CLEARANCE:
2173                # This shouldn't happen, but the application officer
2174                # might have forgotten to lock the form after changing the state
2175                self.flash(_('This form cannot be submitted. Wrong state!'))
2176                return
2177            IWorkflowInfo(self.context).fireTransition('request_clearance')
2178            self.flash(_('Clearance has been requested.'))
2179            self.redirect(self.url(self.context))
[6722]2180        return
2181
[7819]2182class RequestClearancePage(KofaPage):
[6769]2183    grok.context(IStudent)
2184    grok.name('request_clearance')
2185    grok.require('waeup.handleStudent')
2186    grok.template('enterpin')
[7723]2187    label = _('Request clearance')
2188    notice = _('Enter the CLR access code used for starting clearance.')
[6769]2189    ac_prefix = 'CLR'
2190    pnav = 4
[7723]2191    buttonname = _('Request clearance now')
[6769]2192
2193    def update(self, SUBMIT=None):
2194        self.ac_series = self.request.form.get('ac_series', None)
2195        self.ac_number = self.request.form.get('ac_number', None)
2196        if SUBMIT is None:
2197            return
2198        pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number)
[9021]2199        if self.context.clr_code and self.context.clr_code != pin:
[7723]2200            self.flash(_("This isn't your CLR access code."))
[6769]2201            return
2202        state = IWorkflowState(self.context).getState()
2203        if state != CLEARANCE:
[9021]2204            # This shouldn't happen, but the application officer
2205            # might have forgotten to lock the form after changing the state
[7723]2206            self.flash(_('This form cannot be submitted. Wrong state!'))
[6769]2207            return
2208        IWorkflowInfo(self.context).fireTransition('request_clearance')
[7723]2209        self.flash(_('Clearance has been requested.'))
[6769]2210        self.redirect(self.url(self.context))
[6789]2211        return
[6806]2212
[8471]2213class StartSessionPage(KofaPage):
[6944]2214    grok.context(IStudentStudyCourse)
[8471]2215    grok.name('start_session')
[6944]2216    grok.require('waeup.handleStudent')
2217    grok.template('enterpin')
[8471]2218    label = _('Start session')
[6944]2219    ac_prefix = 'SFE'
2220    notice = ''
2221    pnav = 4
[8471]2222    buttonname = _('Start now')
[6944]2223
2224    def update(self, SUBMIT=None):
[9139]2225        if not self.context.is_current:
2226            emit_lock_message(self)
2227            return
2228        super(StartSessionPage, self).update()
[8471]2229        if not self.context.next_session_allowed:
2230            self.flash(_("You are not entitled to start session."))
[6944]2231            self.redirect(self.url(self.context))
2232            return
2233        self.ac_series = self.request.form.get('ac_series', None)
2234        self.ac_number = self.request.form.get('ac_number', None)
2235
2236        if SUBMIT is None:
2237            return
2238        pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number)
2239        code = get_access_code(pin)
2240        if not code:
[7723]2241            self.flash(_('Activation code is invalid.'))
[6944]2242            return
2243        # Mark pin as used (this also fires a pin related transition)
2244        if code.state == USED:
[7723]2245            self.flash(_('Activation code has already been used.'))
[6944]2246            return
2247        else:
[7723]2248            comment = _(u"invalidated")
[6944]2249            # Here we know that the ac is in state initialized so we do not
[8471]2250            # expect an error, but the owner might be different
[6944]2251            if not invalidate_accesscode(
[8736]2252                pin,comment,self.context.student.student_id):
[7723]2253                self.flash(_('You are not the owner of this access code.'))
[6944]2254                return
[9637]2255        try:
2256            if self.context.student.state == CLEARED:
2257                IWorkflowInfo(self.context.student).fireTransition(
2258                    'pay_first_school_fee')
2259            elif self.context.student.state == RETURNING:
2260                IWorkflowInfo(self.context.student).fireTransition(
2261                    'pay_school_fee')
2262            elif self.context.student.state == PAID:
2263                IWorkflowInfo(self.context.student).fireTransition(
2264                    'pay_pg_fee')
2265        except ConstraintNotSatisfied:
2266            self.flash(_('An error occurred, please contact the system administrator.'))
2267            return
[8471]2268        self.flash(_('Session started.'))
[6944]2269        self.redirect(self.url(self.context))
2270        return
2271
[7819]2272class AddStudyLevelFormPage(KofaEditFormPage):
[6806]2273    """ Page for students to add current study levels
2274    """
2275    grok.context(IStudentStudyCourse)
2276    grok.name('add')
2277    grok.require('waeup.handleStudent')
2278    grok.template('studyleveladdpage')
2279    form_fields = grok.AutoFields(IStudentStudyCourse)
2280    pnav = 4
2281
2282    @property
2283    def label(self):
2284        studylevelsource = StudyLevelSource().factory
2285        code = self.context.current_level
2286        title = studylevelsource.getTitle(self.context, code)
[7723]2287        return _('Add current level ${a}', mapping = {'a':title})
[6806]2288
2289    def update(self):
[9139]2290        if not self.context.is_current:
2291            emit_lock_message(self)
2292            return
[8736]2293        if self.context.student.state != PAID:
[7145]2294            emit_lock_message(self)
[6806]2295            return
2296        super(AddStudyLevelFormPage, self).update()
2297        return
2298
[7723]2299    @action(_('Create course list now'), style='primary')
[6806]2300    def addStudyLevel(self, **data):
[8323]2301        studylevel = createObject(u'waeup.StudentStudyLevel')
[6806]2302        studylevel.level = self.context.current_level
2303        studylevel.level_session = self.context.current_session
2304        try:
2305            self.context.addStudentStudyLevel(
2306                self.context.certificate,studylevel)
2307        except KeyError:
[7723]2308            self.flash(_('This level exists.'))
[9467]2309        except RequiredMissing:
2310            self.flash(_('Your data are incomplete'))
[6806]2311        self.redirect(self.url(self.context))
2312        return
[6808]2313
[7819]2314class StudyLevelEditFormPage(KofaEditFormPage):
[6808]2315    """ Page to edit the student study level data by students
2316    """
2317    grok.context(IStudentStudyLevel)
2318    grok.name('edit')
2319    grok.require('waeup.handleStudent')
2320    grok.template('studyleveleditpage')
2321    form_fields = grok.AutoFields(IStudentStudyLevel).omit(
2322        'level_session', 'level_verdict')
2323    pnav = 4
2324
[9895]2325    def update(self, ADD=None, course=None):
[9139]2326        if not self.context.__parent__.is_current:
2327            emit_lock_message(self)
2328            return
[9257]2329        if self.context.student.state != PAID or \
2330            not self.context.is_current_level:
[7539]2331            emit_lock_message(self)
2332            return
[6808]2333        super(StudyLevelEditFormPage, self).update()
2334        datatable.need()
[7329]2335        warning.need()
[9895]2336        if ADD is not None:
2337            if not course:
2338                self.flash(_('No valid course code entered.'))
2339                return
2340            cat = queryUtility(ICatalog, name='courses_catalog')
2341            result = cat.searchResults(code=(course, course))
2342            if len(result) != 1:
2343                self.flash(_('Course not found.'))
2344                return
2345            course = list(result)[0]
2346            addCourseTicket(self, course)
[6808]2347        return
2348
2349    @property
2350    def label(self):
[7833]2351        # Here we know that the cookie has been set
2352        lang = self.request.cookies.get('kofa.language')
[7811]2353        level_title = translate(self.context.level_title, 'waeup.kofa',
[7723]2354            target_language=lang)
[8920]2355        return _('Edit course list of ${a}',
[7723]2356            mapping = {'a':level_title})
[6808]2357
2358    @property
[8921]2359    def translated_values(self):
2360        return translated_values(self)
2361
[9280]2362    def _delCourseTicket(self, **data):
[6808]2363        form = self.request.form
[9701]2364        if 'val_id' in form:
[6808]2365            child_id = form['val_id']
2366        else:
[7723]2367            self.flash(_('No ticket selected.'))
[6808]2368            self.redirect(self.url(self.context, '@@edit'))
2369            return
2370        if not isinstance(child_id, list):
2371            child_id = [child_id]
2372        deleted = []
2373        for id in child_id:
[6940]2374            # Students are not allowed to remove core tickets
[9700]2375            if id in self.context and \
2376                self.context[id].removable_by_student:
[7723]2377                del self.context[id]
2378                deleted.append(id)
[6808]2379        if len(deleted):
[7723]2380            self.flash(_('Successfully removed: ${a}',
2381                mapping = {'a':', '.join(deleted)}))
[9332]2382            self.context.writeLogMessage(
2383                self,'removed: %s' % ', '.join(deleted))
[6808]2384        self.redirect(self.url(self.context, u'@@edit'))
2385        return
2386
[9280]2387    @jsaction(_('Remove selected tickets'))
2388    def delCourseTicket(self, **data):
2389        self._delCourseTicket(**data)
2390        return
2391
2392    def _registerCourses(self, **data):
[9252]2393        if self.context.student.is_postgrad:
2394            self.flash(_(
2395                "You are a postgraduate student, "
2396                "your course list can't bee registered."))
2397            self.redirect(self.url(self.context))
2398            return
[9830]2399        students_utils = getUtility(IStudentsUtils)
2400        max_credits = students_utils.maxCredits(self.context)
2401        if self.context.total_credits > max_credits:
[8642]2402            self.flash(_('Maximum credits of ${a} exceeded.',
[9830]2403                mapping = {'a':max_credits}))
[8642]2404            return
[8736]2405        IWorkflowInfo(self.context.student).fireTransition(
[7642]2406            'register_courses')
[7723]2407        self.flash(_('Course list has been registered.'))
[6810]2408        self.redirect(self.url(self.context))
2409        return
2410
[9895]2411    @action(_('Register course list'))
[9280]2412    def registerCourses(self, **data):
2413        self._registerCourses(**data)
2414        return
2415
[6808]2416class CourseTicketAddFormPage2(CourseTicketAddFormPage):
2417    """Add a course ticket by student.
2418    """
2419    grok.name('ctadd')
2420    grok.require('waeup.handleStudent')
[9420]2421    form_fields = grok.AutoFields(ICourseTicketAdd)
[6808]2422
[7539]2423    def update(self):
[9257]2424        if self.context.student.state != PAID or \
2425            not self.context.is_current_level:
[7539]2426            emit_lock_message(self)
2427            return
2428        super(CourseTicketAddFormPage2, self).update()
2429        return
2430
[7723]2431    @action(_('Add course ticket'))
[6808]2432    def addCourseTicket(self, **data):
[7642]2433        # Safety belt
[8736]2434        if self.context.student.state != PAID:
[7539]2435            return
[6808]2436        course = data['course']
[9895]2437        success = addCourseTicket(self, course)
2438        if success:
2439            self.redirect(self.url(self.context, u'@@edit'))
[6808]2440        return
[7369]2441
[7819]2442class SetPasswordPage(KofaPage):
2443    grok.context(IKofaObject)
[7660]2444    grok.name('setpassword')
2445    grok.require('waeup.Anonymous')
2446    grok.template('setpassword')
[7723]2447    label = _('Set password for first-time login')
[7660]2448    ac_prefix = 'PWD'
2449    pnav = 0
[7738]2450    set_button = _('Set')
[7660]2451
2452    def update(self, SUBMIT=None):
2453        self.reg_number = self.request.form.get('reg_number', None)
2454        self.ac_series = self.request.form.get('ac_series', None)
2455        self.ac_number = self.request.form.get('ac_number', None)
2456
2457        if SUBMIT is None:
2458            return
2459        hitlist = search(query=self.reg_number,
2460            searchtype='reg_number', view=self)
2461        if not hitlist:
[7723]2462            self.flash(_('No student found.'))
[7660]2463            return
2464        if len(hitlist) != 1:   # Cannot happen but anyway
[7723]2465            self.flash(_('More than one student found.'))
[7660]2466            return
2467        student = hitlist[0].context
2468        self.student_id = student.student_id
2469        student_pw = student.password
2470        pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number)
2471        code = get_access_code(pin)
2472        if not code:
[7723]2473            self.flash(_('Access code is invalid.'))
[7660]2474            return
2475        if student_pw and pin == student.adm_code:
[7723]2476            self.flash(_(
2477                'Password has already been set. Your Student Id is ${a}',
2478                mapping = {'a':self.student_id}))
[7660]2479            return
2480        elif student_pw:
2481            self.flash(
[7723]2482                _('Password has already been set. You are using the ' +
2483                'wrong Access Code.'))
[7660]2484            return
2485        # Mark pin as used (this also fires a pin related transition)
2486        # and set student password
2487        if code.state == USED:
[7723]2488            self.flash(_('Access code has already been used.'))
[7660]2489            return
2490        else:
[7723]2491            comment = _(u"invalidated")
[7660]2492            # Here we know that the ac is in state initialized so we do not
2493            # expect an exception
2494            invalidate_accesscode(pin,comment)
2495            IUserAccount(student).setPassword(self.ac_number)
2496            student.adm_code = pin
[7723]2497        self.flash(_('Password has been set. Your Student Id is ${a}',
2498            mapping = {'a':self.student_id}))
[7811]2499        return
[8779]2500
2501class StudentRequestPasswordPage(KofaAddFormPage):
2502    """Captcha'd registration page for applicants.
2503    """
2504    grok.name('requestpw')
2505    grok.require('waeup.Anonymous')
2506    grok.template('requestpw')
2507    form_fields = grok.AutoFields(IStudentRequestPW).select(
[8854]2508        'firstname','number','email')
[8779]2509    label = _('Request password for first-time login')
2510
2511    def update(self):
2512        # Handle captcha
2513        self.captcha = getUtility(ICaptchaManager).getCaptcha()
2514        self.captcha_result = self.captcha.verify(self.request)
2515        self.captcha_code = self.captcha.display(self.captcha_result.error_code)
2516        return
2517
2518    def _redirect(self, email, password, student_id):
2519        # Forward only email to landing page in base package.
2520        self.redirect(self.url(self.context, 'requestpw_complete',
2521            data = dict(email=email)))
2522        return
2523
2524    def _pw_used(self):
[8780]2525        # XXX: False if password has not been used. We need an extra
2526        #      attribute which remembers if student logged in.
[8779]2527        return True
2528
[8854]2529    @action(_('Send login credentials to email address'), style='primary')
[8779]2530    def get_credentials(self, **data):
2531        if not self.captcha_result.is_valid:
2532            # Captcha will display error messages automatically.
2533            # No need to flash something.
2534            return
[8854]2535        number = data.get('number','')
[8779]2536        firstname = data.get('firstname','')
2537        cat = getUtility(ICatalog, name='students_catalog')
2538        results = list(
[8854]2539            cat.searchResults(reg_number=(number, number)))
2540        if not results:
2541            results = list(
2542                cat.searchResults(matric_number=(number, number)))
[8779]2543        if results:
2544            student = results[0]
2545            if getattr(student,'firstname',None) is None:
2546                self.flash(_('An error occurred.'))
2547                return
2548            elif student.firstname.lower() != firstname.lower():
2549                # Don't tell the truth here. Anonymous must not
2550                # know that a record was found and only the firstname
2551                # verification failed.
2552                self.flash(_('No student record found.'))
2553                return
2554            elif student.password is not None and self._pw_used:
2555                self.flash(_('Your password has already been set and used. '
2556                             'Please proceed to the login page.'))
2557                return
2558            # Store email address but nothing else.
2559            student.email = data['email']
2560            notify(grok.ObjectModifiedEvent(student))
2561        else:
2562            # No record found, this is the truth.
2563            self.flash(_('No student record found.'))
2564            return
2565
2566        kofa_utils = getUtility(IKofaUtils)
2567        password = kofa_utils.genPassword()
[8857]2568        mandate = PasswordMandate()
[8853]2569        mandate.params['password'] = password
[8858]2570        mandate.params['user'] = student
[8853]2571        site = grok.getSite()
2572        site['mandates'].addMandate(mandate)
[8779]2573        # Send email with credentials
[8853]2574        args = {'mandate_id':mandate.mandate_id}
2575        mandate_url = self.url(site) + '/mandate?%s' % urlencode(args)
2576        url_info = u'Confirmation link: %s' % mandate_url
[8779]2577        msg = _('You have successfully requested a password for the')
2578        if kofa_utils.sendCredentials(IUserAccount(student),
[8853]2579            password, url_info, msg):
[8779]2580            email_sent = student.email
2581        else:
2582            email_sent = None
2583        self._redirect(email=email_sent, password=password,
2584            student_id=student.student_id)
[8856]2585        ob_class = self.__implemented__.__name__.replace('waeup.kofa.','')
2586        self.context.logger.info(
2587            '%s - %s (%s) - %s' % (ob_class, number, student.student_id, email_sent))
[8779]2588        return
2589
2590class StudentRequestPasswordEmailSent(KofaPage):
2591    """Landing page after successful password request.
2592
2593    """
2594    grok.name('requestpw_complete')
2595    grok.require('waeup.Public')
2596    grok.template('requestpwmailsent')
2597    label = _('Your password request was successful.')
2598
2599    def update(self, email=None, student_id=None, password=None):
2600        self.email = email
2601        self.password = password
2602        self.student_id = student_id
[8974]2603        return
[9797]2604
[9806]2605class FilterStudentsInDepartmentPage(KofaPage):
2606    """Page that filters and lists students.
2607    """
2608    grok.context(IDepartment)
2609    grok.require('waeup.showStudents')
2610    grok.name('students')
2611    grok.template('filterstudentspage')
2612    pnav = 1
[9819]2613    session_label = _('Current Session')
2614    level_label = _('Current Level')
[9806]2615
2616    def label(self):
[9819]2617        return 'Students in %s' % self.context.longtitle()
[9806]2618
2619    def _set_session_values(self):
2620        vocab_terms = academic_sessions_vocab.by_value.values()
2621        self.sessions = sorted(
2622            [(x.title, x.token) for x in vocab_terms], reverse=True)
2623        self.sessions += [('All Sessions', 'all')]
2624        return
2625
2626    def _set_level_values(self):
2627        vocab_terms = course_levels.by_value.values()
2628        self.levels = sorted(
2629            [(x.title, x.token) for x in vocab_terms])
2630        self.levels += [('All Levels', 'all')]
2631        return
2632
2633    def _searchCatalog(self, session, level):
2634        if level not in (10, 999, None):
2635            start_level = 100 * (level // 100)
2636            end_level = start_level + 90
2637        else:
2638            start_level = end_level = level
2639        cat = queryUtility(ICatalog, name='students_catalog')
2640        students = cat.searchResults(
2641            current_session=(session, session),
2642            current_level=(start_level, end_level),
2643            depcode=(self.context.code, self.context.code)
2644            )
2645        hitlist = []
2646        for student in students:
2647            hitlist.append(StudentQueryResultItem(student, view=self))
2648        return hitlist
2649
2650    def update(self, SHOW=None, session=None, level=None):
2651        datatable.need()
2652        self.parent_url = self.url(self.context.__parent__)
2653        self._set_session_values()
2654        self._set_level_values()
2655        self.hitlist = []
2656        self.session_default = session
2657        self.level_default = level
2658        if SHOW is not None:
2659            if session != 'all':
2660                self.session = int(session)
2661                self.session_string = '%s %s/%s' % (
2662                    self.session_label, self.session, self.session+1)
2663            else:
2664                self.session = None
2665                self.session_string = _('in any session')
2666            if level != 'all':
2667                self.level = int(level)
2668                self.level_string = '%s %s' % (self.level_label, self.level)
2669            else:
2670                self.level = None
2671                self.level_string = _('at any level')
2672            self.hitlist = self._searchCatalog(self.session, self.level)
2673            if not self.hitlist:
2674                self.flash(_('No student found.'))
2675        return
2676
2677class FilterStudentsInCertificatePage(FilterStudentsInDepartmentPage):
2678    """Page that filters and lists students.
2679    """
2680    grok.context(ICertificate)
2681
2682    def label(self):
[9819]2683        return 'Students studying %s' % self.context.longtitle()
[9806]2684
2685    def _searchCatalog(self, session, level):
2686        if level not in (10, 999, None):
2687            start_level = 100 * (level // 100)
2688            end_level = start_level + 90
2689        else:
2690            start_level = end_level = level
2691        cat = queryUtility(ICatalog, name='students_catalog')
2692        students = cat.searchResults(
2693            current_session=(session, session),
2694            current_level=(start_level, end_level),
2695            certcode=(self.context.code, self.context.code)
2696            )
2697        hitlist = []
2698        for student in students:
2699            hitlist.append(StudentQueryResultItem(student, view=self))
2700        return hitlist
2701
2702class FilterStudentsInCoursePage(FilterStudentsInDepartmentPage):
2703    """Page that filters and lists students.
2704    """
2705    grok.context(ICourse)
2706
2707    def label(self):
[9819]2708        return 'Students registered for %s' % self.context.longtitle()
[9806]2709
2710    def _searchCatalog(self, session, level):
2711        if level not in (10, 999, None):
2712            start_level = 100 * (level // 100)
2713            end_level = start_level + 90
2714        else:
2715            start_level = end_level = level
2716        cat = queryUtility(ICatalog, name='coursetickets_catalog')
2717        coursetickets = cat.searchResults(
2718            session=(session, session),
2719            level=(start_level, end_level),
2720            code=(self.context.code, self.context.code)
2721            )
2722        hitlist = []
2723        for ticket in coursetickets:
2724            # XXX: If students have registered the same courses twice
2725            # they will be listed twice.
2726            hitlist.append(StudentQueryResultItem(ticket.student, view=self))
2727        return hitlist
2728
[9813]2729class ExportJobContainerOverview(KofaPage):
[9835]2730    """Page that lists active student data export jobs and provides links
2731    to discard or download CSV files.
2732
[9797]2733    """
[9813]2734    grok.context(VirtualExportJobContainer)
[9797]2735    grok.require('waeup.showStudents')
2736    grok.name('index.html')
2737    grok.template('exportjobsindex')
[9813]2738    label = _('Student Data Exports')
[9797]2739    pnav = 1
2740
2741    def update(self, CREATE=None, DISCARD=None, job_id=None):
2742        if CREATE:
[9836]2743            self.redirect(self.url('@@exportconfig'))
[9797]2744            return
2745        if DISCARD and job_id:
2746            entry = self.context.entry_from_job_id(job_id)
2747            self.context.delete_export_entry(entry)
[9836]2748            ob_class = self.__implemented__.__name__.replace('waeup.kofa.','')
2749            self.context.logger.info(
2750                '%s - discarded: job_id=%s' % (ob_class, job_id))
[9819]2751            self.flash(_('Discarded export') + ' %s' % job_id)
[9822]2752        self.entries = doll_up(self, user=self.request.principal.id)
[9797]2753        return
2754
[9833]2755class ExportJobContainerJobConfig(KofaPage):
[9797]2756    """Page that configures a students export job.
[9833]2757
2758    This is a baseclass.
[9797]2759    """
[9833]2760    grok.baseclass()
[9836]2761    grok.name('exportconfig')
[9797]2762    grok.require('waeup.showStudents')
[9836]2763    grok.template('exportconfig')
[9833]2764    label = _('Configure student data export')
[9797]2765    pnav = 1
[9835]2766    redirect_target = ''
[9797]2767
2768    def _set_session_values(self):
2769        vocab_terms = academic_sessions_vocab.by_value.values()
2770        self.sessions = sorted(
2771            [(x.title, x.token) for x in vocab_terms], reverse=True)
[9819]2772        self.sessions += [(_('All Sessions'), 'all')]
[9797]2773        return
2774
2775    def _set_level_values(self):
2776        vocab_terms = course_levels.by_value.values()
2777        self.levels = sorted(
2778            [(x.title, x.token) for x in vocab_terms])
[9819]2779        self.levels += [(_('All Levels'), 'all')]
[9797]2780        return
2781
[9803]2782    def _set_mode_values(self):
2783        utils = getUtility(IKofaUtils)
[9838]2784        self.modes = sorted([(value, key) for key, value in
2785                      utils.STUDY_MODES_DICT.items()])
[9819]2786        self.modes +=[(_('All Modes'), 'all')]
[9803]2787        return
2788
[9804]2789    def _set_exporter_values(self):
2790        # We provide all student exporters, nothing else, yet.
2791        exporters = []
2792        for name in EXPORTER_NAMES:
2793            util = getUtility(ICSVExporter, name=name)
2794            exporters.append((util.title, name),)
2795        self.exporters = exporters
2796
[9833]2797    @property
2798    def depcode(self):
2799        return None
2800
[9842]2801    @property
2802    def certcode(self):
2803        return None
2804
[9804]2805    def update(self, START=None, session=None, level=None, mode=None,
2806               exporter=None):
[9797]2807        self._set_session_values()
2808        self._set_level_values()
[9803]2809        self._set_mode_values()
[9804]2810        self._set_exporter_values()
[9797]2811        if START is None:
2812            return
2813        if session == 'all':
2814            session=None
2815        if level == 'all':
2816            level = None
[9803]2817        if mode == 'all':
2818            mode = None
[9836]2819        job_id = self.context.start_export_job(exporter,
2820                                      self.request.principal.id,
[9797]2821                                      current_session=session,
2822                                      current_level=level,
[9803]2823                                      current_mode=mode,
[9842]2824                                      depcode=self.depcode,
2825                                      certcode=self.certcode)
[9836]2826        ob_class = self.__implemented__.__name__.replace('waeup.kofa.','')
2827        self.context.logger.info(
[9842]2828            '%s - exported: %s (%s, %s, %s, %s, %s), job_id=%s'
2829            % (ob_class, exporter, session, level, mode, self.depcode,
2830            self.certcode, job_id))
[9833]2831        self.flash(_('Export started for students with') +
2832                   ' current_session=%s, current_level=%s, study_mode=%s' % (
2833                   session, level, mode))
[9835]2834        self.redirect(self.url(self.redirect_target))
[9797]2835        return
2836
[9822]2837class ExportJobContainerDownload(ExportCSVView):
[9835]2838    """Page that downloads a students export csv file.
2839
[9797]2840    """
[9813]2841    grok.context(VirtualExportJobContainer)
[9797]2842    grok.require('waeup.showStudents')
[9833]2843
2844class DatacenterExportJobContainerJobConfig(ExportJobContainerJobConfig):
2845    """Page that configures a students export job in datacenter.
2846
2847    """
2848    grok.context(IDataCenter)
[9835]2849    redirect_target = '@@export'
[9833]2850
2851class DepartmentExportJobContainerJobConfig(ExportJobContainerJobConfig):
2852    """Page that configures a students export job in departments.
2853
2854    """
2855    grok.context(VirtualDepartmentExportJobContainer)
2856
2857    @property
2858    def depcode(self):
[9835]2859        return self.context.__parent__.code
[9842]2860
2861class CertificateExportJobContainerJobConfig(ExportJobContainerJobConfig):
2862    """Page that configures a students export job for certificates.
2863
2864    """
2865    grok.context(VirtualCertificateExportJobContainer)
[9843]2866    grok.template('exportconfig_certificate')
[9842]2867
2868    @property
2869    def certcode(self):
2870        return self.context.__parent__.code
[9843]2871
2872class CourseExportJobContainerJobConfig(ExportJobContainerJobConfig):
2873    """Page that configures a students export job for courses.
2874
2875    In contrast to department or certificate student data exports the
2876    coursetickets_catalog is searched here. Therefore the update
2877    method from the base class is customized.
2878    """
2879    grok.context(VirtualCourseExportJobContainer)
2880    grok.template('exportconfig_course')
2881
2882    def _set_exporter_values(self):
[9844]2883        # We provide only two exporters.
[9843]2884        exporters = []
[9844]2885        for name in ('students', 'coursetickets'):
[9843]2886            util = getUtility(ICSVExporter, name=name)
2887            exporters.append((util.title, name),)
2888        self.exporters = exporters
2889
2890    def update(self, START=None, session=None, level=None, mode=None,
2891               exporter=None):
2892        self._set_session_values()
2893        self._set_level_values()
2894        self._set_mode_values()
2895        self._set_exporter_values()
2896        if START is None:
2897            return
2898        if session == 'all':
2899            session=None
2900        if level == 'all':
2901            level = None
2902        job_id = self.context.start_export_job(exporter,
2903                                      self.request.principal.id,
2904                                      # Use a different catalog and
2905                                      # pass different keywords than
2906                                      # for the (default) students_catalog
[9845]2907                                      catalog='coursetickets',
[9843]2908                                      session=session,
2909                                      level=level,
2910                                      code=self.context.__parent__.code)
2911        ob_class = self.__implemented__.__name__.replace('waeup.kofa.','')
2912        self.context.logger.info(
2913            '%s - exported: %s (%s, %s, %s), job_id=%s'
2914            % (ob_class, exporter, session, level,
2915            self.context.__parent__.code, job_id))
2916        self.flash(_('Export started for course tickets with') +
2917                   ' level_session=%s, level=%s' % (
2918                   session, level))
2919        self.redirect(self.url(self.redirect_target))
2920        return
Note: See TracBrowser for help on using the repository browser.