source: main/waeup.kofa/trunk/src/waeup/kofa/applicants/utils.py @ 17009

Last change on this file since 17009 was 17009, checked in by Henrik Bettermann, 2 years ago

Do not require session configuration object for application payments.

  • Property svn:keywords set to Id
File size: 5.4 KB
Line 
1## $Id: utils.py 17009 2022-07-08 06:11:37Z henrik $
2##
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"""General helper functions and utilities for the applicants section.
19"""
20
21from time import time
22from datetime import datetime
23import grok
24from zope.component import getUtility
25from zope.catalog.interfaces import ICatalog
26from waeup.kofa.interfaces import MessageFactory as _
27from waeup.kofa.applicants.interfaces import IApplicantsUtils
28from waeup.kofa.applicants.workflow import (INITIALIZED,
29    STARTED, PAID, ADMITTED, NOT_ADMITTED, SUBMITTED, CREATED, PROCESSED)
30
31class ApplicantsUtils(grok.GlobalUtility):
32    """A collection of parameters and methods subject to customization.
33    """
34    grok.implements(IApplicantsUtils)
35
36    #: A dictionary containing application type names, titles and
37    #: access code prefixes (meanwhile deprecated).
38    APP_TYPES_DICT = {
39      'app': ['General Studies', 'APP'],
40      'special': ['Special Application', 'SPE'],
41      }
42
43    #: A dictionary which maps widget names to headlines.
44    #: The headline is rendered in forms and on pdf slips above the
45    #: respective display or input widget.
46    SEPARATORS_DICT = {
47      'form.applicant_id': _(u'Base Data'),
48      'form.course1': _(u'Desired Study Courses'),
49      'form.notice': _(u'Application Process Data'),
50      'form.referees': _(u'Referees (automatically invited by email '
51                          'after final submission of this form)'),
52      }
53
54    #: A tuple of tuple of file names to be uploaded by applicants and copied
55    #: over to the students section.
56    ADDITIONAL_FILES = (('Test File','testfile'),)
57
58    def setPaymentDetails(self, container, payment, applicant):
59        """Set the payment data of an applicant.
60        In contrast to its `StudentsUtils` counterpart, the payment ticket
61        must exist and is an argument of this method.
62        """
63        timestamp = ("%d" % int(time()*10000))[1:]
64        payment.p_id = "p%s" % timestamp
65        payment.p_item = container.title
66        if container.year:
67            payment.p_session = container.year
68        else:
69            payment.p_session = datetime.now().year
70        payment.amount_auth = 0.0
71        if applicant.special:
72            if applicant.special_application:
73                try:
74                    session_config = grok.getSite()['configuration'][
75                        str(payment.p_session)]
76                except KeyError:
77                    return _(u'Session configuration object is not available.')   
78                fee_name = applicant.special_application + '_fee'
79                payment.amount_auth = getattr(session_config, fee_name, None)
80                payment.p_category = applicant.special_application
81            if payment.amount_auth in (0.0, None):
82                return _('Amount could not be determined. Have you saved the form?')
83            return
84        payment.p_category = 'application'
85        container_fee = container.application_fee
86        if not container_fee:
87            return _('Amount could not be determined.')
88        payment.amount_auth = container_fee
89        return
90
91    def getApplicantsStatistics(self, container):
92        """Count applicants in applicants containers.
93        """
94        state_stats = {INITIALIZED:0, STARTED:0, PAID:0, SUBMITTED:0,
95            ADMITTED:0, NOT_ADMITTED:0, CREATED:0, PROCESSED:0}
96        cat = getUtility(ICatalog, name='applicants_catalog')
97        code = container.code
98        for state in state_stats:
99            if state == 'initialized':
100                results = cat.searchResults(
101                                state=(state, state),
102                                container_code=(code + '+', code + '+'))
103                state_stats[state] = len(results)
104            else:
105                results = cat.searchResults(
106                    state=(state, state),
107                    container_code=(code + '+', code + '-'))
108                state_stats[state] = len(results)
109        return state_stats, None
110
111    def sortCertificates(self, context, resultset):
112        """Sort already filtered certificates in `AppCatCertificateSource`.
113        Display also current course even if certificate in the academic
114        section has been removed.
115        """
116        resultlist = sorted(resultset, key=lambda value: value.code)
117        curr_course = context.course1
118        if curr_course is not None and curr_course not in resultlist:
119            resultlist = [curr_course,] + resultlist
120        return resultlist
121
122    def getCertTitle(self, context, value):
123        """Compose the titles in `AppCatCertificateSource`.
124        """
125        return "%s - %s" % (value.code, value.title)
126
127    def isPictureEditable(self, container):
128        """False if applicants are not allowed to edit uploaded pictures.
129        """
130        return container.with_picture
Note: See TracBrowser for help on using the repository browser.