[7358] | 1 | ## $Id: utils.py 7993 2012-03-27 20:59: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 | ## |
---|
[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 |
---|
[7969] | 29 | from waeup.kofa.utils.countries import COUNTRIES |
---|
| 30 | from waeup.kofa.utils.helpers import get_sorted_preferred |
---|
[7358] | 31 | |
---|
[7471] | 32 | def send_mail(from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config): |
---|
[7811] | 33 | """Wrapper for the real SMTP functionality in :mod:`waeup.kofa.smtp`. |
---|
[7382] | 34 | |
---|
[7471] | 35 | Merely here to stay compatible with lots of calls to this place. |
---|
[7400] | 36 | """ |
---|
[7471] | 37 | mail_id = send_mail_internally( |
---|
| 38 | from_name, from_addr, rcpt_name, rcpt_addr, |
---|
| 39 | subject, body, config) |
---|
[7399] | 40 | return True |
---|
| 41 | |
---|
[7874] | 42 | #: A list of phone prefixes (order num, country, prefix). |
---|
| 43 | #: Items with same order num will be sorted alphabetically. |
---|
| 44 | #: The lower the order num, the higher the precedence. |
---|
| 45 | INT_PHONE_PREFIXES = [ |
---|
| 46 | (99, _('Germany'), '49'), |
---|
| 47 | ( 1, _('Nigeria'), '234'), |
---|
| 48 | (99, _('U.S.'), '1'), |
---|
| 49 | ] |
---|
| 50 | |
---|
| 51 | def sorted_phone_prefixes(data = INT_PHONE_PREFIXES, request=None): |
---|
| 52 | """Sorted tuples of phone prefixes. |
---|
| 53 | |
---|
| 54 | Ordered as shown above and formatted for use in select boxes. |
---|
| 55 | |
---|
| 56 | If request is given, we'll try to translate all country names in |
---|
| 57 | order to sort alphabetically correctly. |
---|
| 58 | |
---|
| 59 | XXX: This is a function (and not a constant) as different |
---|
| 60 | languages might give different orders. This is not tested yet. |
---|
| 61 | |
---|
| 62 | XXX: If we really want to use alphabetic ordering here, we might |
---|
| 63 | think about caching results of translations. |
---|
| 64 | """ |
---|
| 65 | if request is not None: |
---|
| 66 | data = [ |
---|
| 67 | (x, translate(y, context=request), z) |
---|
| 68 | for x, y, z in data] |
---|
| 69 | return tuple([ |
---|
| 70 | ('%s (+%s)' % (x[1],x[2]), '+%s' % x[2]) |
---|
| 71 | for x in sorted(data) |
---|
| 72 | ]) |
---|
| 73 | |
---|
[7831] | 74 | class KofaUtils(grok.GlobalUtility): |
---|
[7678] | 75 | """A collection of parameters and methods subject to customization. |
---|
[7829] | 76 | |
---|
[7358] | 77 | """ |
---|
[7831] | 78 | grok.implements(IKofaUtils) |
---|
[7678] | 79 | # This the only place where we define the portal language |
---|
| 80 | # which is used for the translation of system messages |
---|
| 81 | # (e.g. object histories). |
---|
[7744] | 82 | PORTAL_LANGUAGE = 'en' |
---|
[7358] | 83 | |
---|
[7701] | 84 | PREFERRED_LANGUAGES_DICT = { |
---|
| 85 | 'en':(1, u'English'), |
---|
| 86 | 'fr':(2, u'Français'), |
---|
| 87 | 'de':(3, u'Deutsch'), |
---|
| 88 | 'ha':(4, u'Hausa'), |
---|
| 89 | 'yo':(5, u'Yoruba'), |
---|
[7722] | 90 | 'ig':(6, u'Igbo'), |
---|
[7701] | 91 | } |
---|
| 92 | |
---|
[7874] | 93 | #: A function to return |
---|
| 94 | @classmethod |
---|
| 95 | def sorted_phone_prefixes(cls, data=INT_PHONE_PREFIXES, request=None): |
---|
| 96 | return sorted_phone_prefixes(data, request) |
---|
[7871] | 97 | |
---|
[7841] | 98 | EXAM_SUBJECTS_DICT = { |
---|
[7843] | 99 | 'math': 'Mathematics', |
---|
| 100 | 'computer_science': 'Computer Science', |
---|
[7841] | 101 | } |
---|
[7836] | 102 | |
---|
[7917] | 103 | #: Exam grades. The tuple is sorted as it should be displayed in |
---|
| 104 | #: select boxes. |
---|
| 105 | EXAM_GRADES = ( |
---|
| 106 | ('A', 'Best'), |
---|
| 107 | ('B', 'Better'), |
---|
| 108 | ('C', 'Good'), |
---|
| 109 | ) |
---|
[7836] | 110 | |
---|
[7841] | 111 | INST_TYPES_DICT = { |
---|
[7681] | 112 | 'faculty': 'Faculty of', |
---|
| 113 | 'department': 'Department of', |
---|
| 114 | 'school': 'School of', |
---|
| 115 | 'office': 'Office for', |
---|
| 116 | 'centre': 'Centre for', |
---|
| 117 | 'institute': 'Institute of', |
---|
| 118 | 'school_for': 'School for', |
---|
| 119 | } |
---|
| 120 | |
---|
[7841] | 121 | STUDY_MODES_DICT = { |
---|
[7843] | 122 | 'ug_ft': 'Undergraduate Full-Time', |
---|
| 123 | 'ug_pt': 'Undergraduate Part-Time', |
---|
[7993] | 124 | 'pg_ft': 'Postgraduate Full-Time', |
---|
| 125 | 'pg_pt': 'Postgraduate Part-Time', |
---|
[7681] | 126 | } |
---|
| 127 | |
---|
[7841] | 128 | APP_CATS_DICT = { |
---|
[7843] | 129 | 'basic': 'Basic Application', |
---|
[7681] | 130 | 'no': 'no application', |
---|
| 131 | 'pg': 'Postgraduate', |
---|
| 132 | 'sandwich': 'Sandwich', |
---|
| 133 | 'cest': 'Part-Time, Diploma, Certificate' |
---|
| 134 | } |
---|
| 135 | |
---|
[7841] | 136 | SEMESTER_DICT = { |
---|
[7681] | 137 | 1: 'First Semester', |
---|
| 138 | 2: 'Second Semester', |
---|
| 139 | 3: 'Combined', |
---|
| 140 | 9: 'N/A' |
---|
| 141 | } |
---|
| 142 | |
---|
[7969] | 143 | #: a tuple of tuples (<COUNTRY-NAME>, <ISO-CODE>) with Nigeria first. |
---|
| 144 | COUNTRIES = get_sorted_preferred(COUNTRIES, ['NG']) |
---|
| 145 | |
---|
[7404] | 146 | def sendContactForm(self,from_name,from_addr,rcpt_name,rcpt_addr, |
---|
[7402] | 147 | from_username,usertype,portal,body,subject): |
---|
[7358] | 148 | """Send an email with data provided by forms. |
---|
| 149 | """ |
---|
| 150 | config = grok.getSite()['configuration'] |
---|
[7734] | 151 | text = _(u"""Fullname: ${a} |
---|
| 152 | User Id: ${b} |
---|
| 153 | User Type: ${c} |
---|
| 154 | Portal: ${d} |
---|
[7358] | 155 | |
---|
[7734] | 156 | ${e} |
---|
| 157 | """) |
---|
| 158 | text = _(text, |
---|
| 159 | mapping = { |
---|
| 160 | 'a':from_name, |
---|
| 161 | 'b':from_username, |
---|
| 162 | 'c':usertype, |
---|
| 163 | 'd':portal, |
---|
| 164 | 'e':body}) |
---|
[7811] | 165 | body = translate(text, 'waeup.kofa', |
---|
[7734] | 166 | target_language=self.PORTAL_LANGUAGE) |
---|
[7400] | 167 | return send_mail( |
---|
[7402] | 168 | from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config) |
---|
[7359] | 169 | |
---|
| 170 | def fullname(self,firstname,lastname,middlename=None): |
---|
[7477] | 171 | """Full name constructor. |
---|
| 172 | """ |
---|
[7359] | 173 | # We do not necessarily have the middlename attribute |
---|
| 174 | if middlename: |
---|
[7365] | 175 | return string.capwords( |
---|
| 176 | '%s %s %s' % (firstname, middlename, lastname)) |
---|
[7359] | 177 | else: |
---|
[7365] | 178 | return string.capwords( |
---|
| 179 | '%s %s' % (firstname, lastname)) |
---|
| 180 | |
---|
| 181 | def genPassword(self, length=8, chars=string.letters + string.digits): |
---|
[7477] | 182 | """Generate a random password. |
---|
| 183 | """ |
---|
[7365] | 184 | return ''.join([r().choice(chars) for i in range(length)]) |
---|
| 185 | |
---|
[7382] | 186 | |
---|
[7407] | 187 | def sendCredentials(self, user, password=None, login_url=None, msg=None): |
---|
[7399] | 188 | """Send credentials as email. |
---|
| 189 | |
---|
| 190 | Input is the applicant for which credentials are sent and the |
---|
| 191 | password. |
---|
| 192 | |
---|
| 193 | Returns True or False to indicate successful operation. |
---|
[7365] | 194 | """ |
---|
[7819] | 195 | subject = 'Your Kofa credentials' |
---|
[7734] | 196 | text = _(u"""Dear ${a}, |
---|
[7365] | 197 | |
---|
[7734] | 198 | ${b} |
---|
[7365] | 199 | Student Registration and Information Portal of |
---|
[7734] | 200 | ${c}. |
---|
[7365] | 201 | |
---|
[7734] | 202 | Your user name: ${d} |
---|
| 203 | Your password: ${e} |
---|
| 204 | Login page: ${f} |
---|
[7365] | 205 | |
---|
| 206 | Please remember your user name and keep |
---|
| 207 | your password secret! |
---|
| 208 | |
---|
[7382] | 209 | Please also note that passwords are case-sensitive. |
---|
| 210 | |
---|
[7365] | 211 | Regards |
---|
[7734] | 212 | """) |
---|
[7399] | 213 | config = grok.getSite()['configuration'] |
---|
| 214 | from_name = config.name_admin |
---|
[7402] | 215 | from_addr = config.email_admin |
---|
[7407] | 216 | rcpt_name = user.title |
---|
| 217 | rcpt_addr = user.email |
---|
[7734] | 218 | text = _(text, |
---|
| 219 | mapping = { |
---|
| 220 | 'a':rcpt_name, |
---|
| 221 | 'b':msg, |
---|
| 222 | 'c':config.name, |
---|
| 223 | 'd':user.name, |
---|
| 224 | 'e':password, |
---|
| 225 | 'f':login_url}) |
---|
| 226 | |
---|
[7811] | 227 | body = translate(text, 'waeup.kofa', |
---|
[7734] | 228 | target_language=self.PORTAL_LANGUAGE) |
---|
[7399] | 229 | return send_mail( |
---|
[7402] | 230 | from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config) |
---|