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