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

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

Add doclinks.

Add warning to Certificates section.

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