ED_LRR/ed_lrr_gui/web/utils.py

75 lines
2 KiB
Python

# -*- coding: utf-8 -*-
from flask_nav.renderers import Renderer
from dominate import tags
from urllib.parse import urlparse, urljoin
from flask import request
def is_safe_url(target):
ref_url = urlparse(request.host_url)
test_url = urlparse(urljoin(request.host_url, target))
return test_url.scheme in ("http", "https") and ref_url.netloc == test_url.netloc
def dist(p1, p2):
s = 0
for c1, c2 in zip(p1, p2):
s += (c1 - c2) ** 2
return s ** 0.5
class BootsrapRenderer(Renderer):
def visit_Navbar(self, node):
sub = []
for item in node.items:
sub.append(self.visit(item))
return "".join([v.render() for v in sub])
def visit_View(self, node):
classes = ["nav-link"]
if node.active:
classes.append("active")
return tags.li(
tags.a(node.text, href=node.get_url(), cls=" ".join(classes)),
cls="nav-item",
)
def visit_Subgroup(self, node):
# almost the same as visit_Navbar, but written a bit more concise
return tags.div(node.title, *[self.visit(item) for item in node.items])
colors = {
"O": "#0000FF",
"B": "#140AF0",
"A": "#3C1EDC",
"F": "#EEEEEE",
"G": "#969646",
"K": "#B43C1E",
"M": "#FF280A",
"L": "#FF1E00",
"T": "#800000",
"Y": "#800000",
"White Dwarf": "#5D67EF",
"Neutron": "#99A0FF",
}
def prepare_route(route):
entries = []
prev = route[0]
num = 1
for hop in route[1:]:
prev["jump_dist"] = dist(hop["pos"], prev["pos"])
prev["num"] = num
prev["color"] = colors.get(prev["star_type"].split()[0], "#eee")
prev["distance"] = prev["distance"]
entries.append(prev)
prev = hop
num += 1
prev["jump_dist"] = 0
prev["distance"] = prev["distance"]
prev["num"] = num
prev["color"] = colors.get(prev["star_type"].split()[0], "#eee")
entries.append(prev)
return entries