[7358] | 1 | ## $Id: utils.py 12306 2014-12-24 07:38:53Z 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 |
---|
[9866] | 24 | from copy import deepcopy |
---|
[8181] | 25 | from random import SystemRandom as r |
---|
[7734] | 26 | from zope.i18n import translate |
---|
[11949] | 27 | from waeup.ikoba.interfaces import IIkobaUtils |
---|
| 28 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
| 29 | from waeup.ikoba.smtp import send_mail as send_mail_internally |
---|
| 30 | from waeup.ikoba.utils.helpers import get_sorted_preferred |
---|
[7358] | 31 | |
---|
[11814] | 32 | |
---|
| 33 | def send_mail(from_name, from_addr, |
---|
| 34 | rcpt_name, rcpt_addr, |
---|
| 35 | subject, body, config): |
---|
[11949] | 36 | """Wrapper for the real SMTP functionality in :mod:`waeup.ikoba.smtp`. |
---|
[7382] | 37 | |
---|
[7471] | 38 | Merely here to stay compatible with lots of calls to this place. |
---|
[7400] | 39 | """ |
---|
[7471] | 40 | mail_id = send_mail_internally( |
---|
| 41 | from_name, from_addr, rcpt_name, rcpt_addr, |
---|
| 42 | subject, body, config) |
---|
[7399] | 43 | return True |
---|
| 44 | |
---|
[11814] | 45 | |
---|
[7874] | 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. |
---|
| 49 | INT_PHONE_PREFIXES = [ |
---|
| 50 | (99, _('Germany'), '49'), |
---|
[11814] | 51 | (1, _('Nigeria'), '234'), |
---|
[7874] | 52 | (99, _('U.S.'), '1'), |
---|
| 53 | ] |
---|
| 54 | |
---|
[11814] | 55 | |
---|
| 56 | def sorted_phone_prefixes(data=INT_PHONE_PREFIXES, request=None): |
---|
[7874] | 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([ |
---|
[11814] | 75 | ('%s (+%s)' % (x[1], x[2]), '+%s' % x[2]) |
---|
[7874] | 76 | for x in sorted(data) |
---|
| 77 | ]) |
---|
| 78 | |
---|
[11814] | 79 | |
---|
[11949] | 80 | class IkobaUtils(grok.GlobalUtility): |
---|
[7678] | 81 | """A collection of parameters and methods subject to customization. |
---|
[7829] | 82 | |
---|
[7358] | 83 | """ |
---|
[11949] | 84 | grok.implements(IIkobaUtils) |
---|
[7678] | 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). |
---|
[7744] | 88 | PORTAL_LANGUAGE = 'en' |
---|
[7358] | 89 | |
---|
[7701] | 90 | PREFERRED_LANGUAGES_DICT = { |
---|
[11814] | 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'), |
---|
[7701] | 97 | } |
---|
| 98 | |
---|
[7874] | 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) |
---|
[7871] | 103 | |
---|
[12098] | 104 | CON_CATS_DICT = { |
---|
[12092] | 105 | 'sample': 'Sample Category', |
---|
[12078] | 106 | 'license': 'License', |
---|
[12098] | 107 | 'no': 'no contract', |
---|
[12078] | 108 | } |
---|
| 109 | |
---|
[9405] | 110 | PAYMENT_CATEGORIES = { |
---|
[11981] | 111 | 'license': 'License Fee', |
---|
[9405] | 112 | } |
---|
| 113 | |
---|
[11800] | 114 | #: Set positive number for allowed max, negative for required min |
---|
| 115 | #: avail. |
---|
| 116 | #: |
---|
| 117 | #: Use integer for bytes value, float for percent |
---|
| 118 | #: value. `cpu-load`, of course, accepts float values only. |
---|
| 119 | #: `swap-mem` = Swap Memory, `virt-mem` = Virtual Memory, |
---|
[12060] | 120 | #: `cpu-load` = CPU load in percent. |
---|
[11800] | 121 | SYSTEM_MAX_LOAD = { |
---|
| 122 | 'swap-mem': None, |
---|
| 123 | 'virt-mem': None, |
---|
| 124 | 'cpu-load': 100.0, |
---|
| 125 | } |
---|
| 126 | |
---|
[12297] | 127 | EXPORTER_NAMES = ( |
---|
[12283] | 128 | 'pdfdocuments', |
---|
| 129 | 'htmldocuments', |
---|
| 130 | 'users', |
---|
[12297] | 131 | 'products', |
---|
| 132 | 'customers', |
---|
| 133 | 'customersampledocuments', |
---|
| 134 | 'samplecontracts') |
---|
[12283] | 135 | |
---|
| 136 | BATCH_PROCESSOR_NAMES = ( |
---|
| 137 | 'customerprocessor', |
---|
| 138 | 'customersampledocumentprocessor', |
---|
| 139 | 'samplecontractprocessor', |
---|
| 140 | 'productprocessor', |
---|
| 141 | 'pdfdocumentprocessor', |
---|
| 142 | 'htmldocumentprocessor', |
---|
| 143 | 'userprocessor') |
---|
| 144 | |
---|
[11814] | 145 | def sendContactForm(self, from_name, from_addr, rcpt_name, rcpt_addr, |
---|
| 146 | from_username, usertype, portal, body, subject): |
---|
[7358] | 147 | """Send an email with data provided by forms. |
---|
| 148 | """ |
---|
| 149 | config = grok.getSite()['configuration'] |
---|
[7734] | 150 | text = _(u"""Fullname: ${a} |
---|
| 151 | User Id: ${b} |
---|
| 152 | User Type: ${c} |
---|
| 153 | Portal: ${d} |
---|
[7358] | 154 | |
---|
[7734] | 155 | ${e} |
---|
| 156 | """) |
---|
[11814] | 157 | text = _(text, mapping={ |
---|
| 158 | 'a': from_name, |
---|
| 159 | 'b': from_username, |
---|
| 160 | 'c': usertype, |
---|
| 161 | 'd': portal, |
---|
| 162 | 'e': body}) |
---|
[11949] | 163 | body = translate(text, 'waeup.ikoba', |
---|
[7734] | 164 | target_language=self.PORTAL_LANGUAGE) |
---|
[8436] | 165 | if not (from_addr and rcpt_addr): |
---|
| 166 | return False |
---|
[7400] | 167 | return send_mail( |
---|
[11814] | 168 | from_name, from_addr, rcpt_name, rcpt_addr, |
---|
| 169 | subject, body, config) |
---|
[7359] | 170 | |
---|
[8181] | 171 | @property |
---|
| 172 | def tzinfo(self): |
---|
| 173 | # For Nigeria: pytz.timezone('Africa/Lagos') |
---|
[9543] | 174 | # For Germany: pytz.timezone('Europe/Berlin') |
---|
[8181] | 175 | return pytz.utc |
---|
| 176 | |
---|
[11814] | 177 | def fullname(self, firstname, lastname, middlename=None): |
---|
[7477] | 178 | """Full name constructor. |
---|
| 179 | """ |
---|
[7359] | 180 | # We do not necessarily have the middlename attribute |
---|
| 181 | if middlename: |
---|
[8603] | 182 | name = '%s %s %s' % (firstname, middlename, lastname) |
---|
[7359] | 183 | else: |
---|
[8603] | 184 | name = '%s %s' % (firstname, lastname) |
---|
[11814] | 185 | return string.capwords( |
---|
| 186 | name.replace('-', ' - ')).replace(' - ', '-') |
---|
[7365] | 187 | |
---|
| 188 | def genPassword(self, length=8, chars=string.letters + string.digits): |
---|
[7477] | 189 | """Generate a random password. |
---|
| 190 | """ |
---|
[7365] | 191 | return ''.join([r().choice(chars) for i in range(length)]) |
---|
| 192 | |
---|
[8853] | 193 | def sendCredentials(self, user, password=None, url_info=None, msg=None): |
---|
[7399] | 194 | """Send credentials as email. |
---|
| 195 | |
---|
[11947] | 196 | Input is the customer for which credentials are sent and the |
---|
[7399] | 197 | password. |
---|
| 198 | |
---|
| 199 | Returns True or False to indicate successful operation. |
---|
[7365] | 200 | """ |
---|
[11949] | 201 | subject = 'Your Ikoba credentials' |
---|
[7734] | 202 | text = _(u"""Dear ${a}, |
---|
[7365] | 203 | |
---|
[7734] | 204 | ${b} |
---|
[12192] | 205 | Registration and Application Portal of |
---|
[7734] | 206 | ${c}. |
---|
[7365] | 207 | |
---|
[7734] | 208 | Your user name: ${d} |
---|
| 209 | Your password: ${e} |
---|
[8853] | 210 | ${f} |
---|
[7365] | 211 | |
---|
| 212 | Please remember your user name and keep |
---|
| 213 | your password secret! |
---|
| 214 | |
---|
[7382] | 215 | Please also note that passwords are case-sensitive. |
---|
| 216 | |
---|
[7365] | 217 | Regards |
---|
[7734] | 218 | """) |
---|
[7399] | 219 | config = grok.getSite()['configuration'] |
---|
| 220 | from_name = config.name_admin |
---|
[7402] | 221 | from_addr = config.email_admin |
---|
[7407] | 222 | rcpt_name = user.title |
---|
| 223 | rcpt_addr = user.email |
---|
[11814] | 224 | text = _(text, mapping={ |
---|
| 225 | 'a': rcpt_name, |
---|
| 226 | 'b': msg, |
---|
| 227 | 'c': config.name, |
---|
| 228 | 'd': user.name, |
---|
| 229 | 'e': password, |
---|
| 230 | 'f': url_info}) |
---|
[7734] | 231 | |
---|
[11949] | 232 | body = translate(text, 'waeup.ikoba', |
---|
[7734] | 233 | target_language=self.PORTAL_LANGUAGE) |
---|
[7399] | 234 | return send_mail( |
---|
[11814] | 235 | from_name, from_addr, rcpt_name, rcpt_addr, |
---|
| 236 | subject, body, config) |
---|
[9987] | 237 | |
---|
| 238 | def getPaymentItem(self, payment): |
---|
| 239 | """Return payment item. |
---|
| 240 | |
---|
| 241 | This method can be used to customize the display_item property method. |
---|
| 242 | """ |
---|
| 243 | return payment.p_item |
---|
[11815] | 244 | |
---|
| 245 | def expensive_actions_allowed(self, type=None, request=None): |
---|
| 246 | """Tell, whether expensive actions are currently allowed. |
---|
| 247 | |
---|
| 248 | Check system load/health (or other external circumstances) and |
---|
| 249 | locally set values to see, whether expensive actions should be |
---|
| 250 | allowed (`True`) or better avoided (`False`). |
---|
| 251 | |
---|
| 252 | Use this to allow or forbid exports, report generations, or |
---|
| 253 | similar actions. |
---|
| 254 | """ |
---|
| 255 | max_values = self.SYSTEM_MAX_LOAD |
---|
[11816] | 256 | for (key, func) in ( |
---|
| 257 | ('swap-mem', psutil.swap_memory), |
---|
[11818] | 258 | ('virt-mem', psutil.virtual_memory), |
---|
[11816] | 259 | ): |
---|
| 260 | max_val = max_values.get(key, None) |
---|
| 261 | if max_val is None: |
---|
| 262 | continue |
---|
| 263 | mem_val = func() |
---|
[11815] | 264 | if isinstance(max_val, float): |
---|
[11816] | 265 | # percents |
---|
[11821] | 266 | if max_val < 0.0: |
---|
| 267 | max_val = 100.0 + max_val |
---|
[11816] | 268 | if mem_val.percent > max_val: |
---|
[11815] | 269 | return False |
---|
| 270 | else: |
---|
[11816] | 271 | # number of bytes |
---|
[11821] | 272 | if max_val < 0: |
---|
| 273 | max_val = mem_val.total + max_val |
---|
[11816] | 274 | if mem_val.used > max_val: |
---|
[11815] | 275 | return False |
---|
| 276 | return True |
---|