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

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

We don't need methods to fetch dictionaries.

Update interfaces.

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