-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
92 lines (70 loc) · 1.88 KB
/
Copy pathutils.py
File metadata and controls
92 lines (70 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import re
RESERVED_SUBDOMAINS = {
"www",
"mail",
"ftp",
"admin",
"api",
"app",
"smtp",
"pop",
"imap",
"i",
"m",
"ns",
"mx",
"ww",
"w",
}
RESERVED_SLUGS = {
"signin",
"signup",
"verify",
"signout",
"subscribe",
"auth",
"micropub",
"feed.xml",
"feed.json",
"blogroll.opml",
"healthz",
}
def is_valid_subdomain(name):
if name in RESERVED_SUBDOMAINS:
return False
return bool(re.match(r"^[a-z0-9]([a-z0-9-]{0,30}[a-z0-9])?$", name))
def slugify(text):
slug = re.sub(r"[^a-z0-9]+", "-", text.lower()).strip("-")
return slug or None
def mask_email(email):
local, domain = email.split("@")
return local[:2] + "****@" + domain
def auto_text_color(bg_hex):
r, g, b = int(bg_hex[1:3], 16), int(bg_hex[3:5], 16), int(bg_hex[5:7], 16)
luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255
return "#444444" if luminance > 0.5 else "#cccccc"
def site_url(site):
if site.get("custom_domain") and site.get("domain_verified_at"):
return f"https://{site['custom_domain']}"
return subdomain_url(site)
def subdomain_url(site, path=""):
from app import app
base = app.config["BASE_DOMAIN"]
scheme = "http" if "localhost" in base else "https"
return f"{scheme}://{site['subdomain']}.{base}{path}"
def host_and_base():
from flask import request
from app import app
host = request.host.split(":")[0]
base = app.config["BASE_DOMAIN"].split(":")[0]
return host, base
def get_current_site():
from db import get_user_by_custom_domain, get_user_by_subdomain
host, base = host_and_base()
suffix = "." + base
if host.endswith(suffix):
subdomain = host.removesuffix(suffix)
return get_user_by_subdomain(subdomain)
if host != base:
return get_user_by_custom_domain(host)
return None