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

Last change on this file since 11737 was 11732, checked in by Henrik Bettermann, 10 years ago

Start export job without payment date parameters if not selected.

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