2021-12-01 10:44:15 +00:00
|
|
|
import base64
|
|
|
|
import hashlib
|
|
|
|
import json
|
2023-03-29 18:14:28 +00:00
|
|
|
import re
|
2021-12-20 22:57:27 +00:00
|
|
|
|
|
|
|
from flask import current_app
|
|
|
|
from flask import request
|
2020-11-16 13:42:24 +00:00
|
|
|
|
|
|
|
|
2021-12-01 10:44:15 +00:00
|
|
|
def obj_to_b64(obj):
|
|
|
|
return base64.b64encode(json.dumps(obj).encode("utf-8")).decode("utf-8")
|
|
|
|
|
|
|
|
|
|
|
|
def b64_to_obj(string):
|
|
|
|
return json.loads(base64.b64decode(string.encode("utf-8")).decode("utf-8"))
|
|
|
|
|
|
|
|
|
2023-07-20 16:43:28 +00:00
|
|
|
def build_hash(*args):
|
2021-12-01 10:44:15 +00:00
|
|
|
return hashlib.sha256(
|
|
|
|
current_app.config["SECRET_KEY"].encode("utf-8")
|
2023-11-28 17:51:28 +00:00
|
|
|
+ obj_to_b64(str(args)).encode("utf-8")
|
2021-12-01 10:44:15 +00:00
|
|
|
).hexdigest()
|
|
|
|
|
|
|
|
|
2021-12-08 09:00:36 +00:00
|
|
|
def default_fields():
|
|
|
|
read = set()
|
|
|
|
write = set()
|
|
|
|
for acl in current_app.config["ACL"].values():
|
2021-12-13 22:53:41 +00:00
|
|
|
if "FILTER" not in acl:
|
2021-12-08 09:00:36 +00:00
|
|
|
read |= set(acl.get("READ", []))
|
|
|
|
write |= set(acl.get("WRITE", []))
|
|
|
|
|
|
|
|
return read, write
|
|
|
|
|
|
|
|
|
2022-11-14 17:59:07 +00:00
|
|
|
def get_current_domain():
|
2022-12-20 18:42:09 +00:00
|
|
|
if current_app.config.get("SERVER_NAME"):
|
2022-11-14 17:59:07 +00:00
|
|
|
return current_app.config.get("SERVER_NAME")
|
|
|
|
|
|
|
|
return request.host
|
|
|
|
|
|
|
|
|
2022-12-20 18:42:09 +00:00
|
|
|
def get_current_mail_domain():
|
|
|
|
if current_app.config["SMTP"].get("FROM_ADDR"):
|
|
|
|
return current_app.config["SMTP"]["FROM_ADDR"].split("@")[-1]
|
|
|
|
|
2023-12-14 18:24:09 +00:00
|
|
|
return get_current_domain().split(":")[0]
|
2023-03-29 18:14:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
def validate_uri(value):
|
|
|
|
regex = re.compile(
|
|
|
|
r"^(?:[A-Z0-9\\.-]+)s?://" # scheme + ://
|
|
|
|
r"(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|" # domain...
|
|
|
|
r"[A-Z0-9\\.-]+|" # hostname...
|
|
|
|
r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})" # ...or ip
|
|
|
|
r"(?::\d+)?" # optional port
|
|
|
|
r"(?:/?|[/?]\S+)$",
|
|
|
|
re.IGNORECASE,
|
|
|
|
)
|
|
|
|
return re.match(regex, value) is not None
|
2023-11-15 17:20:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
class classproperty:
|
|
|
|
def __init__(self, f):
|
|
|
|
self.f = f
|
|
|
|
|
|
|
|
def __get__(self, obj, owner):
|
|
|
|
return self.f(owner)
|