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

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

Add Igbo.

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