[7358] | 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 | ## |
---|
[7819] | 18 | """General helper utilities for Kofa. |
---|
[7358] | 19 | """ |
---|
[7568] | 20 | import os |
---|
[7358] | 21 | import grok |
---|
[7365] | 22 | import string |
---|
[7734] | 23 | from zope.i18n import translate |
---|
[7829] | 24 | from zope.interface import implements |
---|
[7365] | 25 | from random import SystemRandom as r |
---|
[7819] | 26 | from waeup.kofa.interfaces import IKofaUtils |
---|
[7811] | 27 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
| 28 | from waeup.kofa.smtp import send_mail as send_mail_internally |
---|
[7358] | 29 | |
---|
[7471] | 30 | def 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 | |
---|
[7831] | 40 | class KofaUtils(grok.GlobalUtility): |
---|
[7678] | 41 | """A collection of parameters and methods subject to customization. |
---|
[7829] | 42 | |
---|
[7358] | 43 | """ |
---|
[7831] | 44 | grok.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 | |
---|
[7841] | 59 | EXAM_SUBJECTS_DICT = { |
---|
[7843] | 60 | 'math': 'Mathematics', |
---|
| 61 | 'computer_science': 'Computer Science', |
---|
[7841] | 62 | } |
---|
[7836] | 63 | |
---|
[7841] | 64 | EXAM_GRADES_DICT = { |
---|
[7843] | 65 | 'A': (1, 'Best'), |
---|
| 66 | 'B': (2, 'Better'), |
---|
| 67 | 'C': (3, 'Good'), |
---|
[7841] | 68 | } |
---|
[7836] | 69 | |
---|
[7841] | 70 | INST_TYPES_DICT = { |
---|
[7681] | 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 | |
---|
[7841] | 80 | STUDY_MODES_DICT = { |
---|
[7843] | 81 | 'ug_ft': 'Undergraduate Full-Time', |
---|
| 82 | 'ug_pt': 'Undergraduate Part-Time', |
---|
[7681] | 83 | } |
---|
| 84 | |
---|
[7841] | 85 | APP_CATS_DICT = { |
---|
[7843] | 86 | 'basic': 'Basic Application', |
---|
[7681] | 87 | 'no': 'no application', |
---|
| 88 | 'pg': 'Postgraduate', |
---|
| 89 | 'sandwich': 'Sandwich', |
---|
| 90 | 'cest': 'Part-Time, Diploma, Certificate' |
---|
| 91 | } |
---|
| 92 | |
---|
[7841] | 93 | SEMESTER_DICT = { |
---|
[7681] | 94 | 1: 'First Semester', |
---|
| 95 | 2: 'Second Semester', |
---|
| 96 | 3: 'Combined', |
---|
| 97 | 9: 'N/A' |
---|
| 98 | } |
---|
| 99 | |
---|
[7404] | 100 | def sendContactForm(self,from_name,from_addr,rcpt_name,rcpt_addr, |
---|
[7402] | 101 | from_username,usertype,portal,body,subject): |
---|
[7358] | 102 | """Send an email with data provided by forms. |
---|
| 103 | """ |
---|
| 104 | config = grok.getSite()['configuration'] |
---|
[7734] | 105 | text = _(u"""Fullname: ${a} |
---|
| 106 | User Id: ${b} |
---|
| 107 | User Type: ${c} |
---|
| 108 | Portal: ${d} |
---|
[7358] | 109 | |
---|
[7734] | 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}) |
---|
[7811] | 119 | body = translate(text, 'waeup.kofa', |
---|
[7734] | 120 | target_language=self.PORTAL_LANGUAGE) |
---|
[7400] | 121 | return send_mail( |
---|
[7402] | 122 | from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config) |
---|
[7359] | 123 | |
---|
| 124 | def fullname(self,firstname,lastname,middlename=None): |
---|
[7477] | 125 | """Full name constructor. |
---|
| 126 | """ |
---|
[7359] | 127 | # We do not necessarily have the middlename attribute |
---|
| 128 | if middlename: |
---|
[7365] | 129 | return string.capwords( |
---|
| 130 | '%s %s %s' % (firstname, middlename, lastname)) |
---|
[7359] | 131 | else: |
---|
[7365] | 132 | return string.capwords( |
---|
| 133 | '%s %s' % (firstname, lastname)) |
---|
| 134 | |
---|
| 135 | def genPassword(self, length=8, chars=string.letters + string.digits): |
---|
[7477] | 136 | """Generate a random password. |
---|
| 137 | """ |
---|
[7365] | 138 | return ''.join([r().choice(chars) for i in range(length)]) |
---|
| 139 | |
---|
[7382] | 140 | |
---|
[7407] | 141 | def sendCredentials(self, user, password=None, login_url=None, msg=None): |
---|
[7399] | 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. |
---|
[7365] | 148 | """ |
---|
[7819] | 149 | subject = 'Your Kofa credentials' |
---|
[7734] | 150 | text = _(u"""Dear ${a}, |
---|
[7365] | 151 | |
---|
[7734] | 152 | ${b} |
---|
[7365] | 153 | Student Registration and Information Portal of |
---|
[7734] | 154 | ${c}. |
---|
[7365] | 155 | |
---|
[7734] | 156 | Your user name: ${d} |
---|
| 157 | Your password: ${e} |
---|
| 158 | Login page: ${f} |
---|
[7365] | 159 | |
---|
| 160 | Please remember your user name and keep |
---|
| 161 | your password secret! |
---|
| 162 | |
---|
[7382] | 163 | Please also note that passwords are case-sensitive. |
---|
| 164 | |
---|
[7365] | 165 | Regards |
---|
[7734] | 166 | """) |
---|
[7399] | 167 | config = grok.getSite()['configuration'] |
---|
| 168 | from_name = config.name_admin |
---|
[7402] | 169 | from_addr = config.email_admin |
---|
[7407] | 170 | rcpt_name = user.title |
---|
| 171 | rcpt_addr = user.email |
---|
[7734] | 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 | |
---|
[7811] | 181 | body = translate(text, 'waeup.kofa', |
---|
[7734] | 182 | target_language=self.PORTAL_LANGUAGE) |
---|
[7399] | 183 | return send_mail( |
---|
[7402] | 184 | from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config) |
---|