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