[7358] | 1 | ## $Id: utils.py 13802 2016-04-05 21:04:39Z 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 | ## |
---|
[11949] | 18 | """General helper utilities for Ikoba. |
---|
[7358] | 19 | """ |
---|
| 20 | import grok |
---|
[11815] | 21 | import psutil |
---|
[7365] | 22 | import string |
---|
[8181] | 23 | import pytz |
---|
| 24 | from random import SystemRandom as r |
---|
[7734] | 25 | from zope.i18n import translate |
---|
[11949] | 26 | from waeup.ikoba.interfaces import IIkobaUtils |
---|
| 27 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
| 28 | from waeup.ikoba.smtp import send_mail as send_mail_internally |
---|
[12553] | 29 | from waeup.ikoba.utils.helpers import ( |
---|
| 30 | get_sorted_preferred, get_current_principal) |
---|
[12378] | 31 | from waeup.ikoba.payments.currencies import ISO_4217_CURRENCIES |
---|
[7358] | 32 | |
---|
[11814] | 33 | |
---|
| 34 | def send_mail(from_name, from_addr, |
---|
| 35 | rcpt_name, rcpt_addr, |
---|
| 36 | subject, body, config): |
---|
[11949] | 37 | """Wrapper for the real SMTP functionality in :mod:`waeup.ikoba.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 | |
---|
[11949] | 81 | class IkobaUtils(grok.GlobalUtility): |
---|
[7678] | 82 | """A collection of parameters and methods subject to customization. |
---|
[7829] | 83 | |
---|
[7358] | 84 | """ |
---|
[11949] | 85 | grok.implements(IIkobaUtils) |
---|
[7678] | 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). |
---|
[7744] | 89 | PORTAL_LANGUAGE = 'en' |
---|
[7358] | 90 | |
---|
[7701] | 91 | PREFERRED_LANGUAGES_DICT = { |
---|
[11814] | 92 | 'en': (1, u'English'), |
---|
| 93 | 'fr': (2, u'Français'), |
---|
| 94 | 'de': (3, u'Deutsch'), |
---|
| 95 | 'ha': (4, u'Hausa'), |
---|
| 96 | 'yo': (5, u'Yoruba'), |
---|
| 97 | 'ig': (6, u'Igbo'), |
---|
[7701] | 98 | } |
---|
| 99 | |
---|
[12378] | 100 | # CURRENCIES must be equal to or an excerpt of ISO_4217_CURRENCIES |
---|
| 101 | CURRENCIES = ISO_4217_CURRENCIES |
---|
| 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 | |
---|
[12098] | 108 | CON_CATS_DICT = { |
---|
[12092] | 109 | 'sample': 'Sample Category', |
---|
[12078] | 110 | 'license': 'License', |
---|
[12545] | 111 | 'no': _('no contract'), |
---|
[12078] | 112 | } |
---|
| 113 | |
---|
[9405] | 114 | PAYMENT_CATEGORIES = { |
---|
[11981] | 115 | 'license': 'License Fee', |
---|
[9405] | 116 | } |
---|
| 117 | |
---|
[11800] | 118 | #: Set positive number for allowed max, negative for required min |
---|
| 119 | #: avail. |
---|
| 120 | #: |
---|
| 121 | #: Use integer for bytes value, float for percent |
---|
| 122 | #: value. `cpu-load`, of course, accepts float values only. |
---|
| 123 | #: `swap-mem` = Swap Memory, `virt-mem` = Virtual Memory, |
---|
[12060] | 124 | #: `cpu-load` = CPU load in percent. |
---|
[11800] | 125 | SYSTEM_MAX_LOAD = { |
---|
| 126 | 'swap-mem': None, |
---|
| 127 | 'virt-mem': None, |
---|
| 128 | 'cpu-load': 100.0, |
---|
| 129 | } |
---|
| 130 | |
---|
[12297] | 131 | EXPORTER_NAMES = ( |
---|
[12283] | 132 | 'pdfdocuments', |
---|
| 133 | 'htmldocuments', |
---|
[12410] | 134 | 'restdocuments', |
---|
[12283] | 135 | 'users', |
---|
[12297] | 136 | 'products', |
---|
[12777] | 137 | 'payments', |
---|
[12297] | 138 | 'customers', |
---|
| 139 | 'customersampledocuments', |
---|
| 140 | 'samplecontracts') |
---|
[12283] | 141 | |
---|
| 142 | BATCH_PROCESSOR_NAMES = ( |
---|
| 143 | 'customerprocessor', |
---|
| 144 | 'customersampledocumentprocessor', |
---|
| 145 | 'samplecontractprocessor', |
---|
| 146 | 'productprocessor', |
---|
| 147 | 'pdfdocumentprocessor', |
---|
| 148 | 'htmldocumentprocessor', |
---|
[12410] | 149 | 'restdocumentprocessor', |
---|
[12283] | 150 | 'userprocessor') |
---|
| 151 | |
---|
[11814] | 152 | def sendContactForm(self, from_name, from_addr, rcpt_name, rcpt_addr, |
---|
| 153 | from_username, usertype, portal, body, subject): |
---|
[7358] | 154 | """Send an email with data provided by forms. |
---|
| 155 | """ |
---|
| 156 | config = grok.getSite()['configuration'] |
---|
[7734] | 157 | text = _(u"""Fullname: ${a} |
---|
| 158 | User Id: ${b} |
---|
| 159 | User Type: ${c} |
---|
| 160 | Portal: ${d} |
---|
[7358] | 161 | |
---|
[7734] | 162 | ${e} |
---|
| 163 | """) |
---|
[11814] | 164 | text = _(text, mapping={ |
---|
| 165 | 'a': from_name, |
---|
| 166 | 'b': from_username, |
---|
| 167 | 'c': usertype, |
---|
| 168 | 'd': portal, |
---|
| 169 | 'e': body}) |
---|
[11949] | 170 | body = translate(text, 'waeup.ikoba', |
---|
[7734] | 171 | target_language=self.PORTAL_LANGUAGE) |
---|
[8436] | 172 | if not (from_addr and rcpt_addr): |
---|
| 173 | return False |
---|
[7400] | 174 | return send_mail( |
---|
[11814] | 175 | from_name, from_addr, rcpt_name, rcpt_addr, |
---|
| 176 | subject, body, config) |
---|
[7359] | 177 | |
---|
[8181] | 178 | @property |
---|
| 179 | def tzinfo(self): |
---|
| 180 | # For Nigeria: pytz.timezone('Africa/Lagos') |
---|
[9543] | 181 | # For Germany: pytz.timezone('Europe/Berlin') |
---|
[8181] | 182 | return pytz.utc |
---|
| 183 | |
---|
[11814] | 184 | def fullname(self, firstname, lastname, middlename=None): |
---|
[7477] | 185 | """Full name constructor. |
---|
| 186 | """ |
---|
[7359] | 187 | # We do not necessarily have the middlename attribute |
---|
| 188 | if middlename: |
---|
[8603] | 189 | name = '%s %s %s' % (firstname, middlename, lastname) |
---|
[7359] | 190 | else: |
---|
[8603] | 191 | name = '%s %s' % (firstname, lastname) |
---|
[11814] | 192 | return string.capwords( |
---|
| 193 | name.replace('-', ' - ')).replace(' - ', '-') |
---|
[7365] | 194 | |
---|
| 195 | def genPassword(self, length=8, chars=string.letters + string.digits): |
---|
[7477] | 196 | """Generate a random password. |
---|
| 197 | """ |
---|
[7365] | 198 | return ''.join([r().choice(chars) for i in range(length)]) |
---|
| 199 | |
---|
[8853] | 200 | def sendCredentials(self, user, password=None, url_info=None, msg=None): |
---|
[7399] | 201 | """Send credentials as email. |
---|
| 202 | |
---|
[11947] | 203 | Input is the customer for which credentials are sent and the |
---|
[7399] | 204 | password. |
---|
| 205 | |
---|
| 206 | Returns True or False to indicate successful operation. |
---|
[7365] | 207 | """ |
---|
[11949] | 208 | subject = 'Your Ikoba credentials' |
---|
[7734] | 209 | text = _(u"""Dear ${a}, |
---|
[7365] | 210 | |
---|
[7734] | 211 | ${b} |
---|
[12192] | 212 | Registration and Application Portal of |
---|
[7734] | 213 | ${c}. |
---|
[7365] | 214 | |
---|
[7734] | 215 | Your user name: ${d} |
---|
| 216 | Your password: ${e} |
---|
[8853] | 217 | ${f} |
---|
[7365] | 218 | |
---|
| 219 | Please remember your user name and keep |
---|
| 220 | your password secret! |
---|
| 221 | |
---|
[7382] | 222 | Please also note that passwords are case-sensitive. |
---|
| 223 | |
---|
[7365] | 224 | Regards |
---|
[7734] | 225 | """) |
---|
[7399] | 226 | config = grok.getSite()['configuration'] |
---|
| 227 | from_name = config.name_admin |
---|
[7402] | 228 | from_addr = config.email_admin |
---|
[7407] | 229 | rcpt_name = user.title |
---|
| 230 | rcpt_addr = user.email |
---|
[11814] | 231 | text = _(text, mapping={ |
---|
| 232 | 'a': rcpt_name, |
---|
| 233 | 'b': msg, |
---|
| 234 | 'c': config.name, |
---|
| 235 | 'd': user.name, |
---|
| 236 | 'e': password, |
---|
| 237 | 'f': url_info}) |
---|
[7734] | 238 | |
---|
[11949] | 239 | body = translate(text, 'waeup.ikoba', |
---|
[7734] | 240 | target_language=self.PORTAL_LANGUAGE) |
---|
[7399] | 241 | return send_mail( |
---|
[11814] | 242 | from_name, from_addr, rcpt_name, rcpt_addr, |
---|
| 243 | subject, body, config) |
---|
[9987] | 244 | |
---|
[12553] | 245 | def sendTransitionInfo(self, customer, obj, msg=None): |
---|
| 246 | """Send transition information as email. |
---|
[9987] | 247 | |
---|
[12553] | 248 | Input is the customer for which credentials are sent and the object |
---|
| 249 | which was transitioned. |
---|
| 250 | |
---|
| 251 | Returns True or False to indicate successful operation. |
---|
[9987] | 252 | """ |
---|
[12553] | 253 | config = grok.getSite()['configuration'] |
---|
| 254 | if not config.email_notification: |
---|
| 255 | return |
---|
| 256 | subject = 'Ikoba status change information' |
---|
| 257 | text = _(u"""Dear ${a}, |
---|
[11815] | 258 | |
---|
[12553] | 259 | The status of the following object has been changed: |
---|
| 260 | |
---|
| 261 | Object Id: ${c} |
---|
| 262 | Title: ${d} |
---|
| 263 | Transition: ${e} |
---|
| 264 | New state: ${f} |
---|
| 265 | |
---|
| 266 | Regards, |
---|
| 267 | |
---|
| 268 | ${b} |
---|
| 269 | |
---|
| 270 | -- |
---|
| 271 | ${g} |
---|
| 272 | """) |
---|
| 273 | from_name = config.name_admin |
---|
| 274 | from_addr = config.email_admin |
---|
| 275 | rcpt_name = customer.title |
---|
| 276 | rcpt_addr = customer.email |
---|
| 277 | |
---|
| 278 | user = get_current_principal() |
---|
| 279 | if user is None: |
---|
| 280 | usertitle = 'system' |
---|
| 281 | elif user.id == 'zope.anybody': |
---|
| 282 | usertitle = 'Anonymous' |
---|
| 283 | else: |
---|
| 284 | usertitle = getattr(user, 'public_name', None) |
---|
| 285 | if not usertitle: |
---|
| 286 | usertitle = user.title |
---|
| 287 | |
---|
| 288 | msg = translate(msg,'waeup.ikoba', target_language=self.PORTAL_LANGUAGE) |
---|
| 289 | new_state = translate(obj.translated_state,'waeup.ikoba', |
---|
| 290 | target_language=self.PORTAL_LANGUAGE) |
---|
| 291 | |
---|
| 292 | text = _(text, mapping={ |
---|
| 293 | 'a': rcpt_name, |
---|
| 294 | 'b': usertitle, |
---|
| 295 | 'c': obj.__name__, |
---|
| 296 | 'd': obj.title, |
---|
| 297 | 'e': msg.lower(), |
---|
| 298 | 'f': new_state, |
---|
| 299 | 'g': config.name}) |
---|
| 300 | |
---|
| 301 | body = translate(text, 'waeup.ikoba', |
---|
| 302 | target_language=self.PORTAL_LANGUAGE) |
---|
| 303 | |
---|
| 304 | return send_mail( |
---|
| 305 | from_name, from_addr, rcpt_name, rcpt_addr, |
---|
| 306 | subject, body, config) |
---|
| 307 | |
---|
[11815] | 308 | def expensive_actions_allowed(self, type=None, request=None): |
---|
| 309 | """Tell, whether expensive actions are currently allowed. |
---|
| 310 | |
---|
| 311 | Check system load/health (or other external circumstances) and |
---|
| 312 | locally set values to see, whether expensive actions should be |
---|
| 313 | allowed (`True`) or better avoided (`False`). |
---|
| 314 | |
---|
| 315 | Use this to allow or forbid exports, report generations, or |
---|
| 316 | similar actions. |
---|
| 317 | """ |
---|
| 318 | max_values = self.SYSTEM_MAX_LOAD |
---|
[11816] | 319 | for (key, func) in ( |
---|
| 320 | ('swap-mem', psutil.swap_memory), |
---|
[11818] | 321 | ('virt-mem', psutil.virtual_memory), |
---|
[11816] | 322 | ): |
---|
| 323 | max_val = max_values.get(key, None) |
---|
| 324 | if max_val is None: |
---|
| 325 | continue |
---|
| 326 | mem_val = func() |
---|
[11815] | 327 | if isinstance(max_val, float): |
---|
[11816] | 328 | # percents |
---|
[11821] | 329 | if max_val < 0.0: |
---|
| 330 | max_val = 100.0 + max_val |
---|
[11816] | 331 | if mem_val.percent > max_val: |
---|
[11815] | 332 | return False |
---|
| 333 | else: |
---|
[11816] | 334 | # number of bytes |
---|
[11821] | 335 | if max_val < 0: |
---|
| 336 | max_val = mem_val.total + max_val |
---|
[11816] | 337 | if mem_val.used > max_val: |
---|
[11815] | 338 | return False |
---|
| 339 | return True |
---|
[13802] | 340 | |
---|
| 341 | def export_disabled_message(self): |
---|
| 342 | export_disabled_message = grok.getSite()[ |
---|
| 343 | 'configuration'].export_disabled_message |
---|
| 344 | if export_disabled_message: |
---|
| 345 | return export_disabled_message |
---|
| 346 | return None |
---|