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

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

Reduce dictionaries in base package. They will be part of the custom package.

  • Property svn:keywords set to Id
File size: 5.4 KB
Line 
1## $Id: utils.py 7843 2012-03-12 11:19:57Z 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 Kofa.
19"""
20import os
21import grok
22import string
23from zope.i18n import translate
24from zope.interface import implements
25from random import SystemRandom as r
26from waeup.kofa.interfaces import IKofaUtils
27from waeup.kofa.interfaces import MessageFactory as _
28from waeup.kofa.smtp import send_mail as send_mail_internally
29
30def send_mail(from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config):
31    """Wrapper for the real SMTP functionality in :mod:`waeup.kofa.smtp`.
32
33    Merely here to stay compatible with lots of calls to this place.
34    """
35    mail_id = send_mail_internally(
36        from_name, from_addr, rcpt_name, rcpt_addr,
37        subject, body, config)
38    return True
39
40class KofaUtils(grok.GlobalUtility):
41    """A collection of parameters and methods subject to customization.
42
43    """
44    grok.implements(IKofaUtils)
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).
48    PORTAL_LANGUAGE = 'en'
49
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'),
56        'ig':(6, u'Igbo'),
57        }
58
59    EXAM_SUBJECTS_DICT = {
60        'math': 'Mathematics',
61        'computer_science': 'Computer Science',
62        }
63
64    EXAM_GRADES_DICT = {
65        'A': (1, 'Best'),
66        'B': (2, 'Better'),
67        'C': (3, 'Good'),
68        }
69
70    INST_TYPES_DICT = {
71        'faculty': 'Faculty of',
72        'department': 'Department of',
73        'school': 'School of',
74        'office': 'Office for',
75        'centre': 'Centre for',
76        'institute': 'Institute of',
77        'school_for': 'School for',
78        }
79
80    STUDY_MODES_DICT = {
81        'ug_ft': 'Undergraduate Full-Time',
82        'ug_pt': 'Undergraduate Part-Time',
83        }
84
85    APP_CATS_DICT = {
86        'basic': 'Basic Application',
87        'no': 'no application',
88        'pg': 'Postgraduate',
89        'sandwich': 'Sandwich',
90        'cest': 'Part-Time, Diploma, Certificate'
91        }
92
93    SEMESTER_DICT = {
94        1: 'First Semester',
95        2: 'Second Semester',
96        3: 'Combined',
97        9: 'N/A'
98        }
99
100    def sendContactForm(self,from_name,from_addr,rcpt_name,rcpt_addr,
101                from_username,usertype,portal,body,subject):
102        """Send an email with data provided by forms.
103        """
104        config = grok.getSite()['configuration']
105        text = _(u"""Fullname: ${a}
106User Id: ${b}
107User Type: ${c}
108Portal: ${d}
109
110${e}
111""")
112        text = _(text,
113            mapping = {
114            'a':from_name,
115            'b':from_username,
116            'c':usertype,
117            'd':portal,
118            'e':body})
119        body = translate(text, 'waeup.kofa',
120            target_language=self.PORTAL_LANGUAGE)
121        return send_mail(
122            from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config)
123
124    def fullname(self,firstname,lastname,middlename=None):
125        """Full name constructor.
126        """
127        # We do not necessarily have the middlename attribute
128        if middlename:
129            return string.capwords(
130                '%s %s %s' % (firstname, middlename, lastname))
131        else:
132            return string.capwords(
133                '%s %s' % (firstname, lastname))
134
135    def genPassword(self, length=8, chars=string.letters + string.digits):
136        """Generate a random password.
137        """
138        return ''.join([r().choice(chars) for i in range(length)])
139
140
141    def sendCredentials(self, user, password=None, login_url=None, msg=None):
142        """Send credentials as email.
143
144        Input is the applicant for which credentials are sent and the
145        password.
146
147        Returns True or False to indicate successful operation.
148        """
149        subject = 'Your Kofa credentials'
150        text = _(u"""Dear ${a},
151
152${b}
153Student Registration and Information Portal of
154${c}.
155
156Your user name: ${d}
157Your password: ${e}
158Login page: ${f}
159
160Please remember your user name and keep
161your password secret!
162
163Please also note that passwords are case-sensitive.
164
165Regards
166""")
167        config = grok.getSite()['configuration']
168        from_name = config.name_admin
169        from_addr = config.email_admin
170        rcpt_name = user.title
171        rcpt_addr = user.email
172        text = _(text,
173            mapping = {
174            'a':rcpt_name,
175            'b':msg,
176            'c':config.name,
177            'd':user.name,
178            'e':password,
179            'f':login_url})
180
181        body = translate(text, 'waeup.kofa',
182            target_language=self.PORTAL_LANGUAGE)
183        return send_mail(
184            from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config)
Note: See TracBrowser for help on using the repository browser.