source: main/waeup.ikoba/trunk/src/waeup/ikoba/utils/utils.py @ 12078

Last change on this file since 12078 was 12078, checked in by Henrik Bettermann, 10 years ago

Add application_category attribute to products.

  • Property svn:keywords set to Id
File size: 8.3 KB
Line 
1## $Id: utils.py 12078 2014-11-28 08:24:49Z 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 Ikoba.
19"""
20import grok
21import psutil
22import string
23import pytz
24from copy import deepcopy
25from random import SystemRandom as r
26from zope.i18n import translate
27from waeup.ikoba.interfaces import IIkobaUtils
28from waeup.ikoba.interfaces import MessageFactory as _
29from waeup.ikoba.smtp import send_mail as send_mail_internally
30from waeup.ikoba.utils.helpers import get_sorted_preferred
31
32
33def send_mail(from_name, from_addr,
34              rcpt_name, rcpt_addr,
35              subject, body, config):
36    """Wrapper for the real SMTP functionality in :mod:`waeup.ikoba.smtp`.
37
38    Merely here to stay compatible with lots of calls to this place.
39    """
40    mail_id = send_mail_internally(
41        from_name, from_addr, rcpt_name, rcpt_addr,
42        subject, body, config)
43    return True
44
45
46#: A list of phone prefixes (order num, country, prefix).
47#: Items with same order num will be sorted alphabetically.
48#: The lower the order num, the higher the precedence.
49INT_PHONE_PREFIXES = [
50    (99, _('Germany'), '49'),
51    (1, _('Nigeria'), '234'),
52    (99, _('U.S.'), '1'),
53    ]
54
55
56def sorted_phone_prefixes(data=INT_PHONE_PREFIXES, request=None):
57    """Sorted tuples of phone prefixes.
58
59    Ordered as shown above and formatted for use in select boxes.
60
61    If request is given, we'll try to translate all country names in
62    order to sort alphabetically correctly.
63
64    XXX: This is a function (and not a constant) as different
65    languages might give different orders. This is not tested yet.
66
67    XXX: If we really want to use alphabetic ordering here, we might
68    think about caching results of translations.
69    """
70    if request is not None:
71        data = [
72            (x, translate(y, context=request), z)
73            for x, y, z in data]
74    return tuple([
75        ('%s (+%s)' % (x[1], x[2]), '+%s' % x[2])
76        for x in sorted(data)
77        ])
78
79
80class IkobaUtils(grok.GlobalUtility):
81    """A collection of parameters and methods subject to customization.
82
83    """
84    grok.implements(IIkobaUtils)
85    # This the only place where we define the portal language
86    # which is used for the translation of system messages
87    # (e.g. object histories).
88    PORTAL_LANGUAGE = 'en'
89
90    PREFERRED_LANGUAGES_DICT = {
91        'en': (1, u'English'),
92        'fr': (2, u'Français'),
93        'de': (3, u'Deutsch'),
94        'ha': (4, u'Hausa'),
95        'yo': (5, u'Yoruba'),
96        'ig': (6, u'Igbo'),
97        }
98
99    #: A function to return
100    @classmethod
101    def sorted_phone_prefixes(cls, data=INT_PHONE_PREFIXES, request=None):
102        return sorted_phone_prefixes(data, request)
103
104    APP_CATS_DICT = {
105        'license': 'License',
106        'no': 'no application',
107        }
108
109    EXAM_SUBJECTS_DICT = {
110        'math': 'Mathematics',
111        'computer_science': 'Computer Science',
112        }
113
114    #: Exam grades. The tuple is sorted as it should be displayed in
115    #: select boxes.
116    EXAM_GRADES = (
117        ('A', 'Best'),
118        ('B', 'Better'),
119        ('C', 'Good'),
120        )
121
122    PAYMENT_CATEGORIES = {
123        'license': 'License Fee',
124        }
125
126    #: Set positive number for allowed max, negative for required min
127    #: avail.
128    #:
129    #: Use integer for bytes value, float for percent
130    #: value. `cpu-load`, of course, accepts float values only.
131    #: `swap-mem` = Swap Memory, `virt-mem` = Virtual Memory,
132    #: `cpu-load` = CPU load in percent.
133    SYSTEM_MAX_LOAD = {
134        'swap-mem': None,
135        'virt-mem': None,
136        'cpu-load': 100.0,
137        }
138
139    def sendContactForm(self, from_name, from_addr, rcpt_name, rcpt_addr,
140                        from_username, usertype, portal, body, subject):
141        """Send an email with data provided by forms.
142        """
143        config = grok.getSite()['configuration']
144        text = _(u"""Fullname: ${a}
145User Id: ${b}
146User Type: ${c}
147Portal: ${d}
148
149${e}
150""")
151        text = _(text, mapping={
152            'a': from_name,
153            'b': from_username,
154            'c': usertype,
155            'd': portal,
156            'e': body})
157        body = translate(text, 'waeup.ikoba',
158            target_language=self.PORTAL_LANGUAGE)
159        if not (from_addr and rcpt_addr):
160            return False
161        return send_mail(
162            from_name, from_addr, rcpt_name, rcpt_addr,
163            subject, body, config)
164
165    @property
166    def tzinfo(self):
167        # For Nigeria: pytz.timezone('Africa/Lagos')
168        # For Germany: pytz.timezone('Europe/Berlin')
169        return pytz.utc
170
171    def fullname(self, firstname, lastname, middlename=None):
172        """Full name constructor.
173        """
174        # We do not necessarily have the middlename attribute
175        if middlename:
176            name = '%s %s %s' % (firstname, middlename, lastname)
177        else:
178            name = '%s %s' % (firstname, lastname)
179        return string.capwords(
180            name.replace('-', ' - ')).replace(' - ', '-')
181
182    def genPassword(self, length=8, chars=string.letters + string.digits):
183        """Generate a random password.
184        """
185        return ''.join([r().choice(chars) for i in range(length)])
186
187    def sendCredentials(self, user, password=None, url_info=None, msg=None):
188        """Send credentials as email.
189
190        Input is the customer for which credentials are sent and the
191        password.
192
193        Returns True or False to indicate successful operation.
194        """
195        subject = 'Your Ikoba credentials'
196        text = _(u"""Dear ${a},
197
198${b}
199Application and Registration Portal of
200${c}.
201
202Your user name: ${d}
203Your password: ${e}
204${f}
205
206Please remember your user name and keep
207your password secret!
208
209Please also note that passwords are case-sensitive.
210
211Regards
212""")
213        config = grok.getSite()['configuration']
214        from_name = config.name_admin
215        from_addr = config.email_admin
216        rcpt_name = user.title
217        rcpt_addr = user.email
218        text = _(text, mapping={
219            'a': rcpt_name,
220            'b': msg,
221            'c': config.name,
222            'd': user.name,
223            'e': password,
224            'f': url_info})
225
226        body = translate(text, 'waeup.ikoba',
227            target_language=self.PORTAL_LANGUAGE)
228        return send_mail(
229            from_name, from_addr, rcpt_name, rcpt_addr,
230            subject, body, config)
231
232    def getPaymentItem(self, payment):
233        """Return payment item.
234
235        This method can be used to customize the display_item property method.
236        """
237        return payment.p_item
238
239    def expensive_actions_allowed(self, type=None, request=None):
240        """Tell, whether expensive actions are currently allowed.
241
242        Check system load/health (or other external circumstances) and
243        locally set values to see, whether expensive actions should be
244        allowed (`True`) or better avoided (`False`).
245
246        Use this to allow or forbid exports, report generations, or
247        similar actions.
248        """
249        max_values = self.SYSTEM_MAX_LOAD
250        for (key, func) in (
251            ('swap-mem', psutil.swap_memory),
252            ('virt-mem', psutil.virtual_memory),
253            ):
254            max_val = max_values.get(key, None)
255            if max_val is None:
256                continue
257            mem_val = func()
258            if isinstance(max_val, float):
259                # percents
260                if max_val < 0.0:
261                    max_val = 100.0 + max_val
262                if mem_val.percent > max_val:
263                    return False
264            else:
265                # number of bytes
266                if max_val < 0:
267                    max_val = mem_val.total + max_val
268                if mem_val.used > max_val:
269                    return False
270        return True
Note: See TracBrowser for help on using the repository browser.