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

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

Reorganize setPaymentDetails. Add test.

  • Property svn:keywords set to Id
File size: 3.8 KB
Line 
1## $Id: utils.py 10832 2013-12-10 07:40:18Z 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 application section.
19"""
20
21from time import time
22import grok
23from zope.component import getUtility
24from zope.catalog.interfaces import ICatalog
25from waeup.kofa.interfaces import MessageFactory as _
26from waeup.kofa.applicants.interfaces import IApplicantsUtils
27from waeup.kofa.applicants.workflow import (INITIALIZED,
28    STARTED, PAID, ADMITTED, NOT_ADMITTED, SUBMITTED, CREATED)
29
30class ApplicantsUtils(grok.GlobalUtility):
31    """A collection of parameters and methods subject to customization.
32    """
33    grok.implements(IApplicantsUtils)
34
35    APP_TYPES_DICT = {
36      'app': ['General Studies', 'APP'],
37      'special': ['Special Application', 'SPE'],
38      }
39
40    SEPARATORS_DICT = {
41      'form.course1': _(u'Desired Study Courses'),
42      'form.student_id': _(u'Process Data'),
43      }
44
45    def setPaymentDetails(self, container, payment, applicant):
46        """Set the payment data of an applicant.
47        """
48        timestamp = ("%d" % int(time()*10000))[1:]
49        session = str(container.year)
50        try:
51            session_config = grok.getSite()['configuration'][session]
52        except KeyError:
53            return _(u'Session configuration object is not available.')
54        payment.p_id = "p%s" % timestamp
55        payment.p_item = container.title
56        payment.p_session = container.year
57        if container.prefix.startswith('special'):
58            fee_name = applicant.special_application + '_fee'
59            payment.amount_auth = getattr(session_config, fee_name, 0.0)
60            payment.p_category = applicant.special_application
61            return
62        payment.p_category = 'application'
63        container_fee = container.application_fee
64        if container_fee:
65            payment.amount_auth = container_fee
66            return
67        payment.amount_auth = session_config.application_fee
68        return
69
70    def getApplicantsStatistics(self, container):
71        """Count applicants in containers.
72        """
73        state_stats = {INITIALIZED:0, STARTED:0, PAID:0, SUBMITTED:0,
74            ADMITTED:0, NOT_ADMITTED:0, CREATED:0}
75        cat = getUtility(ICatalog, name='applicants_catalog')
76        code = container.code
77        year = int(code[-4:])
78        target = code[:-4]
79        mxcode = target + str(year + 1)
80        for state in state_stats:
81            state_stats[state] = len(cat.searchResults(
82                state=(state, state),
83                applicant_id=(code, mxcode)))
84        return state_stats, None
85
86    def filterCertificates(self, context, resultset):
87        """Filter and sort certificates in AppCatCertificateSource.
88        """
89        resultlist = sorted(resultset, key=lambda value: value.code)
90        curr_course = context.course1
91        if curr_course is not None and curr_course not in resultlist:
92            # display also current course even if certificate has been removed
93            resultlist = [curr_course,] + resultlist
94        return resultlist
95
96    def getCertTitle(self, context, value):
97        """Compose the titles in AppCatCertificateSource.
98        """
99        return "%s - %s" % (value.code, value.title)
Note: See TracBrowser for help on using the repository browser.