10 """Returns a random 32-bit int ID.
12 return random.randint(-2147483648, 2147483647)
16 """Returns a random 64-bit int ID.
18 return random.randint(-9223372036854775808, 9223372036854775807)
22 """Returns a value that is no smaller than min or larger than max.
35 chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_'
37 """Turns a 32-bit value into a six character alphanumeric code. Useful
38 for shortening JSON data
or use
in URLs.
40 Thanks to http://programanddesign.com/php/base62-encode/
42 val = int(val) + 2147483648
49 code = chars[mod] + code
50 val = (val - mod) / base
63 chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_'
65 """Turns a six character alphanumeric code into a 32-bit value. It's
73 chars = [c for c
in chars]
76 for i
in range(0, base):
80 val = val + (arr[code[i]] * (base ** (l - i - 1)))
82 val = val - 2147483648
88 """Separates a filename into three character segments with the
89 directory separator '/' in between. Useful
for avoiding having
90 millions of small files inside of the same directory, which
is known
91 to severely degrade performance on some filesystems.
98 ls.append(code[i:i + 3])
105 """Returns the best translation found in a dict of different
108 languages should be a list of of ISO 639-1 language codes.
110 defaultlang is set to
'en', so be sure to set it to your native
111 language
if used on other systems.
116 if not isinstance(languages, list):
117 languages = [languages]
120 if l
in translations
and translations[l]:
121 return translations[l]
123 if defaultlang
in translations:
124 return translations[defaultlang]
126 return translations[list(translations.keys())[0]]
130 """Load the translation into the global language dict in g.T
132 languages should be a list of of ISO 639-1 language codes.
134 'en' will always be the last language code on the list.
138 if not isinstance(languages, list):
139 languages = [languages]
141 languages.append(
'en')
143 for lang
in languages:
145 with open(
'static/' + app +
'/translations/' + lang +
'/' + translation +
'.js',
'r')
as f:
149 except Exception
as e:
152 raise Exception(f
"Translation {app}/{translation} was not found for {languages}")
156 """Returns the model object from the correct module.
159 raise Exception(
'GetModel: Missing model name')
161 parts = model.split(
'_')
164 raise Exception(f
"GetModel: {model} doesn't look like a valid model name.")
166 module = importlib.import_module(
'apps.' + parts[0])
168 if model
not in dir(module):
169 raise Exception(f
"GetModel: Model {model} not found in module apps.{parts[0]}")
171 return getattr(module, model)
175 """Helper function to ensure that a b64 string is correctly padded to
182 missing_padding = len(s) % 4
185 s +=
'=' * (4 - missing_padding)
187 return base64.b64decode(s)
191 """Escapes special characters to be used in a SQL query string.
193 Taken from mysql-connector
's version.
208 elif c ==
'\u00a5' or c ==
'\u20a9':
219 """Does a reverse lookup to find the key of a value in a dict.
221 return ls.keys()[ls.values().index(value)]
def KeyFromValue(ls, value)
Does a reverse lookup to find the key of a value in a dict.
def EscapeSQL(s)
Escapes special characters to be used in a SQL query string.
def Bound(val, min, max)
Returns a value that is no smaller than min or larger than max.
def FromShortCode(code, chars='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_')
Turns a six character alphanumeric code into a 32-bit value.
def GetModel(model)
Returns the model object from the correct module.
def ToShortCode(val, chars='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_')
Turns a 32-bit value into a six character alphanumeric code.
def MakeRID()
Returns a random 32-bit int ID.
def BestTranslation(translations, languages, defaultlang='en')
Returns the best translation found in a dict of different translations.
def b64decodepad(s)
Helper function to ensure that a b64 string is correctly padded to four spaces.
def MakeRBID()
Returns a random 64-bit int ID.
def CodeDir(code)
Separates a filename into three character segments with the directory separator '/' in between.
def LoadTranslation(g, app, translation, languages)
Load the translation into the global language dict in g.T.