1 | ## $Id: utils.py 9730 2012-11-27 13:19:03Z 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 utilities for Kofa. |
---|
19 | """ |
---|
20 | import os |
---|
21 | import grok |
---|
22 | import string |
---|
23 | import pytz |
---|
24 | from random import SystemRandom as r |
---|
25 | from zope.i18n import translate |
---|
26 | from zope.interface import implements |
---|
27 | from waeup.kofa.interfaces import IKofaUtils |
---|
28 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
29 | from waeup.kofa.smtp import send_mail as send_mail_internally |
---|
30 | from waeup.kofa.utils.countries import COUNTRIES |
---|
31 | from waeup.kofa.utils.helpers import get_sorted_preferred |
---|
32 | |
---|
33 | def send_mail(from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config): |
---|
34 | """Wrapper for the real SMTP functionality in :mod:`waeup.kofa.smtp`. |
---|
35 | |
---|
36 | Merely here to stay compatible with lots of calls to this place. |
---|
37 | """ |
---|
38 | mail_id = send_mail_internally( |
---|
39 | from_name, from_addr, rcpt_name, rcpt_addr, |
---|
40 | subject, body, config) |
---|
41 | return True |
---|
42 | |
---|
43 | #: A list of phone prefixes (order num, country, prefix). |
---|
44 | #: Items with same order num will be sorted alphabetically. |
---|
45 | #: The lower the order num, the higher the precedence. |
---|
46 | INT_PHONE_PREFIXES = [ |
---|
47 | (99, _('Germany'), '49'), |
---|
48 | ( 1, _('Nigeria'), '234'), |
---|
49 | (99, _('U.S.'), '1'), |
---|
50 | ] |
---|
51 | |
---|
52 | def sorted_phone_prefixes(data = INT_PHONE_PREFIXES, request=None): |
---|
53 | """Sorted tuples of phone prefixes. |
---|
54 | |
---|
55 | Ordered as shown above and formatted for use in select boxes. |
---|
56 | |
---|
57 | If request is given, we'll try to translate all country names in |
---|
58 | order to sort alphabetically correctly. |
---|
59 | |
---|
60 | XXX: This is a function (and not a constant) as different |
---|
61 | languages might give different orders. This is not tested yet. |
---|
62 | |
---|
63 | XXX: If we really want to use alphabetic ordering here, we might |
---|
64 | think about caching results of translations. |
---|
65 | """ |
---|
66 | if request is not None: |
---|
67 | data = [ |
---|
68 | (x, translate(y, context=request), z) |
---|
69 | for x, y, z in data] |
---|
70 | return tuple([ |
---|
71 | ('%s (+%s)' % (x[1],x[2]), '+%s' % x[2]) |
---|
72 | for x in sorted(data) |
---|
73 | ]) |
---|
74 | |
---|
75 | class KofaUtils(grok.GlobalUtility): |
---|
76 | """A collection of parameters and methods subject to customization. |
---|
77 | |
---|
78 | """ |
---|
79 | grok.implements(IKofaUtils) |
---|
80 | # This the only place where we define the portal language |
---|
81 | # which is used for the translation of system messages |
---|
82 | # (e.g. object histories). |
---|
83 | PORTAL_LANGUAGE = 'en' |
---|
84 | |
---|
85 | PREFERRED_LANGUAGES_DICT = { |
---|
86 | 'en':(1, u'English'), |
---|
87 | 'fr':(2, u'Français'), |
---|
88 | 'de':(3, u'Deutsch'), |
---|
89 | 'ha':(4, u'Hausa'), |
---|
90 | 'yo':(5, u'Yoruba'), |
---|
91 | 'ig':(6, u'Igbo'), |
---|
92 | } |
---|
93 | |
---|
94 | #: A function to return |
---|
95 | @classmethod |
---|
96 | def sorted_phone_prefixes(cls, data=INT_PHONE_PREFIXES, request=None): |
---|
97 | return sorted_phone_prefixes(data, request) |
---|
98 | |
---|
99 | EXAM_SUBJECTS_DICT = { |
---|
100 | 'math': 'Mathematics', |
---|
101 | 'computer_science': 'Computer Science', |
---|
102 | } |
---|
103 | |
---|
104 | #: Exam grades. The tuple is sorted as it should be displayed in |
---|
105 | #: select boxes. |
---|
106 | EXAM_GRADES = ( |
---|
107 | ('A', 'Best'), |
---|
108 | ('B', 'Better'), |
---|
109 | ('C', 'Good'), |
---|
110 | ) |
---|
111 | |
---|
112 | INST_TYPES_DICT = { |
---|
113 | 'none': '', |
---|
114 | 'faculty': 'Faculty of', |
---|
115 | 'department': 'Department of', |
---|
116 | 'school': 'School of', |
---|
117 | 'office': 'Office for', |
---|
118 | 'centre': 'Centre for', |
---|
119 | 'institute': 'Institute of', |
---|
120 | 'school_for': 'School for', |
---|
121 | 'college': 'College of', |
---|
122 | } |
---|
123 | |
---|
124 | STUDY_MODES_DICT = { |
---|
125 | 'transfer': 'Transfer', |
---|
126 | 'ug_ft': 'Undergraduate Full-Time', |
---|
127 | 'ug_pt': 'Undergraduate Part-Time', |
---|
128 | 'pg_ft': 'Postgraduate Full-Time', |
---|
129 | 'pg_pt': 'Postgraduate Part-Time', |
---|
130 | } |
---|
131 | |
---|
132 | APP_CATS_DICT = { |
---|
133 | 'basic': 'Basic Application', |
---|
134 | 'no': 'no application', |
---|
135 | 'pg': 'Postgraduate', |
---|
136 | 'sandwich': 'Sandwich', |
---|
137 | 'cest': 'Part-Time, Diploma, Certificate' |
---|
138 | } |
---|
139 | |
---|
140 | SEMESTER_DICT = { |
---|
141 | 1: 'First Semester', |
---|
142 | 2: 'Second Semester', |
---|
143 | 3: 'Combined', |
---|
144 | 9: 'N/A' |
---|
145 | } |
---|
146 | |
---|
147 | SPECIAL_HANDLING_DICT = { |
---|
148 | 'regular': 'Regular Hostel', |
---|
149 | 'blocked': 'Blocked Hostel', |
---|
150 | 'pd': 'Postgraduate Hostel' |
---|
151 | } |
---|
152 | |
---|
153 | PAYMENT_CATEGORIES = { |
---|
154 | 'schoolfee': 'School Fee', |
---|
155 | 'clearance': 'Acceptance Fee', |
---|
156 | 'bed_allocation': 'Bed Allocation Fee', |
---|
157 | 'hostel_maintenance': 'Hostel Maintenance Fee', |
---|
158 | 'transfer': 'Transfer Fee', |
---|
159 | 'gown': 'Gown Hire Fee', |
---|
160 | 'application': 'Application Fee' |
---|
161 | } |
---|
162 | |
---|
163 | SELECTABLE_PAYMENT_CATEGORIES = PAYMENT_CATEGORIES |
---|
164 | |
---|
165 | MODE_GROUPS = { |
---|
166 | 'All':('all',), |
---|
167 | 'Undergraduate Full-Time':('ug_ft',), |
---|
168 | 'Undergraduate Part-Time':('ug_pt',), |
---|
169 | 'Postgraduate Full-Time':('pg_ft',), |
---|
170 | 'Postgraduate Part-Time':('pg_pt',), |
---|
171 | } |
---|
172 | |
---|
173 | def sendContactForm(self,from_name,from_addr,rcpt_name,rcpt_addr, |
---|
174 | from_username,usertype,portal,body,subject): |
---|
175 | """Send an email with data provided by forms. |
---|
176 | """ |
---|
177 | config = grok.getSite()['configuration'] |
---|
178 | text = _(u"""Fullname: ${a} |
---|
179 | User Id: ${b} |
---|
180 | User Type: ${c} |
---|
181 | Portal: ${d} |
---|
182 | |
---|
183 | ${e} |
---|
184 | """) |
---|
185 | text = _(text, |
---|
186 | mapping = { |
---|
187 | 'a':from_name, |
---|
188 | 'b':from_username, |
---|
189 | 'c':usertype, |
---|
190 | 'd':portal, |
---|
191 | 'e':body}) |
---|
192 | body = translate(text, 'waeup.kofa', |
---|
193 | target_language=self.PORTAL_LANGUAGE) |
---|
194 | if not (from_addr and rcpt_addr): |
---|
195 | return False |
---|
196 | return send_mail( |
---|
197 | from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config) |
---|
198 | |
---|
199 | @property |
---|
200 | def tzinfo(self): |
---|
201 | # For Nigeria: pytz.timezone('Africa/Lagos') |
---|
202 | # For Germany: pytz.timezone('Europe/Berlin') |
---|
203 | return pytz.utc |
---|
204 | |
---|
205 | def fullname(self,firstname,lastname,middlename=None): |
---|
206 | """Full name constructor. |
---|
207 | """ |
---|
208 | # We do not necessarily have the middlename attribute |
---|
209 | if middlename: |
---|
210 | name = '%s %s %s' % (firstname, middlename, lastname) |
---|
211 | else: |
---|
212 | name = '%s %s' % (firstname, lastname) |
---|
213 | return string.capwords(name.replace('-',' - ')).replace(' - ','-') |
---|
214 | |
---|
215 | |
---|
216 | def genPassword(self, length=8, chars=string.letters + string.digits): |
---|
217 | """Generate a random password. |
---|
218 | """ |
---|
219 | return ''.join([r().choice(chars) for i in range(length)]) |
---|
220 | |
---|
221 | |
---|
222 | def sendCredentials(self, user, password=None, url_info=None, msg=None): |
---|
223 | """Send credentials as email. |
---|
224 | |
---|
225 | Input is the applicant for which credentials are sent and the |
---|
226 | password. |
---|
227 | |
---|
228 | Returns True or False to indicate successful operation. |
---|
229 | """ |
---|
230 | subject = 'Your Kofa credentials' |
---|
231 | text = _(u"""Dear ${a}, |
---|
232 | |
---|
233 | ${b} |
---|
234 | Student Registration and Information Portal of |
---|
235 | ${c}. |
---|
236 | |
---|
237 | Your user name: ${d} |
---|
238 | Your password: ${e} |
---|
239 | ${f} |
---|
240 | |
---|
241 | Please remember your user name and keep |
---|
242 | your password secret! |
---|
243 | |
---|
244 | Please also note that passwords are case-sensitive. |
---|
245 | |
---|
246 | Regards |
---|
247 | """) |
---|
248 | config = grok.getSite()['configuration'] |
---|
249 | from_name = config.name_admin |
---|
250 | from_addr = config.email_admin |
---|
251 | rcpt_name = user.title |
---|
252 | rcpt_addr = user.email |
---|
253 | text = _(text, |
---|
254 | mapping = { |
---|
255 | 'a':rcpt_name, |
---|
256 | 'b':msg, |
---|
257 | 'c':config.name, |
---|
258 | 'd':user.name, |
---|
259 | 'e':password, |
---|
260 | 'f':url_info}) |
---|
261 | |
---|
262 | body = translate(text, 'waeup.kofa', |
---|
263 | target_language=self.PORTAL_LANGUAGE) |
---|
264 | return send_mail( |
---|
265 | from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config) |
---|