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