2018-01-04 03:42:20 +00:00
|
|
|
|
import os
|
2017-11-06 13:45:52 +00:00
|
|
|
|
import re
|
2017-09-19 11:10:50 +00:00
|
|
|
|
import datetime
|
2017-10-01 19:54:34 +00:00
|
|
|
|
import random
|
2017-09-12 03:45:17 +00:00
|
|
|
|
from base64 import b64encode
|
2017-10-01 19:54:34 +00:00
|
|
|
|
from io import BytesIO
|
2015-08-10 04:25:35 +00:00
|
|
|
|
|
2016-11-19 04:32:23 +00:00
|
|
|
|
from django.utils.crypto import get_random_string
|
2017-12-24 03:34:40 +00:00
|
|
|
|
from envelopes import Envelope
|
2017-04-18 18:03:48 +00:00
|
|
|
|
|
|
|
|
|
|
2016-10-29 18:17:35 +00:00
|
|
|
|
def rand_str(length=32, type="lower_hex"):
|
|
|
|
|
"""
|
2016-11-19 04:32:23 +00:00
|
|
|
|
生成指定长度的随机字符串或者数字, 可以用于密钥等安全场景
|
|
|
|
|
:param length: 字符串或者数字的长度
|
|
|
|
|
:param type: str 代表随机字符串,num 代表随机数字
|
|
|
|
|
:return: 字符串
|
2016-10-29 18:17:35 +00:00
|
|
|
|
"""
|
|
|
|
|
if type == "str":
|
2016-11-19 04:32:23 +00:00
|
|
|
|
return get_random_string(length, allowed_chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
|
2016-10-29 18:17:35 +00:00
|
|
|
|
elif type == "lower_str":
|
2016-11-19 04:32:23 +00:00
|
|
|
|
return get_random_string(length, allowed_chars="abcdefghijklmnopqrstuvwxyz0123456789")
|
2016-10-29 18:17:35 +00:00
|
|
|
|
elif type == "lower_hex":
|
2016-11-19 04:32:23 +00:00
|
|
|
|
return random.choice("123456789abcdef") + get_random_string(length - 1, allowed_chars="0123456789abcdef")
|
2016-10-29 18:17:35 +00:00
|
|
|
|
else:
|
2017-01-23 08:01:56 +00:00
|
|
|
|
return random.choice("123456789") + get_random_string(length - 1, allowed_chars="0123456789")
|
2017-05-08 09:29:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def build_query_string(kv_data, ignore_none=True):
|
|
|
|
|
# {"a": 1, "b": "test"} -> "?a=1&b=test"
|
|
|
|
|
query_string = ""
|
2017-05-15 05:09:54 +00:00
|
|
|
|
for k, v in kv_data.items():
|
2017-05-08 09:29:01 +00:00
|
|
|
|
if ignore_none is True and kv_data[k] is None:
|
|
|
|
|
continue
|
|
|
|
|
if query_string != "":
|
|
|
|
|
query_string += "&"
|
|
|
|
|
else:
|
|
|
|
|
query_string = "?"
|
|
|
|
|
query_string += (k + "=" + str(v))
|
|
|
|
|
return query_string
|
2017-09-12 03:45:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def img2base64(img):
|
|
|
|
|
with BytesIO() as buf:
|
|
|
|
|
img.save(buf, "gif")
|
|
|
|
|
buf_str = buf.getvalue()
|
|
|
|
|
img_prefix = "data:image/png;base64,"
|
|
|
|
|
b64_str = img_prefix + b64encode(buf_str).decode("utf-8")
|
2017-09-13 14:37:57 +00:00
|
|
|
|
return b64_str
|
2017-09-16 02:38:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def datetime2str(value, format="iso-8601"):
|
|
|
|
|
if format.lower() == "iso-8601":
|
|
|
|
|
value = value.isoformat()
|
|
|
|
|
if value.endswith("+00:00"):
|
|
|
|
|
value = value[:-6] + "Z"
|
|
|
|
|
return value
|
|
|
|
|
return value.strftime(format)
|
2017-09-19 11:10:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def timestamp2utcstr(value):
|
|
|
|
|
return datetime.datetime.utcfromtimestamp(value).isoformat()
|
2017-11-06 13:45:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def natural_sort_key(s, _nsre=re.compile(r"(\d+)")):
|
|
|
|
|
return [int(text) if text.isdigit() else text.lower()
|
|
|
|
|
for text in re.split(_nsre, s)]
|
2017-12-24 03:34:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def send_email(smtp_config, from_name, to_email, to_name, subject, content):
|
|
|
|
|
envelope = Envelope(from_addr=(smtp_config["email"], from_name),
|
|
|
|
|
to_addr=(to_email, to_name),
|
|
|
|
|
subject=subject,
|
|
|
|
|
html_body=content)
|
|
|
|
|
return envelope.send(smtp_config["server"],
|
|
|
|
|
login=smtp_config["email"],
|
|
|
|
|
password=smtp_config["password"],
|
|
|
|
|
port=smtp_config["port"],
|
|
|
|
|
tls=smtp_config["tls"])
|
2018-01-04 03:42:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_env(name, default=""):
|
|
|
|
|
return os.environ.get(name, default)
|
2019-03-11 08:21:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def DRAMATIQ_WORKER_ARGS(time_limit=3600_000, max_retries=0, max_age=7200_000):
|
|
|
|
|
return {"max_retries": max_retries, "time_limit": time_limit, "max_age": max_age}
|
2019-03-26 01:33:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_is_id(value):
|
|
|
|
|
try:
|
|
|
|
|
return int(value) > 0
|
2019-03-26 02:51:49 +00:00
|
|
|
|
except Exception:
|
2019-03-26 01:33:05 +00:00
|
|
|
|
return False
|