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
RevLine 
[7358]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"""
[7568]20import os
[7358]21import grok
[7365]22import string
23from random import SystemRandom as r
[7358]24from waeup.sirp.interfaces import ISIRPUtils
[7471]25from waeup.sirp.smtp import send_mail as send_mail_internally
[7358]26
[7471]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`.
[7382]29
[7471]30    Merely here to stay compatible with lots of calls to this place.
[7400]31    """
[7471]32    mail_id = send_mail_internally(
33        from_name, from_addr, rcpt_name, rcpt_addr,
34        subject, body, config)
[7399]35    return True
36
[7358]37class SIRPUtils(grok.GlobalUtility):
[7678]38    """A collection of parameters and methods subject to customization.
[7358]39    """
40    grok.implements(ISIRPUtils)
[7678]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).
[7715]44    PORTAL_LANGUAGE = 'en'
[7358]45
[7701]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'),
[7722]52        'ig':(6, u'Igbo'),
[7701]53        }
54
[7681]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
[7404]112    def sendContactForm(self,from_name,from_addr,rcpt_name,rcpt_addr,
[7402]113                from_username,usertype,portal,body,subject):
[7358]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"""
[7402]124        body = text % (from_name,from_username,usertype,portal,body)
[7400]125        return send_mail(
[7402]126            from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config)
[7359]127
128    def fullname(self,firstname,lastname,middlename=None):
[7477]129        """Full name constructor.
130        """
[7359]131        # We do not necessarily have the middlename attribute
132        if middlename:
[7365]133            return string.capwords(
134                '%s %s %s' % (firstname, middlename, lastname))
[7359]135        else:
[7365]136            return string.capwords(
137                '%s %s' % (firstname, lastname))
138
139    def genPassword(self, length=8, chars=string.letters + string.digits):
[7477]140        """Generate a random password.
141        """
[7365]142        return ''.join([r().choice(chars) for i in range(length)])
143
[7382]144
[7407]145    def sendCredentials(self, user, password=None, login_url=None, msg=None):
[7399]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.
[7365]152        """
[7399]153        subject = 'Your SIRP credentials'
[7365]154        text = """Dear %s,
155
156%s
157Student Registration and Information Portal of
[7368]158%s.
[7365]159
[7368]160Your user name: %s
161Your password: %s
[7365]162Login page: %s
163
164Please remember your user name and keep
165your password secret!
166
[7382]167Please also note that passwords are case-sensitive.
168
[7365]169Regards
170"""
[7399]171        config = grok.getSite()['configuration']
172        from_name = config.name_admin
[7402]173        from_addr = config.email_admin
[7407]174        rcpt_name = user.title
175        rcpt_addr = user.email
[7382]176        body = text % (
[7407]177                rcpt_name, msg,config.name,user.name,password,login_url)
[7399]178        return send_mail(
[7402]179            from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config)
Note: See TracBrowser for help on using the repository browser.