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

Last change on this file since 10563 was 10531, checked in by Henrik Bettermann, 11 years ago

Allow signature tables in transcripts.

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