1 | ## $Id: utils.py 7871 2012-03-13 09:15:00Z 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 | from zope.i18n import translate |
---|
24 | from zope.interface import implements |
---|
25 | from random import SystemRandom as r |
---|
26 | from waeup.kofa.interfaces import IKofaUtils |
---|
27 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
28 | from waeup.kofa.smtp import send_mail as send_mail_internally |
---|
29 | |
---|
30 | def send_mail(from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config): |
---|
31 | """Wrapper for the real SMTP functionality in :mod:`waeup.kofa.smtp`. |
---|
32 | |
---|
33 | Merely here to stay compatible with lots of calls to this place. |
---|
34 | """ |
---|
35 | mail_id = send_mail_internally( |
---|
36 | from_name, from_addr, rcpt_name, rcpt_addr, |
---|
37 | subject, body, config) |
---|
38 | return True |
---|
39 | |
---|
40 | class KofaUtils(grok.GlobalUtility): |
---|
41 | """A collection of parameters and methods subject to customization. |
---|
42 | |
---|
43 | """ |
---|
44 | grok.implements(IKofaUtils) |
---|
45 | # This the only place where we define the portal language |
---|
46 | # which is used for the translation of system messages |
---|
47 | # (e.g. object histories). |
---|
48 | PORTAL_LANGUAGE = 'en' |
---|
49 | |
---|
50 | PREFERRED_LANGUAGES_DICT = { |
---|
51 | 'en':(1, u'English'), |
---|
52 | 'fr':(2, u'Français'), |
---|
53 | 'de':(3, u'Deutsch'), |
---|
54 | 'ha':(4, u'Hausa'), |
---|
55 | 'yo':(5, u'Yoruba'), |
---|
56 | 'ig':(6, u'Igbo'), |
---|
57 | } |
---|
58 | |
---|
59 | INT_PHONE_PREFIXES = { |
---|
60 | _('Germany'): (2, '49'), |
---|
61 | _('Nigeria'): (1, '234'), |
---|
62 | _('U.S.'): (3, '1'), |
---|
63 | } |
---|
64 | |
---|
65 | EXAM_SUBJECTS_DICT = { |
---|
66 | 'math': 'Mathematics', |
---|
67 | 'computer_science': 'Computer Science', |
---|
68 | } |
---|
69 | |
---|
70 | EXAM_GRADES_DICT = { |
---|
71 | 'A': (1, 'Best'), |
---|
72 | 'B': (2, 'Better'), |
---|
73 | 'C': (3, 'Good'), |
---|
74 | } |
---|
75 | |
---|
76 | INST_TYPES_DICT = { |
---|
77 | 'faculty': 'Faculty of', |
---|
78 | 'department': 'Department of', |
---|
79 | 'school': 'School of', |
---|
80 | 'office': 'Office for', |
---|
81 | 'centre': 'Centre for', |
---|
82 | 'institute': 'Institute of', |
---|
83 | 'school_for': 'School for', |
---|
84 | } |
---|
85 | |
---|
86 | STUDY_MODES_DICT = { |
---|
87 | 'ug_ft': 'Undergraduate Full-Time', |
---|
88 | 'ug_pt': 'Undergraduate Part-Time', |
---|
89 | } |
---|
90 | |
---|
91 | APP_CATS_DICT = { |
---|
92 | 'basic': 'Basic Application', |
---|
93 | 'no': 'no application', |
---|
94 | 'pg': 'Postgraduate', |
---|
95 | 'sandwich': 'Sandwich', |
---|
96 | 'cest': 'Part-Time, Diploma, Certificate' |
---|
97 | } |
---|
98 | |
---|
99 | SEMESTER_DICT = { |
---|
100 | 1: 'First Semester', |
---|
101 | 2: 'Second Semester', |
---|
102 | 3: 'Combined', |
---|
103 | 9: 'N/A' |
---|
104 | } |
---|
105 | |
---|
106 | def sendContactForm(self,from_name,from_addr,rcpt_name,rcpt_addr, |
---|
107 | from_username,usertype,portal,body,subject): |
---|
108 | """Send an email with data provided by forms. |
---|
109 | """ |
---|
110 | config = grok.getSite()['configuration'] |
---|
111 | text = _(u"""Fullname: ${a} |
---|
112 | User Id: ${b} |
---|
113 | User Type: ${c} |
---|
114 | Portal: ${d} |
---|
115 | |
---|
116 | ${e} |
---|
117 | """) |
---|
118 | text = _(text, |
---|
119 | mapping = { |
---|
120 | 'a':from_name, |
---|
121 | 'b':from_username, |
---|
122 | 'c':usertype, |
---|
123 | 'd':portal, |
---|
124 | 'e':body}) |
---|
125 | body = translate(text, 'waeup.kofa', |
---|
126 | target_language=self.PORTAL_LANGUAGE) |
---|
127 | return send_mail( |
---|
128 | from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config) |
---|
129 | |
---|
130 | def fullname(self,firstname,lastname,middlename=None): |
---|
131 | """Full name constructor. |
---|
132 | """ |
---|
133 | # We do not necessarily have the middlename attribute |
---|
134 | if middlename: |
---|
135 | return string.capwords( |
---|
136 | '%s %s %s' % (firstname, middlename, lastname)) |
---|
137 | else: |
---|
138 | return string.capwords( |
---|
139 | '%s %s' % (firstname, lastname)) |
---|
140 | |
---|
141 | def genPassword(self, length=8, chars=string.letters + string.digits): |
---|
142 | """Generate a random password. |
---|
143 | """ |
---|
144 | return ''.join([r().choice(chars) for i in range(length)]) |
---|
145 | |
---|
146 | |
---|
147 | def sendCredentials(self, user, password=None, login_url=None, msg=None): |
---|
148 | """Send credentials as email. |
---|
149 | |
---|
150 | Input is the applicant for which credentials are sent and the |
---|
151 | password. |
---|
152 | |
---|
153 | Returns True or False to indicate successful operation. |
---|
154 | """ |
---|
155 | subject = 'Your Kofa credentials' |
---|
156 | text = _(u"""Dear ${a}, |
---|
157 | |
---|
158 | ${b} |
---|
159 | Student Registration and Information Portal of |
---|
160 | ${c}. |
---|
161 | |
---|
162 | Your user name: ${d} |
---|
163 | Your password: ${e} |
---|
164 | Login page: ${f} |
---|
165 | |
---|
166 | Please remember your user name and keep |
---|
167 | your password secret! |
---|
168 | |
---|
169 | Please also note that passwords are case-sensitive. |
---|
170 | |
---|
171 | Regards |
---|
172 | """) |
---|
173 | config = grok.getSite()['configuration'] |
---|
174 | from_name = config.name_admin |
---|
175 | from_addr = config.email_admin |
---|
176 | rcpt_name = user.title |
---|
177 | rcpt_addr = user.email |
---|
178 | text = _(text, |
---|
179 | mapping = { |
---|
180 | 'a':rcpt_name, |
---|
181 | 'b':msg, |
---|
182 | 'c':config.name, |
---|
183 | 'd':user.name, |
---|
184 | 'e':password, |
---|
185 | 'f':login_url}) |
---|
186 | |
---|
187 | body = translate(text, 'waeup.kofa', |
---|
188 | target_language=self.PORTAL_LANGUAGE) |
---|
189 | return send_mail( |
---|
190 | from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config) |
---|