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

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

Register KofaUtils? in configure.zcml. Is that really necessary?

  • Property svn:keywords set to Id
File size: 6.1 KB
Line 
1## $Id: utils.py 7829 2012-03-09 22:22:23Z 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:
41    """A collection of parameters and methods subject to customization.
42
43    """
44    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 getInstTypeDict(self):
60        """Provide a dictionary of study modes.
61        """
62        return {
63        'faculty': 'Faculty of',
64        'department': 'Department of',
65        'school': 'School of',
66        'office': 'Office for',
67        'centre': 'Centre for',
68        'institute': 'Institute of',
69        'school_for': 'School for',
70        }
71
72    def getStudyModesDict(self):
73        """Provide a dictionary of study modes.
74        """
75        return {
76        'rmd_ft': 'Remedial with deficiencies',
77        'dp_pt': 'Diploma Part Time',
78        'ct_ft': 'Certificate Full Time',
79        'dp_ft': 'Diploma Full Time',
80        'de_pt': 'Direct Entry Part Time',
81        'pg_ft': 'Postgraduate Full Time',
82        'pg_pt': 'Postgraduate Part Time',
83        'jm_ft': 'Joint Matriculation Full Time',
84        'ume_ft': 'UME Full Time',
85        'de_ft': 'Direct Entry Full Time',
86        'ph_ft': 'Post Higher Education Full Time',
87        'transfer_pt': 'Transfer Part Time',
88        'ug_pt': 'Undergraduate Part Time',
89        'transfer_ft': 'Transfer Full Time',
90        'ct_pt': 'Certificate Part Time',
91        'ug_ft': 'Undergraduate Full Time',
92        'rm_ft': 'Remedial'
93        }
94
95    def getAppCatDict(self):
96        """Provide a dictionary of study modes.
97        """
98        return {
99        'basic': 'PUME, PDE, PCE, PRENCE',
100        'no': 'no application',
101        'pg': 'Postgraduate',
102        'sandwich': 'Sandwich',
103        'cest': 'Part-Time, Diploma, Certificate'
104        }
105
106    def getSemesterDict(self):
107        """Provide a dictionary of semester or trimester types.
108        """
109        return {
110        1: 'First Semester',
111        2: 'Second Semester',
112        3: 'Combined',
113        9: 'N/A'
114        }
115
116    def sendContactForm(self,from_name,from_addr,rcpt_name,rcpt_addr,
117                from_username,usertype,portal,body,subject):
118        """Send an email with data provided by forms.
119        """
120        config = grok.getSite()['configuration']
121        text = _(u"""Fullname: ${a}
122User Id: ${b}
123User Type: ${c}
124Portal: ${d}
125
126${e}
127""")
128        text = _(text,
129            mapping = {
130            'a':from_name,
131            'b':from_username,
132            'c':usertype,
133            'd':portal,
134            'e':body})
135        body = translate(text, 'waeup.kofa',
136            target_language=self.PORTAL_LANGUAGE)
137        return send_mail(
138            from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config)
139
140    def fullname(self,firstname,lastname,middlename=None):
141        """Full name constructor.
142        """
143        # We do not necessarily have the middlename attribute
144        if middlename:
145            return string.capwords(
146                '%s %s %s' % (firstname, middlename, lastname))
147        else:
148            return string.capwords(
149                '%s %s' % (firstname, lastname))
150
151    def genPassword(self, length=8, chars=string.letters + string.digits):
152        """Generate a random password.
153        """
154        return ''.join([r().choice(chars) for i in range(length)])
155
156
157    def sendCredentials(self, user, password=None, login_url=None, msg=None):
158        """Send credentials as email.
159
160        Input is the applicant for which credentials are sent and the
161        password.
162
163        Returns True or False to indicate successful operation.
164        """
165        subject = 'Your Kofa credentials'
166        text = _(u"""Dear ${a},
167
168${b}
169Student Registration and Information Portal of
170${c}.
171
172Your user name: ${d}
173Your password: ${e}
174Login page: ${f}
175
176Please remember your user name and keep
177your password secret!
178
179Please also note that passwords are case-sensitive.
180
181Regards
182""")
183        config = grok.getSite()['configuration']
184        from_name = config.name_admin
185        from_addr = config.email_admin
186        rcpt_name = user.title
187        rcpt_addr = user.email
188        text = _(text,
189            mapping = {
190            'a':rcpt_name,
191            'b':msg,
192            'c':config.name,
193            'd':user.name,
194            'e':password,
195            'f':login_url})
196
197        body = translate(text, 'waeup.kofa',
198            target_language=self.PORTAL_LANGUAGE)
199        return send_mail(
200            from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config)
Note: See TracBrowser for help on using the repository browser.