[7358] | 1 | ## $Id: utils.py 14638 2017-03-21 10:13:41Z 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 | """ |
---|
| 20 | import grok |
---|
[11815] | 21 | import psutil |
---|
[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 |
---|
[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.helpers import get_sorted_preferred |
---|
[13617] | 31 | from waeup.kofa.utils.degrees import DEGREES_DICT |
---|
[7358] | 32 | |
---|
[11814] | 33 | |
---|
| 34 | def send_mail(from_name, from_addr, |
---|
| 35 | rcpt_name, rcpt_addr, |
---|
| 36 | subject, body, config): |
---|
[7811] | 37 | """Wrapper for the real SMTP functionality in :mod:`waeup.kofa.smtp`. |
---|
[7382] | 38 | |
---|
[7471] | 39 | Merely here to stay compatible with lots of calls to this place. |
---|
[7400] | 40 | """ |
---|
[7471] | 41 | mail_id = send_mail_internally( |
---|
| 42 | from_name, from_addr, rcpt_name, rcpt_addr, |
---|
| 43 | subject, body, config) |
---|
[7399] | 44 | return True |
---|
| 45 | |
---|
[11814] | 46 | |
---|
[7874] | 47 | #: A list of phone prefixes (order num, country, prefix). |
---|
| 48 | #: Items with same order num will be sorted alphabetically. |
---|
| 49 | #: The lower the order num, the higher the precedence. |
---|
| 50 | INT_PHONE_PREFIXES = [ |
---|
| 51 | (99, _('Germany'), '49'), |
---|
[11814] | 52 | (1, _('Nigeria'), '234'), |
---|
[7874] | 53 | (99, _('U.S.'), '1'), |
---|
| 54 | ] |
---|
| 55 | |
---|
[11814] | 56 | |
---|
| 57 | def sorted_phone_prefixes(data=INT_PHONE_PREFIXES, request=None): |
---|
[7874] | 58 | """Sorted tuples of phone prefixes. |
---|
| 59 | |
---|
| 60 | Ordered as shown above and formatted for use in select boxes. |
---|
| 61 | |
---|
| 62 | If request is given, we'll try to translate all country names in |
---|
| 63 | order to sort alphabetically correctly. |
---|
| 64 | |
---|
| 65 | XXX: This is a function (and not a constant) as different |
---|
| 66 | languages might give different orders. This is not tested yet. |
---|
| 67 | |
---|
| 68 | XXX: If we really want to use alphabetic ordering here, we might |
---|
| 69 | think about caching results of translations. |
---|
| 70 | """ |
---|
| 71 | if request is not None: |
---|
| 72 | data = [ |
---|
| 73 | (x, translate(y, context=request), z) |
---|
| 74 | for x, y, z in data] |
---|
| 75 | return tuple([ |
---|
[11814] | 76 | ('%s (+%s)' % (x[1], x[2]), '+%s' % x[2]) |
---|
[7874] | 77 | for x in sorted(data) |
---|
| 78 | ]) |
---|
| 79 | |
---|
[11814] | 80 | |
---|
[7831] | 81 | class KofaUtils(grok.GlobalUtility): |
---|
[7678] | 82 | """A collection of parameters and methods subject to customization. |
---|
[7358] | 83 | """ |
---|
[7831] | 84 | grok.implements(IKofaUtils) |
---|
[13132] | 85 | |
---|
| 86 | #: This the only place where we define the portal language |
---|
| 87 | #: which is used for the translation of system messages |
---|
| 88 | #: (e.g. object histories) pdf slips. |
---|
[7744] | 89 | PORTAL_LANGUAGE = 'en' |
---|
[7358] | 90 | |
---|
[13617] | 91 | DEGREES_DICT = DEGREES_DICT |
---|
| 92 | |
---|
[7701] | 93 | PREFERRED_LANGUAGES_DICT = { |
---|
[11814] | 94 | 'en': (1, u'English'), |
---|
| 95 | 'fr': (2, u'Français'), |
---|
| 96 | 'de': (3, u'Deutsch'), |
---|
| 97 | 'ha': (4, u'Hausa'), |
---|
| 98 | 'yo': (5, u'Yoruba'), |
---|
| 99 | 'ig': (6, u'Igbo'), |
---|
[7701] | 100 | } |
---|
| 101 | |
---|
[7874] | 102 | #: A function to return |
---|
| 103 | @classmethod |
---|
| 104 | def sorted_phone_prefixes(cls, data=INT_PHONE_PREFIXES, request=None): |
---|
| 105 | return sorted_phone_prefixes(data, request) |
---|
[7871] | 106 | |
---|
[7841] | 107 | EXAM_SUBJECTS_DICT = { |
---|
[7843] | 108 | 'math': 'Mathematics', |
---|
| 109 | 'computer_science': 'Computer Science', |
---|
[7841] | 110 | } |
---|
[7836] | 111 | |
---|
[7917] | 112 | #: Exam grades. The tuple is sorted as it should be displayed in |
---|
| 113 | #: select boxes. |
---|
| 114 | EXAM_GRADES = ( |
---|
| 115 | ('A', 'Best'), |
---|
| 116 | ('B', 'Better'), |
---|
| 117 | ('C', 'Good'), |
---|
| 118 | ) |
---|
[7836] | 119 | |
---|
[7841] | 120 | INST_TYPES_DICT = { |
---|
[8084] | 121 | 'none': '', |
---|
[7681] | 122 | 'faculty': 'Faculty of', |
---|
| 123 | 'department': 'Department of', |
---|
| 124 | 'school': 'School of', |
---|
| 125 | 'office': 'Office for', |
---|
| 126 | 'centre': 'Centre for', |
---|
| 127 | 'institute': 'Institute of', |
---|
| 128 | 'school_for': 'School for', |
---|
[8084] | 129 | 'college': 'College of', |
---|
[10302] | 130 | 'directorate': 'Directorate of', |
---|
[7681] | 131 | } |
---|
| 132 | |
---|
[7841] | 133 | STUDY_MODES_DICT = { |
---|
[9131] | 134 | 'transfer': 'Transfer', |
---|
[7843] | 135 | 'ug_ft': 'Undergraduate Full-Time', |
---|
| 136 | 'ug_pt': 'Undergraduate Part-Time', |
---|
[7993] | 137 | 'pg_ft': 'Postgraduate Full-Time', |
---|
| 138 | 'pg_pt': 'Postgraduate Part-Time', |
---|
[7681] | 139 | } |
---|
| 140 | |
---|
[11451] | 141 | DISABLE_PAYMENT_GROUP_DICT = { |
---|
| 142 | 'sf_all': 'School Fee - All Students', |
---|
| 143 | } |
---|
| 144 | |
---|
[7841] | 145 | APP_CATS_DICT = { |
---|
[7843] | 146 | 'basic': 'Basic Application', |
---|
[7681] | 147 | 'no': 'no application', |
---|
| 148 | 'pg': 'Postgraduate', |
---|
| 149 | 'sandwich': 'Sandwich', |
---|
| 150 | 'cest': 'Part-Time, Diploma, Certificate' |
---|
| 151 | } |
---|
| 152 | |
---|
[7841] | 153 | SEMESTER_DICT = { |
---|
[10437] | 154 | 1: '1st Semester', |
---|
| 155 | 2: '2nd Semester', |
---|
[7681] | 156 | 3: 'Combined', |
---|
| 157 | 9: 'N/A' |
---|
| 158 | } |
---|
| 159 | |
---|
[14638] | 160 | COURSE_CATEGORY_DICT = { |
---|
| 161 | } |
---|
| 162 | |
---|
[9400] | 163 | SPECIAL_HANDLING_DICT = { |
---|
| 164 | 'regular': 'Regular Hostel', |
---|
| 165 | 'blocked': 'Blocked Hostel', |
---|
[10831] | 166 | 'pg': 'Postgraduate Hostel' |
---|
[9400] | 167 | } |
---|
| 168 | |
---|
[10831] | 169 | SPECIAL_APP_DICT = { |
---|
| 170 | 'transcript': 'Transcript Fee Payment', |
---|
[11575] | 171 | 'clearance': 'Acceptance Fee', |
---|
[10831] | 172 | } |
---|
| 173 | |
---|
[9405] | 174 | PAYMENT_CATEGORIES = { |
---|
| 175 | 'schoolfee': 'School Fee', |
---|
| 176 | 'clearance': 'Acceptance Fee', |
---|
| 177 | 'bed_allocation': 'Bed Allocation Fee', |
---|
| 178 | 'hostel_maintenance': 'Hostel Maintenance Fee', |
---|
| 179 | 'transfer': 'Transfer Fee', |
---|
| 180 | 'gown': 'Gown Hire Fee', |
---|
[9866] | 181 | 'application': 'Application Fee', |
---|
[10449] | 182 | 'transcript': 'Transcript Fee', |
---|
[13031] | 183 | 'late_registration': 'Late Course Registration Fee' |
---|
[9405] | 184 | } |
---|
| 185 | |
---|
[9866] | 186 | SELECTABLE_PAYMENT_CATEGORIES = deepcopy(PAYMENT_CATEGORIES) |
---|
[9730] | 187 | |
---|
[9866] | 188 | PREVIOUS_PAYMENT_CATEGORIES = deepcopy(SELECTABLE_PAYMENT_CATEGORIES) |
---|
[9862] | 189 | |
---|
[12564] | 190 | REPORTABLE_PAYMENT_CATEGORIES = { |
---|
| 191 | 'schoolfee': 'School Fee', |
---|
| 192 | 'clearance': 'Acceptance Fee', |
---|
| 193 | 'hostel_maintenance': 'Hostel Maintenance Fee', |
---|
| 194 | 'gown': 'Gown Hire Fee', |
---|
| 195 | } |
---|
| 196 | |
---|
[9868] | 197 | BALANCE_PAYMENT_CATEGORIES = { |
---|
[9867] | 198 | 'schoolfee': 'School Fee', |
---|
| 199 | } |
---|
[9864] | 200 | |
---|
[9649] | 201 | MODE_GROUPS = { |
---|
[11814] | 202 | 'All': ('all',), |
---|
| 203 | 'Undergraduate Full-Time': ('ug_ft',), |
---|
| 204 | 'Undergraduate Part-Time': ('ug_pt',), |
---|
| 205 | 'Postgraduate Full-Time': ('pg_ft',), |
---|
| 206 | 'Postgraduate Part-Time': ('pg_pt',), |
---|
[9649] | 207 | } |
---|
| 208 | |
---|
[13125] | 209 | VERDICTS_DICT = { |
---|
| 210 | '0': _('(not yet)'), |
---|
| 211 | 'A': 'Successful student', |
---|
| 212 | 'B': 'Student with carryover courses', |
---|
| 213 | 'C': 'Student on probation', |
---|
| 214 | } |
---|
| 215 | |
---|
[11800] | 216 | #: Set positive number for allowed max, negative for required min |
---|
| 217 | #: avail. |
---|
| 218 | #: Use integer for bytes value, float for percent |
---|
| 219 | #: value. `cpu-load`, of course, accepts float values only. |
---|
| 220 | #: `swap-mem` = Swap Memory, `virt-mem` = Virtual Memory, |
---|
[11969] | 221 | #: `cpu-load` = CPU load in percent. |
---|
[11800] | 222 | SYSTEM_MAX_LOAD = { |
---|
| 223 | 'swap-mem': None, |
---|
| 224 | 'virt-mem': None, |
---|
| 225 | 'cpu-load': 100.0, |
---|
| 226 | } |
---|
| 227 | |
---|
[11814] | 228 | def sendContactForm(self, from_name, from_addr, rcpt_name, rcpt_addr, |
---|
| 229 | from_username, usertype, portal, body, subject): |
---|
[7358] | 230 | """Send an email with data provided by forms. |
---|
| 231 | """ |
---|
| 232 | config = grok.getSite()['configuration'] |
---|
[7734] | 233 | text = _(u"""Fullname: ${a} |
---|
| 234 | User Id: ${b} |
---|
| 235 | User Type: ${c} |
---|
| 236 | Portal: ${d} |
---|
[7358] | 237 | |
---|
[7734] | 238 | ${e} |
---|
| 239 | """) |
---|
[11814] | 240 | text = _(text, mapping={ |
---|
| 241 | 'a': from_name, |
---|
| 242 | 'b': from_username, |
---|
| 243 | 'c': usertype, |
---|
| 244 | 'd': portal, |
---|
| 245 | 'e': body}) |
---|
[7811] | 246 | body = translate(text, 'waeup.kofa', |
---|
[7734] | 247 | target_language=self.PORTAL_LANGUAGE) |
---|
[8436] | 248 | if not (from_addr and rcpt_addr): |
---|
| 249 | return False |
---|
[7400] | 250 | return send_mail( |
---|
[11814] | 251 | from_name, from_addr, rcpt_name, rcpt_addr, |
---|
| 252 | subject, body, config) |
---|
[7359] | 253 | |
---|
[8181] | 254 | @property |
---|
| 255 | def tzinfo(self): |
---|
[13124] | 256 | """Time zone of the university. |
---|
| 257 | """ |
---|
[8181] | 258 | # For Nigeria: pytz.timezone('Africa/Lagos') |
---|
[9543] | 259 | # For Germany: pytz.timezone('Europe/Berlin') |
---|
[8181] | 260 | return pytz.utc |
---|
| 261 | |
---|
[11814] | 262 | def fullname(self, firstname, lastname, middlename=None): |
---|
[13124] | 263 | """Construct fullname. |
---|
[7477] | 264 | """ |
---|
[7359] | 265 | # We do not necessarily have the middlename attribute |
---|
| 266 | if middlename: |
---|
[8603] | 267 | name = '%s %s %s' % (firstname, middlename, lastname) |
---|
[7359] | 268 | else: |
---|
[8603] | 269 | name = '%s %s' % (firstname, lastname) |
---|
[13492] | 270 | if '<' in name: |
---|
| 271 | return 'XXX' |
---|
[11814] | 272 | return string.capwords( |
---|
| 273 | name.replace('-', ' - ')).replace(' - ', '-') |
---|
[7365] | 274 | |
---|
| 275 | def genPassword(self, length=8, chars=string.letters + string.digits): |
---|
[7477] | 276 | """Generate a random password. |
---|
| 277 | """ |
---|
[7365] | 278 | return ''.join([r().choice(chars) for i in range(length)]) |
---|
| 279 | |
---|
[8853] | 280 | def sendCredentials(self, user, password=None, url_info=None, msg=None): |
---|
[13124] | 281 | """Send credentials as email. Input is the user for which credentials |
---|
| 282 | are sent and the password. Method returns True or False to indicate |
---|
| 283 | successful operation. |
---|
[7365] | 284 | """ |
---|
[7819] | 285 | subject = 'Your Kofa credentials' |
---|
[7734] | 286 | text = _(u"""Dear ${a}, |
---|
[7365] | 287 | |
---|
[7734] | 288 | ${b} |
---|
[7365] | 289 | Student Registration and Information Portal of |
---|
[7734] | 290 | ${c}. |
---|
[7365] | 291 | |
---|
[7734] | 292 | Your user name: ${d} |
---|
| 293 | Your password: ${e} |
---|
[8853] | 294 | ${f} |
---|
[7365] | 295 | |
---|
| 296 | Please remember your user name and keep |
---|
| 297 | your password secret! |
---|
| 298 | |
---|
[7382] | 299 | Please also note that passwords are case-sensitive. |
---|
| 300 | |
---|
[7365] | 301 | Regards |
---|
[7734] | 302 | """) |
---|
[7399] | 303 | config = grok.getSite()['configuration'] |
---|
| 304 | from_name = config.name_admin |
---|
[7402] | 305 | from_addr = config.email_admin |
---|
[7407] | 306 | rcpt_name = user.title |
---|
| 307 | rcpt_addr = user.email |
---|
[11814] | 308 | text = _(text, mapping={ |
---|
| 309 | 'a': rcpt_name, |
---|
| 310 | 'b': msg, |
---|
| 311 | 'c': config.name, |
---|
| 312 | 'd': user.name, |
---|
| 313 | 'e': password, |
---|
| 314 | 'f': url_info}) |
---|
[7734] | 315 | |
---|
[7811] | 316 | body = translate(text, 'waeup.kofa', |
---|
[7734] | 317 | target_language=self.PORTAL_LANGUAGE) |
---|
[7399] | 318 | return send_mail( |
---|
[11814] | 319 | from_name, from_addr, rcpt_name, rcpt_addr, |
---|
| 320 | subject, body, config) |
---|
[9987] | 321 | |
---|
[14014] | 322 | def inviteReferee(self, referee, applicant, url_info=None): |
---|
| 323 | """Send invitation email to referee. |
---|
| 324 | """ |
---|
| 325 | config = grok.getSite()['configuration'] |
---|
| 326 | subject = 'Request for referee report from %s' % config.name |
---|
| 327 | text = _(u"""Dear ${a}, |
---|
| 328 | |
---|
[14040] | 329 | The candidate with Id ${b} and name ${c} applied to |
---|
| 330 | the ${d} to study ${e} for the ${f} session. |
---|
| 331 | The candidate has listed you as referee. You are thus required to kindly use |
---|
| 332 | the link below to provide your referral remarks on or before |
---|
| 333 | ${g}. |
---|
[14014] | 334 | |
---|
[14040] | 335 | ${h} |
---|
| 336 | |
---|
| 337 | Thank You |
---|
| 338 | |
---|
| 339 | The Secretary |
---|
| 340 | Post Graduate School |
---|
| 341 | ${d} |
---|
[14014] | 342 | """) |
---|
| 343 | from_name = config.name_admin |
---|
| 344 | from_addr = config.email_admin |
---|
| 345 | rcpt_name = referee.name |
---|
| 346 | rcpt_addr = referee.email |
---|
[14040] | 347 | session = '%s/%s' % ( |
---|
| 348 | applicant.__parent__.year, applicant.__parent__.year+1) |
---|
[14014] | 349 | text = _(text, mapping={ |
---|
| 350 | 'a': rcpt_name, |
---|
[14040] | 351 | 'b': applicant.applicant_id, |
---|
| 352 | 'c': applicant.display_fullname, |
---|
| 353 | 'd': config.name, |
---|
| 354 | 'e': applicant.course1.title, |
---|
| 355 | 'f': session, |
---|
| 356 | 'g': applicant.__parent__.enddate, |
---|
| 357 | 'h': url_info, |
---|
| 358 | }) |
---|
[14014] | 359 | |
---|
| 360 | body = translate(text, 'waeup.kofa', |
---|
| 361 | target_language=self.PORTAL_LANGUAGE) |
---|
| 362 | return send_mail( |
---|
| 363 | from_name, from_addr, rcpt_name, rcpt_addr, |
---|
| 364 | subject, body, config) |
---|
| 365 | |
---|
[9987] | 366 | def getPaymentItem(self, payment): |
---|
[13124] | 367 | """Return payment item. This method can be used to customize the |
---|
| 368 | `display_item` property attribute, e.g. in order to hide bed coordinates |
---|
| 369 | if maintenance fee is not paid. |
---|
[9987] | 370 | """ |
---|
| 371 | return payment.p_item |
---|
[11815] | 372 | |
---|
| 373 | def expensive_actions_allowed(self, type=None, request=None): |
---|
| 374 | """Tell, whether expensive actions are currently allowed. |
---|
| 375 | Check system load/health (or other external circumstances) and |
---|
| 376 | locally set values to see, whether expensive actions should be |
---|
| 377 | allowed (`True`) or better avoided (`False`). |
---|
| 378 | Use this to allow or forbid exports, report generations, or |
---|
| 379 | similar actions. |
---|
| 380 | """ |
---|
| 381 | max_values = self.SYSTEM_MAX_LOAD |
---|
[11816] | 382 | for (key, func) in ( |
---|
| 383 | ('swap-mem', psutil.swap_memory), |
---|
[11818] | 384 | ('virt-mem', psutil.virtual_memory), |
---|
[11816] | 385 | ): |
---|
| 386 | max_val = max_values.get(key, None) |
---|
| 387 | if max_val is None: |
---|
| 388 | continue |
---|
| 389 | mem_val = func() |
---|
[11815] | 390 | if isinstance(max_val, float): |
---|
[11816] | 391 | # percents |
---|
[11821] | 392 | if max_val < 0.0: |
---|
| 393 | max_val = 100.0 + max_val |
---|
[11816] | 394 | if mem_val.percent > max_val: |
---|
[11815] | 395 | return False |
---|
| 396 | else: |
---|
[11816] | 397 | # number of bytes |
---|
[11821] | 398 | if max_val < 0: |
---|
| 399 | max_val = mem_val.total + max_val |
---|
[11816] | 400 | if mem_val.used > max_val: |
---|
[11815] | 401 | return False |
---|
| 402 | return True |
---|
[13198] | 403 | |
---|
| 404 | def export_disabled_message(self): |
---|
| 405 | export_disabled_message = grok.getSite()[ |
---|
| 406 | 'configuration'].export_disabled_message |
---|
| 407 | if export_disabled_message: |
---|
| 408 | return export_disabled_message |
---|
[14473] | 409 | return None |
---|
| 410 | |
---|
| 411 | def format_float(self, value, prec): |
---|
| 412 | # cut floating point value |
---|
| 413 | value = int(pow(10, prec)*value) / (1.0*pow(10, prec)) |
---|
| 414 | return '{:{width}.{prec}f}'.format(value, width=0, prec=prec) |
---|