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

Last change on this file since 9800 was 9797, checked in by uli, 12 years ago

Local exports for departments.

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