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

Last change on this file since 7756 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
RevLine 
[7358]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"""
[7568]20import os
[7358]21import grok
[7365]22import string
[7734]23from zope.i18n import translate
[7365]24from random import SystemRandom as r
[7358]25from waeup.sirp.interfaces import ISIRPUtils
[7734]26from waeup.sirp.interfaces import MessageFactory as _
[7471]27from waeup.sirp.smtp import send_mail as send_mail_internally
[7358]28
[7471]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`.
[7382]31
[7471]32    Merely here to stay compatible with lots of calls to this place.
[7400]33    """
[7471]34    mail_id = send_mail_internally(
35        from_name, from_addr, rcpt_name, rcpt_addr,
36        subject, body, config)
[7399]37    return True
38
[7358]39class SIRPUtils(grok.GlobalUtility):
[7678]40    """A collection of parameters and methods subject to customization.
[7358]41    """
42    grok.implements(ISIRPUtils)
[7678]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).
[7744]46    PORTAL_LANGUAGE = 'en'
[7358]47
[7701]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'),
[7722]54        'ig':(6, u'Igbo'),
[7701]55        }
56
[7681]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
[7404]114    def sendContactForm(self,from_name,from_addr,rcpt_name,rcpt_addr,
[7402]115                from_username,usertype,portal,body,subject):
[7358]116        """Send an email with data provided by forms.
117        """
118        config = grok.getSite()['configuration']
[7734]119        text = _(u"""Fullname: ${a}
120User Id: ${b}
121User Type: ${c}
122Portal: ${d}
[7358]123
[7734]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)
[7400]135        return send_mail(
[7402]136            from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config)
[7359]137
138    def fullname(self,firstname,lastname,middlename=None):
[7477]139        """Full name constructor.
140        """
[7359]141        # We do not necessarily have the middlename attribute
142        if middlename:
[7365]143            return string.capwords(
144                '%s %s %s' % (firstname, middlename, lastname))
[7359]145        else:
[7365]146            return string.capwords(
147                '%s %s' % (firstname, lastname))
148
149    def genPassword(self, length=8, chars=string.letters + string.digits):
[7477]150        """Generate a random password.
151        """
[7365]152        return ''.join([r().choice(chars) for i in range(length)])
153
[7382]154
[7407]155    def sendCredentials(self, user, password=None, login_url=None, msg=None):
[7399]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.
[7365]162        """
[7399]163        subject = 'Your SIRP credentials'
[7734]164        text = _(u"""Dear ${a},
[7365]165
[7734]166${b}
[7365]167Student Registration and Information Portal of
[7734]168${c}.
[7365]169
[7734]170Your user name: ${d}
171Your password: ${e}
172Login page: ${f}
[7365]173
174Please remember your user name and keep
175your password secret!
176
[7382]177Please also note that passwords are case-sensitive.
178
[7365]179Regards
[7734]180""")
[7399]181        config = grok.getSite()['configuration']
182        from_name = config.name_admin
[7402]183        from_addr = config.email_admin
[7407]184        rcpt_name = user.title
185        rcpt_addr = user.email
[7734]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)
[7399]197        return send_mail(
[7402]198            from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config)
Note: See TracBrowser for help on using the repository browser.