source: main/waeup.sirp/branches/ulif-groktoolkit-1.4/src/waeup/sirp/utils/utils.py @ 11110

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

Define preferred languages in SIRPUtils and merge language viewlets to a single viewlet.

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