source: main/waeup.sirp/trunk/src/waeup/sirp/utils/utils.py @ 11107

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

Switch back to English.

  • Property svn:keywords set to Id
File size: 6.1 KB
Line 
1## $Id: utils.py 7744 2012-03-01 22:00: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 SIRP.
19"""
20import os
21import grok
22import string
23from zope.i18n import translate
24from random import SystemRandom as r
25from waeup.sirp.interfaces import ISIRPUtils
26from waeup.sirp.interfaces import MessageFactory as _
27from waeup.sirp.smtp import send_mail as send_mail_internally
28
29def send_mail(from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config):
30    """Wrapper for the real SMTP functionality in :mod:`waeup.sirp.smtp`.
31
32    Merely here to stay compatible with lots of calls to this place.
33    """
34    mail_id = send_mail_internally(
35        from_name, from_addr, rcpt_name, rcpt_addr,
36        subject, body, config)
37    return True
38
39class SIRPUtils(grok.GlobalUtility):
40    """A collection of parameters and methods subject to customization.
41    """
42    grok.implements(ISIRPUtils)
43    # This the only place where we define the portal language
44    # which is used for the translation of system messages
45    # (e.g. object histories).
46    PORTAL_LANGUAGE = 'en'
47
48    PREFERRED_LANGUAGES_DICT = {
49        'en':(1, u'English'),
50        'fr':(2, u'Français'),
51        'de':(3, u'Deutsch'),
52        'ha':(4, u'Hausa'),
53        'yo':(5, u'Yoruba'),
54        'ig':(6, u'Igbo'),
55        }
56
57    def getInstTypeDict(self):
58        """Provide a dictionary of study modes.
59        """
60        return {
61        'faculty': 'Faculty of',
62        'department': 'Department of',
63        'school': 'School of',
64        'office': 'Office for',
65        'centre': 'Centre for',
66        'institute': 'Institute of',
67        'school_for': 'School for',
68        }
69
70    def getStudyModesDict(self):
71        """Provide a dictionary of study modes.
72        """
73        return {
74        'rmd_ft': 'Remedial with deficiencies',
75        'dp_pt': 'Diploma Part Time',
76        'ct_ft': 'Certificate Full Time',
77        'dp_ft': 'Diploma Full Time',
78        'de_pt': 'Direct Entry Part Time',
79        'pg_ft': 'Postgraduate Full Time',
80        'pg_pt': 'Postgraduate Part Time',
81        'jm_ft': 'Joint Matriculation Full Time',
82        'ume_ft': 'UME Full Time',
83        'de_ft': 'Direct Entry Full Time',
84        'ph_ft': 'Post Higher Education Full Time',
85        'transfer_pt': 'Transfer Part Time',
86        'ug_pt': 'Undergraduate Part Time',
87        'transfer_ft': 'Transfer Full Time',
88        'ct_pt': 'Certificate Part Time',
89        'ug_ft': 'Undergraduate Full Time',
90        'rm_ft': 'Remedial'
91        }
92
93    def getAppCatDict(self):
94        """Provide a dictionary of study modes.
95        """
96        return {
97        'basic': 'PUME, PDE, PCE, PRENCE',
98        'no': 'no application',
99        'pg': 'Postgraduate',
100        'sandwich': 'Sandwich',
101        'cest': 'Part-Time, Diploma, Certificate'
102        }
103
104    def getSemesterDict(self):
105        """Provide a dictionary of semester or trimester types.
106        """
107        return {
108        1: 'First Semester',
109        2: 'Second Semester',
110        3: 'Combined',
111        9: 'N/A'
112        }
113
114    def sendContactForm(self,from_name,from_addr,rcpt_name,rcpt_addr,
115                from_username,usertype,portal,body,subject):
116        """Send an email with data provided by forms.
117        """
118        config = grok.getSite()['configuration']
119        text = _(u"""Fullname: ${a}
120User Id: ${b}
121User Type: ${c}
122Portal: ${d}
123
124${e}
125""")
126        text = _(text,
127            mapping = {
128            'a':from_name,
129            'b':from_username,
130            'c':usertype,
131            'd':portal,
132            'e':body})
133        body = translate(text, 'waeup.sirp',
134            target_language=self.PORTAL_LANGUAGE)
135        return send_mail(
136            from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config)
137
138    def fullname(self,firstname,lastname,middlename=None):
139        """Full name constructor.
140        """
141        # We do not necessarily have the middlename attribute
142        if middlename:
143            return string.capwords(
144                '%s %s %s' % (firstname, middlename, lastname))
145        else:
146            return string.capwords(
147                '%s %s' % (firstname, lastname))
148
149    def genPassword(self, length=8, chars=string.letters + string.digits):
150        """Generate a random password.
151        """
152        return ''.join([r().choice(chars) for i in range(length)])
153
154
155    def sendCredentials(self, user, password=None, login_url=None, msg=None):
156        """Send credentials as email.
157
158        Input is the applicant for which credentials are sent and the
159        password.
160
161        Returns True or False to indicate successful operation.
162        """
163        subject = 'Your SIRP credentials'
164        text = _(u"""Dear ${a},
165
166${b}
167Student Registration and Information Portal of
168${c}.
169
170Your user name: ${d}
171Your password: ${e}
172Login page: ${f}
173
174Please remember your user name and keep
175your password secret!
176
177Please also note that passwords are case-sensitive.
178
179Regards
180""")
181        config = grok.getSite()['configuration']
182        from_name = config.name_admin
183        from_addr = config.email_admin
184        rcpt_name = user.title
185        rcpt_addr = user.email
186        text = _(text,
187            mapping = {
188            'a':rcpt_name,
189            'b':msg,
190            'c':config.name,
191            'd':user.name,
192            'e':password,
193            'f':login_url})
194
195        body = translate(text, 'waeup.sirp',
196            target_language=self.PORTAL_LANGUAGE)
197        return send_mail(
198            from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config)
Note: See TracBrowser for help on using the repository browser.