source: main/waeup.kofa/trunk/src/waeup/kofa/utils/utils.py @ 7829

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

Register KofaUtils? in configure.zcml. Is that really necessary?

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