1 | ## $Id: utils.py 13133 2015-07-02 10:06:52Z 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 | |
---|
21 | from time import time |
---|
22 | import grok |
---|
23 | from zope.component import getUtility |
---|
24 | from zope.catalog.interfaces import ICatalog |
---|
25 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
26 | from waeup.kofa.applicants.interfaces import IApplicantsUtils |
---|
27 | from waeup.kofa.applicants.workflow import (INITIALIZED, |
---|
28 | STARTED, PAID, ADMITTED, NOT_ADMITTED, SUBMITTED, CREATED) |
---|
29 | |
---|
30 | class ApplicantsUtils(grok.GlobalUtility): |
---|
31 | """A collection of parameters and methods subject to customization. |
---|
32 | """ |
---|
33 | grok.implements(IApplicantsUtils) |
---|
34 | |
---|
35 | #: A dictionary containing application type names, titles and |
---|
36 | #: access code prefixes (meanwhile deprecated). |
---|
37 | APP_TYPES_DICT = { |
---|
38 | 'app': ['General Studies', 'APP'], |
---|
39 | 'special': ['Special Application', 'SPE'], |
---|
40 | } |
---|
41 | |
---|
42 | #: A dictionary which maps widget names to headlines. |
---|
43 | #: The headline is rendered in forms and on pdf slips above the |
---|
44 | #: respective display or input widget. |
---|
45 | SEPARATORS_DICT = { |
---|
46 | 'form.course1': _(u'Desired Study Courses'), |
---|
47 | 'form.student_id': _(u'Process Data'), |
---|
48 | } |
---|
49 | |
---|
50 | def setPaymentDetails(self, container, payment, applicant): |
---|
51 | """Set the payment data of an applicant. |
---|
52 | In contrast to its `StudentsUtils` counterpart, the payment ticket |
---|
53 | must exist and is an argument of this method. |
---|
54 | """ |
---|
55 | timestamp = ("%d" % int(time()*10000))[1:] |
---|
56 | session = str(container.year) |
---|
57 | try: |
---|
58 | session_config = grok.getSite()['configuration'][session] |
---|
59 | except KeyError: |
---|
60 | return _(u'Session configuration object is not available.') |
---|
61 | payment.p_id = "p%s" % timestamp |
---|
62 | payment.p_item = container.title |
---|
63 | payment.p_session = container.year |
---|
64 | payment.amount_auth = 0.0 |
---|
65 | if applicant.special: |
---|
66 | if applicant.special_application: |
---|
67 | fee_name = applicant.special_application + '_fee' |
---|
68 | payment.amount_auth = getattr(session_config, fee_name, None) |
---|
69 | if payment.amount_auth in (0.0, None): |
---|
70 | return _('Amount could not be determined.') |
---|
71 | payment.p_category = applicant.special_application |
---|
72 | return |
---|
73 | payment.p_category = 'application' |
---|
74 | container_fee = container.application_fee |
---|
75 | if container_fee: |
---|
76 | payment.amount_auth = container_fee |
---|
77 | return |
---|
78 | payment.amount_auth = session_config.application_fee |
---|
79 | if payment.amount_auth in (0.0, None): |
---|
80 | return _('Amount could not be determined.') |
---|
81 | return |
---|
82 | |
---|
83 | def getApplicantsStatistics(self, container): |
---|
84 | """Count applicants in applicants containers. |
---|
85 | """ |
---|
86 | state_stats = {INITIALIZED:0, STARTED:0, PAID:0, SUBMITTED:0, |
---|
87 | ADMITTED:0, NOT_ADMITTED:0, CREATED:0} |
---|
88 | cat = getUtility(ICatalog, name='applicants_catalog') |
---|
89 | code = container.code |
---|
90 | year = int(code[-4:]) |
---|
91 | target = code[:-4] |
---|
92 | mxcode = target + str(year + 1) |
---|
93 | for state in state_stats: |
---|
94 | state_stats[state] = len(cat.searchResults( |
---|
95 | state=(state, state), |
---|
96 | applicant_id=(code, mxcode))) |
---|
97 | return state_stats, None |
---|
98 | |
---|
99 | def sortCertificates(self, context, resultset): |
---|
100 | """Sort already filtered certificates in `AppCatCertificateSource`. |
---|
101 | Display also current course even if certificate in the academic |
---|
102 | section has been removed. |
---|
103 | """ |
---|
104 | resultlist = sorted(resultset, key=lambda value: value.code) |
---|
105 | curr_course = context.course1 |
---|
106 | if curr_course is not None and curr_course not in resultlist: |
---|
107 | resultlist = [curr_course,] + resultlist |
---|
108 | return resultlist |
---|
109 | |
---|
110 | def getCertTitle(self, context, value): |
---|
111 | """Compose the titles in `AppCatCertificateSource`. |
---|
112 | """ |
---|
113 | return "%s - %s" % (value.code, value.title) |
---|