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

Last change on this file since 7836 was 7836, checked in by Henrik Bettermann, 13 years ago

Let's KofaUtils? provide dictionaries for exam subjects and grades.

  • Property svn:keywords set to Id
File size: 10.7 KB
Line 
1## $Id: utils.py 7836 2012-03-11 15:59:34Z 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"""
20import os
21import grok
22import string
23from zope.i18n import translate
24from zope.interface import implements
25from random import SystemRandom as r
26from waeup.kofa.interfaces import IKofaUtils
27from waeup.kofa.interfaces import MessageFactory as _
28from waeup.kofa.smtp import send_mail as send_mail_internally
29
30def 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
40class 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    def getExamSubjectsDict(self):
60        """Provide a dictionary of exam subjects.
61        """
62        return {
63            'accounts': 'Accounts',
64            'add_general_science': 'Add General Science',
65            'add_maths': 'Add Maths',
66            'agricultural_science': 'Agricultural Science',
67            'applied_electricity': 'Applied Electricity',
68            'arabi': 'Arabic',
69            'arithmetics': 'Arithmetics',
70            'art': 'Art',
71            'auto_mechanics': 'Auto Mechanics',
72            'basic_electricity': 'Basic Electricity',
73            'bible_knowledge': 'Bible Knowledge',
74            'biology': 'Biology',
75            'biology_alt_syl': 'Biology (Alt.Syl)',
76            'book_keeping': 'Book Keeping',
77            'building_construction': 'Building Construction',
78            'business_management': 'Business Management',
79            'business_construction_management': 'Business/Construction Management (Adv)',
80            'chemistry': 'Chemistry',
81            'chemistry_alt_syl': 'Chemistry (Alt.Syl)',
82            'christian_religious_studies': 'Christian Religious Studies',
83            'clerical_office_duties': 'Clerical Office Duties',
84            'clothing_and_textiles': 'Clothing and Textiles',
85            'commerce': 'Commerce',
86            'economics': 'Economics',
87            'education': 'Education',
88            'efik': 'Efik',
89            'electronics': 'Electronics',
90            'elementary_surveying': 'Elementary Surveying',
91            'english_language': 'English Language',
92            'engineering_science': 'Engineering Science',
93            'financial_accounting': 'Financial Accounting',
94            'food_and_nutrition': 'Food and Nutrition',
95            'french': 'French',
96            'further_mathematics': 'Further Mathematics',
97            'general_science': 'General Science',
98            'geography': 'Geography',
99            'german': 'German',
100            'government': 'Government',
101            'hausa': 'Hausa',
102            'hausa_literature': 'Hausa Literature',
103            'health_science': 'Health Science',
104            'history': 'History',
105            'home_management': 'Home Management',
106            'industrial_electrical_installation': 'Industrial Elect Installation (Adv)',
107            'intergrated_science': 'Intergrated Science',
108            'islamic_studies': 'Islamic Studies',
109            'literature_in_english': 'Literature in English',
110            'literature_in_nigerian_languages': 'Literature in Nigerian Languages',
111            'igbo': 'Igbo',
112            'igbo_literature': 'Igbo Literature',
113            'yoruba': 'Yoruba',
114            'yoruba_literature': 'Yoruba Literature',
115            'management_in_living': 'Management in Living',
116            'mathematics': 'Mathematics',
117            'metalwork': 'Metalwork',
118            'music': 'Music',
119            'nigerian_language': 'Nigerian Language',
120            'office_practice': 'Office Practice',
121            'physical_education': 'Physical Education',
122            'physical_health_education': 'Physical and Health Education',
123            'physics': 'Physics',
124            'physics_alt_syl': 'Physics (Alt.Syl)',
125            'principles_of_cost_accounting': 'Principles of Cost Accounting',
126            'rural_science': 'Rural Science',
127            'science': 'Science',
128            'secretarial_duties': 'Secretarial Duties',
129            'shorthand': 'Shorthand',
130            'sierra_leone_studies': 'Sierra Leone Studies',
131            'sierra_leone_languages': 'Sierra Leone Languages',
132            'social_studies': 'Social Studies',
133            'statistics': 'Statistics',
134            'teaching_practice': 'Teaching Practice',
135            'technical_drawing': 'Technical Drawing',
136            'typewriting': 'Typewriting',
137            'visual_art': 'Visual Art',
138            'woodwork': 'Woodwork',
139            'winding_elect_machines': 'Winding of Elect Machines and Elect Eng Sc'
140            }
141
142    def getExamGradesDict(self):
143        """Provide a dictionary of exam grades.
144        """
145        return {
146            'A1': 'Excellent (A1)',
147            'A2': 'Very Good (A2)',
148            'A3': 'Good (A3)',
149            'B2': 'Very Good (B2)',
150            'B3': 'Good (B3)',
151            'C4': 'Credit (C4)',
152            'C5': 'Credit (C5)',
153            'C6': 'Credit (C6)',
154            'D7': 'Pass (D7)',
155            'E8': 'Pass (E8)',
156            'F9': 'Fail (F9)',
157            'Aa': 'A (a)',
158            'Bb': 'B (b)',
159            'Cc': 'C (c)',
160            'Dd': 'D (d)',
161            'Ee': 'E (e)',
162            'Ff': 'F (f)',
163            'Gg': 'G (g)'
164            }
165
166    def getInstTypeDict(self):
167        """Provide a dictionary of study modes.
168        """
169        return {
170        'faculty': 'Faculty of',
171        'department': 'Department of',
172        'school': 'School of',
173        'office': 'Office for',
174        'centre': 'Centre for',
175        'institute': 'Institute of',
176        'school_for': 'School for',
177        }
178
179    def getStudyModesDict(self):
180        """Provide a dictionary of study modes.
181        """
182        return {
183        'rmd_ft': 'Remedial with deficiencies',
184        'dp_pt': 'Diploma Part Time',
185        'ct_ft': 'Certificate Full Time',
186        'dp_ft': 'Diploma Full Time',
187        'de_pt': 'Direct Entry Part Time',
188        'pg_ft': 'Postgraduate Full Time',
189        'pg_pt': 'Postgraduate Part Time',
190        'jm_ft': 'Joint Matriculation Full Time',
191        'ume_ft': 'UME Full Time',
192        'de_ft': 'Direct Entry Full Time',
193        'ph_ft': 'Post Higher Education Full Time',
194        'transfer_pt': 'Transfer Part Time',
195        'ug_pt': 'Undergraduate Part Time',
196        'transfer_ft': 'Transfer Full Time',
197        'ct_pt': 'Certificate Part Time',
198        'ug_ft': 'Undergraduate Full Time',
199        'rm_ft': 'Remedial'
200        }
201
202    def getAppCatDict(self):
203        """Provide a dictionary of study modes.
204        """
205        return {
206        'basic': 'PUME, PDE, PCE, PRENCE',
207        'no': 'no application',
208        'pg': 'Postgraduate',
209        'sandwich': 'Sandwich',
210        'cest': 'Part-Time, Diploma, Certificate'
211        }
212
213    def getSemesterDict(self):
214        """Provide a dictionary of semester or trimester types.
215        """
216        return {
217        1: 'First Semester',
218        2: 'Second Semester',
219        3: 'Combined',
220        9: 'N/A'
221        }
222
223    def sendContactForm(self,from_name,from_addr,rcpt_name,rcpt_addr,
224                from_username,usertype,portal,body,subject):
225        """Send an email with data provided by forms.
226        """
227        config = grok.getSite()['configuration']
228        text = _(u"""Fullname: ${a}
229User Id: ${b}
230User Type: ${c}
231Portal: ${d}
232
233${e}
234""")
235        text = _(text,
236            mapping = {
237            'a':from_name,
238            'b':from_username,
239            'c':usertype,
240            'd':portal,
241            'e':body})
242        body = translate(text, 'waeup.kofa',
243            target_language=self.PORTAL_LANGUAGE)
244        return send_mail(
245            from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config)
246
247    def fullname(self,firstname,lastname,middlename=None):
248        """Full name constructor.
249        """
250        # We do not necessarily have the middlename attribute
251        if middlename:
252            return string.capwords(
253                '%s %s %s' % (firstname, middlename, lastname))
254        else:
255            return string.capwords(
256                '%s %s' % (firstname, lastname))
257
258    def genPassword(self, length=8, chars=string.letters + string.digits):
259        """Generate a random password.
260        """
261        return ''.join([r().choice(chars) for i in range(length)])
262
263
264    def sendCredentials(self, user, password=None, login_url=None, msg=None):
265        """Send credentials as email.
266
267        Input is the applicant for which credentials are sent and the
268        password.
269
270        Returns True or False to indicate successful operation.
271        """
272        subject = 'Your Kofa credentials'
273        text = _(u"""Dear ${a},
274
275${b}
276Student Registration and Information Portal of
277${c}.
278
279Your user name: ${d}
280Your password: ${e}
281Login page: ${f}
282
283Please remember your user name and keep
284your password secret!
285
286Please also note that passwords are case-sensitive.
287
288Regards
289""")
290        config = grok.getSite()['configuration']
291        from_name = config.name_admin
292        from_addr = config.email_admin
293        rcpt_name = user.title
294        rcpt_addr = user.email
295        text = _(text,
296            mapping = {
297            'a':rcpt_name,
298            'b':msg,
299            'c':config.name,
300            'd':user.name,
301            'e':password,
302            'f':login_url})
303
304        body = translate(text, 'waeup.kofa',
305            target_language=self.PORTAL_LANGUAGE)
306        return send_mail(
307            from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config)
Note: See TracBrowser for help on using the repository browser.