source: main/waeup.kofa/trunk/src/waeup/kofa/utils/utils.py @ 15517

Last change on this file since 15517 was 15476, checked in by Henrik Bettermann, 6 years ago

Use decimal.Decimal to counteract Python floating point limitation.

  • Property svn:keywords set to Id
File size: 13.0 KB
Line 
1## $Id: utils.py 15476 2019-06-27 06:07:52Z 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##
18"""General helper utilities for Kofa.
19"""
20import grok
21import psutil
22import string
23import pytz
24import decimal
25from copy import deepcopy
26from random import SystemRandom as r
27from zope.i18n import translate
28from waeup.kofa.interfaces import IKofaUtils
29from waeup.kofa.interfaces import MessageFactory as _
30from waeup.kofa.smtp import send_mail as send_mail_internally
31from waeup.kofa.utils.helpers import get_sorted_preferred
32from waeup.kofa.utils.degrees import DEGREES_DICT
33
34
35def send_mail(from_name, from_addr,
36              rcpt_name, rcpt_addr,
37              subject, body, config):
38    """Wrapper for the real SMTP functionality in :mod:`waeup.kofa.smtp`.
39
40    Merely here to stay compatible with lots of calls to this place.
41    """
42    mail_id = send_mail_internally(
43        from_name, from_addr, rcpt_name, rcpt_addr,
44        subject, body, config)
45    return True
46
47
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.
51INT_PHONE_PREFIXES = [
52    (99, _('Germany'), '49'),
53    (1, _('Nigeria'), '234'),
54    (99, _('U.S.'), '1'),
55    ]
56
57
58def sorted_phone_prefixes(data=INT_PHONE_PREFIXES, request=None):
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([
77        ('%s (+%s)' % (x[1], x[2]), '+%s' % x[2])
78        for x in sorted(data)
79        ])
80
81
82class KofaUtils(grok.GlobalUtility):
83    """A collection of parameters and methods subject to customization.
84    """
85    grok.implements(IKofaUtils)
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.
90    PORTAL_LANGUAGE = 'en'
91
92    DEGREES_DICT = DEGREES_DICT
93
94    PREFERRED_LANGUAGES_DICT = {
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'),
101        }
102
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)
107
108    EXAM_SUBJECTS_DICT = {
109        'math': 'Mathematics',
110        'computer_science': 'Computer Science',
111        }
112
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        )
120
121    INST_TYPES_DICT = {
122        'none': '',
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',
130        'college': 'College of',
131        'directorate': 'Directorate of',
132        }
133
134    STUDY_MODES_DICT = {
135        'transfer': 'Transfer',
136        'ug_ft': 'Undergraduate Full-Time',
137        'ug_pt': 'Undergraduate Part-Time',
138        'pg_ft': 'Postgraduate Full-Time',
139        'pg_pt': 'Postgraduate Part-Time',
140        }
141
142    DISABLE_PAYMENT_GROUP_DICT = {
143        'sf_all': 'School Fee - All Students',
144        }
145
146    APP_CATS_DICT = {
147        'basic': 'Basic Application',
148        'no': 'no application',
149        'pg': 'Postgraduate',
150        'sandwich': 'Sandwich',
151        'cest': 'Part-Time, Diploma, Certificate'
152        }
153
154    SEMESTER_DICT = {
155        1: '1st Semester',
156        2: '2nd Semester',
157        3: 'Combined',
158        9: 'N/A'
159        }
160
161    COURSE_CATEGORY_DICT = {
162        }
163
164    SPECIAL_HANDLING_DICT = {
165        'regular': 'Regular Hostel',
166        'blocked': 'Blocked Hostel',
167        'pg': 'Postgraduate Hostel'
168        }
169
170    SPECIAL_APP_DICT = {
171        'transcript': 'Transcript Fee Payment',
172        'clearance': 'Acceptance Fee',
173        }
174
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',
182        'application': 'Application Fee',
183        'transcript': 'Transcript Fee',
184        'late_registration': 'Late Course Registration Fee'
185        }
186
187    def selectable_payment_categories(self, student):
188        return self.PAYMENT_CATEGORIES
189
190    PREVIOUS_PAYMENT_CATEGORIES = deepcopy(PAYMENT_CATEGORIES)
191
192    REPORTABLE_PAYMENT_CATEGORIES = {
193        'schoolfee': 'School Fee',
194        'clearance': 'Acceptance Fee',
195        'hostel_maintenance': 'Hostel Maintenance Fee',
196        'gown': 'Gown Hire Fee',
197        }
198
199    BALANCE_PAYMENT_CATEGORIES = {
200        'schoolfee': 'School Fee',
201        }
202
203    MODE_GROUPS = {
204        'All': ('all',),
205        'Undergraduate Full-Time': ('ug_ft',),
206        'Undergraduate Part-Time': ('ug_pt',),
207        'Postgraduate Full-Time': ('pg_ft',),
208        'Postgraduate Part-Time': ('pg_pt',),
209        }
210
211    VERDICTS_DICT = {
212        '0': _('(not yet)'),
213        'A': 'Successful student',
214        'B': 'Student with carryover courses',
215        'C': 'Student on probation',
216        }
217
218    #: Set positive number for allowed max, negative for required min
219    #: avail.
220    #: Use integer for bytes value, float for percent
221    #: value. `cpu-load`, of course, accepts float values only.
222    #: `swap-mem` = Swap Memory, `virt-mem` = Virtual Memory,
223    #: `cpu-load` = CPU load in percent.
224    SYSTEM_MAX_LOAD = {
225        'swap-mem': None,
226        'virt-mem': None,
227        'cpu-load': 100.0,
228        }
229
230    #: Maximum number of files listed in `finished` subfolder
231    MAX_FILES = 100
232
233    def sendContactForm(self, from_name, from_addr, rcpt_name, rcpt_addr,
234                        from_username, usertype, portal, body, subject):
235        """Send an email with data provided by forms.
236        """
237        config = grok.getSite()['configuration']
238        text = _(u"""Fullname: ${a}
239User Id: ${b}
240User Type: ${c}
241Portal: ${d}
242
243${e}
244""")
245        text = _(text, mapping={
246            'a': from_name,
247            'b': from_username,
248            'c': usertype,
249            'd': portal,
250            'e': body})
251        body = translate(text, 'waeup.kofa',
252            target_language=self.PORTAL_LANGUAGE)
253        if not (from_addr and rcpt_addr):
254            return False
255        return send_mail(
256            from_name, from_addr, rcpt_name, rcpt_addr,
257            subject, body, config)
258
259    @property
260    def tzinfo(self):
261        """Time zone of the university.
262        """
263        # For Nigeria: pytz.timezone('Africa/Lagos')
264        # For Germany: pytz.timezone('Europe/Berlin')
265        return pytz.utc
266
267    def fullname(self, firstname, lastname, middlename=None):
268        """Construct fullname.
269        """
270        # We do not necessarily have the middlename attribute
271        if middlename:
272            name = '%s %s %s' % (firstname, middlename, lastname)
273        else:
274            name = '%s %s' % (firstname, lastname)
275        if '<' in name:
276            return 'XXX'
277        return string.capwords(
278            name.replace('-', ' - ')).replace(' - ', '-')
279
280    def genPassword(self, length=4, chars=string.letters + string.digits):
281        """Generate a random password.
282        """
283        return ''.join([
284            r().choice(string.uppercase) +
285            r().choice(string.lowercase) +
286            r().choice(string.digits) for i in range(length)])
287
288    def sendCredentials(self, user, password=None, url_info=None, msg=None):
289        """Send credentials as email. Input is the user for which credentials
290        are sent and the password. Method returns True or False to indicate
291        successful operation.
292        """
293        subject = 'Your Kofa credentials'
294        text = _(u"""Dear ${a},
295
296${b}
297Student Registration and Information Portal of
298${c}.
299
300Your user name: ${d}
301Your password: ${e}
302${f}
303
304Please remember your user name and keep
305your password secret!
306
307Please also note that passwords are case-sensitive.
308
309Regards
310""")
311        config = grok.getSite()['configuration']
312        from_name = config.name_admin
313        from_addr = config.email_admin
314        rcpt_name = user.title
315        rcpt_addr = user.email
316        text = _(text, mapping={
317            'a': rcpt_name,
318            'b': msg,
319            'c': config.name,
320            'd': user.name,
321            'e': password,
322            'f': url_info})
323
324        body = translate(text, 'waeup.kofa',
325            target_language=self.PORTAL_LANGUAGE)
326        return send_mail(
327            from_name, from_addr, rcpt_name, rcpt_addr,
328            subject, body, config)
329
330    def inviteReferee(self, referee, applicant, url_info=None):
331        """Send invitation email to referee.
332        """
333        config = grok.getSite()['configuration']
334        subject = 'Request for referee report from %s' % config.name
335        text = _(u"""Dear ${a},
336
337The candidate with Id ${b} and name ${c} applied to
338the ${d} to study ${e} for the ${f} session.
339The candidate has listed you as referee. You are thus required to kindly use
340the link below to provide your referral remarks on or before
341${g}.
342
343${h}
344
345Thank You
346
347The Secretary
348Post Graduate School
349${d}
350""")
351        from_name = config.name_admin
352        from_addr = config.email_admin
353        rcpt_name = referee.name
354        rcpt_addr = referee.email
355        session = '%s/%s' % (
356            applicant.__parent__.year, applicant.__parent__.year+1)
357        text = _(text, mapping={
358            'a': rcpt_name,
359            'b': applicant.applicant_id,
360            'c': applicant.display_fullname,
361            'd': config.name,
362            'e': applicant.course1.title,
363            'f': session,
364            'g': applicant.__parent__.enddate,
365            'h': url_info,
366            })
367
368        body = translate(text, 'waeup.kofa',
369            target_language=self.PORTAL_LANGUAGE)
370        return send_mail(
371            from_name, from_addr, rcpt_name, rcpt_addr,
372            subject, body, config)
373
374    def getPaymentItem(self, payment):
375        """Return payment item. This method can be used to customize the
376        `display_item` property attribute, e.g. in order to hide bed coordinates
377        if maintenance fee is not paid.
378        """
379        return payment.p_item
380
381    def expensive_actions_allowed(self, type=None, request=None):
382        """Tell, whether expensive actions are currently allowed.
383        Check system load/health (or other external circumstances) and
384        locally set values to see, whether expensive actions should be
385        allowed (`True`) or better avoided (`False`).
386        Use this to allow or forbid exports, report generations, or
387        similar actions.
388        """
389        max_values = self.SYSTEM_MAX_LOAD
390        for (key, func) in (
391            ('swap-mem', psutil.swap_memory),
392            ('virt-mem', psutil.virtual_memory),
393            ):
394            max_val = max_values.get(key, None)
395            if max_val is None:
396                continue
397            mem_val = func()
398            if isinstance(max_val, float):
399                # percents
400                if max_val < 0.0:
401                    max_val = 100.0 + max_val
402                if mem_val.percent > max_val:
403                    return False
404            else:
405                # number of bytes
406                if max_val < 0:
407                    max_val = mem_val.total + max_val
408                if mem_val.used > max_val:
409                    return False
410        return True
411
412    def export_disabled_message(self):
413        export_disabled_message = grok.getSite()[
414            'configuration'].export_disabled_message
415        if export_disabled_message:
416            return export_disabled_message
417        return None
418
419    def format_float(self, value, prec):
420        # >>> 4.6 * 100
421        # 459.99999999999994
422        value = decimal.Decimal(str(value))
423        # cut floating point value
424        value = int(pow(10, prec)*value) / (1.0*pow(10, prec))
425        return '{:{width}.{prec}f}'.format(value, width=0, prec=prec)
Note: See TracBrowser for help on using the repository browser.