mirror of
https://github.com/checktheroads/hyperglass
synced 2024-05-11 05:55:08 +00:00
refactor front end params
This commit is contained in:
@@ -18,201 +18,242 @@ from pydantic.color import Color
|
||||
# Project Imports
|
||||
from hyperglass.configuration.models._utils import HyperglassModel
|
||||
from hyperglass.configuration.models.opengraph import OpenGraph
|
||||
from hyperglass.constants import FUNC_COLOR_MAP
|
||||
|
||||
|
||||
class Web(HyperglassModel):
|
||||
"""Validation model for params.branding."""
|
||||
class Analytics(HyperglassModel):
|
||||
"""Validation model for Google Analytics."""
|
||||
|
||||
class Analytics(HyperglassModel):
|
||||
"""Validation model for Google Analytics."""
|
||||
enable: StrictBool = False
|
||||
id: Optional[StrictStr]
|
||||
|
||||
enable: StrictBool = False
|
||||
id: Optional[StrictStr]
|
||||
@validator("id")
|
||||
def validate_id(cls, value, values):
|
||||
"""Ensure ID is set if analytics is enabled.
|
||||
|
||||
@validator("id")
|
||||
def validate_id(cls, value, values):
|
||||
"""Ensure ID is set if analytics is enabled.
|
||||
Arguments:
|
||||
value {str|None} -- Google Analytics ID
|
||||
values {[type]} -- Already-validated model parameters
|
||||
|
||||
Arguments:
|
||||
value {str|None} -- Google Analytics ID
|
||||
values {[type]} -- Already-validated model parameters
|
||||
Raises:
|
||||
ValueError: Raised if analytics is enabled but no ID is set.
|
||||
|
||||
Raises:
|
||||
ValueError: Raised if analytics is enabled but no ID is set.
|
||||
Returns:
|
||||
{str|None} -- Google Analytics ID if enabled.
|
||||
"""
|
||||
if values["enable"] and value is None:
|
||||
raise ValueError("Analytics is enabled, but no ID is set.")
|
||||
return value
|
||||
|
||||
Returns:
|
||||
{str|None} -- Google Analytics ID if enabled.
|
||||
"""
|
||||
if values["enable"] and value is None:
|
||||
raise ValueError("Analytics is enabled, but no ID is set.")
|
||||
return value
|
||||
|
||||
class Credit(HyperglassModel):
|
||||
"""Validation model for developer credit."""
|
||||
|
||||
enable: StrictBool = True
|
||||
|
||||
|
||||
class ExternalLink(HyperglassModel):
|
||||
"""Validation model for external link."""
|
||||
|
||||
enable: StrictBool = True
|
||||
title: StrictStr = "PeeringDB"
|
||||
url: HttpUrl = "https://www.peeringdb.com/AS{primary_asn}"
|
||||
|
||||
|
||||
class Font(HyperglassModel):
|
||||
"""Validation model for params.branding.font."""
|
||||
|
||||
class Primary(HyperglassModel):
|
||||
"""Validation model for params.branding.font.primary."""
|
||||
|
||||
name: StrictStr = "Nunito"
|
||||
size: StrictStr = "1rem"
|
||||
|
||||
class Mono(HyperglassModel):
|
||||
"""Validation model for params.branding.font.mono."""
|
||||
|
||||
name: StrictStr = "Fira Code"
|
||||
size: StrictStr = "87.5%"
|
||||
|
||||
primary: Primary = Primary()
|
||||
mono: Mono = Mono()
|
||||
|
||||
|
||||
class HelpMenu(HyperglassModel):
|
||||
"""Validation model for generic help menu."""
|
||||
|
||||
enable: StrictBool = True
|
||||
file: Optional[FilePath]
|
||||
title: StrictStr = "Help"
|
||||
|
||||
|
||||
class Logo(HyperglassModel):
|
||||
"""Validation model for logo configuration."""
|
||||
|
||||
light: Optional[FilePath]
|
||||
dark: Optional[FilePath]
|
||||
width: StrictInt = 384
|
||||
height: Optional[StrictInt]
|
||||
favicons: StrictStr = "ui/images/favicons/"
|
||||
|
||||
@validator("favicons")
|
||||
def favicons_trailing_slash(cls, value):
|
||||
"""If the favicons path does not end in a '/', append it."""
|
||||
chars = list(value)
|
||||
if chars[len(chars) - 1] != "/":
|
||||
chars.append("/")
|
||||
return "".join(chars)
|
||||
|
||||
@root_validator(pre=True)
|
||||
def validate_logo_model(cls, values):
|
||||
"""Set default opengraph image location.
|
||||
|
||||
Arguments:
|
||||
values {dict} -- Unvalidated model
|
||||
|
||||
Returns:
|
||||
{dict} -- Modified model
|
||||
"""
|
||||
logo_light = values.get("light")
|
||||
logo_dark = values.get("dark")
|
||||
default_logo_light = (
|
||||
Path(__file__).parent.parent.parent / "static/images/hyperglass-light.png"
|
||||
)
|
||||
default_logo_dark = (
|
||||
Path(__file__).parent.parent.parent / "static/images/hyperglass-dark.png"
|
||||
)
|
||||
|
||||
# Use light logo as dark logo if dark logo is undefined.
|
||||
if logo_light is not None and logo_dark is None:
|
||||
values["dark"] = logo_light
|
||||
|
||||
# Use dark logo as light logo if light logo is undefined.
|
||||
if logo_dark is not None and logo_light is None:
|
||||
values["light"] = logo_dark
|
||||
|
||||
# Set default logo paths if logo is undefined.
|
||||
if logo_light is None and logo_dark is None:
|
||||
values["light"] = default_logo_light
|
||||
values["dark"] = default_logo_dark
|
||||
|
||||
return values
|
||||
|
||||
@validator("light", "dark")
|
||||
def validate_logos(cls, value):
|
||||
"""Convert file path to URL path.
|
||||
|
||||
Arguments:
|
||||
value {FilePath} -- Path to logo file.
|
||||
|
||||
Returns:
|
||||
{str} -- Formatted logo path
|
||||
"""
|
||||
return "".join(str(value).split("static")[1::])
|
||||
|
||||
class Config:
|
||||
"""Override pydantic config."""
|
||||
|
||||
fields = {"logo_path": "path"}
|
||||
|
||||
|
||||
class Terms(HyperglassModel):
|
||||
"""Validation model for terms & conditions."""
|
||||
|
||||
enable: StrictBool = True
|
||||
file: Optional[FilePath]
|
||||
title: StrictStr = "Terms"
|
||||
|
||||
|
||||
class Text(HyperglassModel):
|
||||
"""Validation model for params.branding.text."""
|
||||
|
||||
title_mode: constr(regex=("logo_only|text_only|logo_title|all")) = "logo_only"
|
||||
title: StrictStr = "hyperglass"
|
||||
subtitle: StrictStr = "AS{primary_asn}"
|
||||
query_location: StrictStr = "Location"
|
||||
query_type: StrictStr = "Query Type"
|
||||
query_target: StrictStr = "Target"
|
||||
query_vrf: StrictStr = "Routing Table"
|
||||
fqdn_tooltip: StrictStr = "Use {protocol}" # Formatted by Javascript
|
||||
cache: StrictStr = "Results will be cached for {timeout} {period}."
|
||||
|
||||
class Error404(HyperglassModel):
|
||||
"""Validation model for 404 Error Page."""
|
||||
|
||||
title: StrictStr = "Error"
|
||||
subtitle: StrictStr = "{uri} isn't a thing"
|
||||
button: StrictStr = "Home"
|
||||
|
||||
class Error500(HyperglassModel):
|
||||
"""Validation model for 500 Error Page."""
|
||||
|
||||
title: StrictStr = "Error"
|
||||
subtitle: StrictStr = "Something Went Wrong"
|
||||
button: StrictStr = "Home"
|
||||
|
||||
error404: Error404 = Error404()
|
||||
error500: Error500 = Error500()
|
||||
|
||||
|
||||
class Theme(HyperglassModel):
|
||||
"""Validation model for theme variables."""
|
||||
|
||||
class Colors(HyperglassModel):
|
||||
"""Validation model for params.colors."""
|
||||
"""Validation model for theme colors."""
|
||||
|
||||
primary: Color = "#40798c"
|
||||
secondary: Color = "#330036"
|
||||
danger: Color = "#a21024"
|
||||
warning: Color = "#eec643"
|
||||
light: Color = "#fbfffe"
|
||||
dark: Color = "#383541"
|
||||
background: Color = "#fbfffe"
|
||||
black: Color = "#262626"
|
||||
white: Color = "#f7f7f7"
|
||||
gray: Color = "#c1c7cc"
|
||||
red: Color = "#d84b4b"
|
||||
orange: Color = "ff6b35"
|
||||
yellow: Color = "#edae49"
|
||||
green: Color = "#35b246"
|
||||
blue: Color = "#314cb6"
|
||||
teal: Color = "#35b299"
|
||||
cyan: Color = "#118ab2"
|
||||
pink: Color = "#f2607d"
|
||||
purple: Color = "#8d30b5"
|
||||
primary: Optional[Color]
|
||||
secondary: Optional[Color]
|
||||
success: Optional[Color]
|
||||
warning: Optional[Color]
|
||||
error: Optional[Color]
|
||||
danger: Optional[Color]
|
||||
|
||||
@validator(*FUNC_COLOR_MAP.keys(), pre=True, always=True)
|
||||
def validate_colors(cls, value, values, field):
|
||||
"""Set default functional color mapping.
|
||||
|
||||
Arguments:
|
||||
value {str|None} -- Functional color
|
||||
values {str} -- Already-validated colors
|
||||
|
||||
Returns:
|
||||
{str} -- Mapped color.
|
||||
"""
|
||||
|
||||
if value is None:
|
||||
default_color = FUNC_COLOR_MAP[field.name]
|
||||
value = str(values[default_color])
|
||||
return value
|
||||
|
||||
def dict(self, *args, **kwargs):
|
||||
"""Return dict for colors only."""
|
||||
_dict = {}
|
||||
for k, v in self.__dict__.items():
|
||||
_dict.update({k: v.as_hex()})
|
||||
return _dict
|
||||
return {k: v.as_hex() for k, v in self.__dict__.items()}
|
||||
|
||||
class Credit(HyperglassModel):
|
||||
"""Validation model for params.branding.credit."""
|
||||
class Fonts(HyperglassModel):
|
||||
"""Validation model for theme fonts."""
|
||||
|
||||
enable: StrictBool = True
|
||||
|
||||
class Font(HyperglassModel):
|
||||
"""Validation model for params.branding.font."""
|
||||
|
||||
class Primary(HyperglassModel):
|
||||
"""Validation model for params.branding.font.primary."""
|
||||
|
||||
name: StrictStr = "Nunito"
|
||||
size: StrictStr = "1rem"
|
||||
|
||||
class Mono(HyperglassModel):
|
||||
"""Validation model for params.branding.font.mono."""
|
||||
|
||||
name: StrictStr = "Fira Code"
|
||||
size: StrictStr = "87.5%"
|
||||
|
||||
primary: Primary = Primary()
|
||||
mono: Mono = Mono()
|
||||
|
||||
class HelpMenu(HyperglassModel):
|
||||
"""Validation model for params.branding.help_menu."""
|
||||
|
||||
enable: StrictBool = True
|
||||
file: Optional[FilePath]
|
||||
title: StrictStr = "Help"
|
||||
|
||||
class Logo(HyperglassModel):
|
||||
"""Validation model for params.branding.logo."""
|
||||
|
||||
light: Optional[FilePath]
|
||||
dark: Optional[FilePath]
|
||||
width: StrictInt = 384
|
||||
height: Optional[StrictInt]
|
||||
favicons: StrictStr = "ui/images/favicons/"
|
||||
|
||||
@validator("favicons")
|
||||
def favicons_trailing_slash(cls, value):
|
||||
"""If the favicons path does not end in a '/', append it."""
|
||||
chars = list(value)
|
||||
if chars[len(chars) - 1] != "/":
|
||||
chars.append("/")
|
||||
return "".join(chars)
|
||||
|
||||
@root_validator(pre=True)
|
||||
def validate_logo_model(cls, values):
|
||||
"""Set default opengraph image location.
|
||||
|
||||
Arguments:
|
||||
values {dict} -- Unvalidated model
|
||||
|
||||
Returns:
|
||||
{dict} -- Modified model
|
||||
"""
|
||||
logo_light = values.get("light")
|
||||
logo_dark = values.get("dark")
|
||||
default_logo_light = (
|
||||
Path(__file__).parent.parent.parent
|
||||
/ "static/images/hyperglass-light.png"
|
||||
)
|
||||
default_logo_dark = (
|
||||
Path(__file__).parent.parent.parent
|
||||
/ "static/images/hyperglass-dark.png"
|
||||
)
|
||||
|
||||
# Use light logo as dark logo if dark logo is undefined.
|
||||
if logo_light is not None and logo_dark is None:
|
||||
values["dark"] = logo_light
|
||||
|
||||
# Use dark logo as light logo if light logo is undefined.
|
||||
if logo_dark is not None and logo_light is None:
|
||||
values["light"] = logo_dark
|
||||
|
||||
# Set default logo paths if logo is undefined.
|
||||
if logo_light is None and logo_dark is None:
|
||||
values["light"] = default_logo_light
|
||||
values["dark"] = default_logo_dark
|
||||
|
||||
return values
|
||||
|
||||
@validator("light", "dark")
|
||||
def validate_logos(cls, value):
|
||||
"""Convert file path to URL path.
|
||||
|
||||
Arguments:
|
||||
value {FilePath} -- Path to logo file.
|
||||
|
||||
Returns:
|
||||
{str} -- Formatted logo path
|
||||
"""
|
||||
return "".join(str(value).split("static")[1::])
|
||||
|
||||
class Config:
|
||||
"""Override pydantic config."""
|
||||
|
||||
fields = {"logo_path": "path"}
|
||||
|
||||
class ExternalLink(HyperglassModel):
|
||||
"""Validation model for params.branding.external_link."""
|
||||
|
||||
enable: StrictBool = True
|
||||
title: StrictStr = "PeeringDB"
|
||||
url: HttpUrl = "https://www.peeringdb.com/AS{primary_asn}"
|
||||
|
||||
class Terms(HyperglassModel):
|
||||
"""Validation model for params.branding.terms."""
|
||||
|
||||
enable: StrictBool = True
|
||||
file: Optional[FilePath]
|
||||
title: StrictStr = "Terms"
|
||||
|
||||
class Text(HyperglassModel):
|
||||
"""Validation model for params.branding.text."""
|
||||
|
||||
title_mode: constr(regex=("logo_only|text_only|logo_title|all")) = "logo_only"
|
||||
title: StrictStr = "hyperglass"
|
||||
subtitle: StrictStr = "AS{primary_asn}"
|
||||
query_location: StrictStr = "Location"
|
||||
query_type: StrictStr = "Query Type"
|
||||
query_target: StrictStr = "Target"
|
||||
query_vrf: StrictStr = "Routing Table"
|
||||
terms: StrictStr = "Terms"
|
||||
info: StrictStr = "Help"
|
||||
peeringdb = "PeeringDB"
|
||||
fqdn_tooltip: StrictStr = "Use {protocol}"
|
||||
cache: StrictStr = "Results will be cached for {timeout} {period}."
|
||||
|
||||
class Error404(HyperglassModel):
|
||||
"""Validation model for 404 Error Page."""
|
||||
|
||||
title: StrictStr = "Error"
|
||||
subtitle: StrictStr = "{uri} isn't a thing"
|
||||
button: StrictStr = "Home"
|
||||
|
||||
class Error500(HyperglassModel):
|
||||
"""Validation model for 500 Error Page."""
|
||||
|
||||
title: StrictStr = "Error"
|
||||
subtitle: StrictStr = "Something Went Wrong"
|
||||
button: StrictStr = "Home"
|
||||
|
||||
error404: Error404 = Error404()
|
||||
error500: Error500 = Error500()
|
||||
body: StrictStr = "Nunito"
|
||||
mono: StrictStr = "Fira Code"
|
||||
|
||||
colors: Colors = Colors()
|
||||
fonts: Fonts = Fonts()
|
||||
|
||||
|
||||
class Web(HyperglassModel):
|
||||
"""Validation model for all web/browser-related configuration."""
|
||||
|
||||
credit: Credit = Credit()
|
||||
external_link: ExternalLink = ExternalLink()
|
||||
font: Font = Font()
|
||||
@@ -221,3 +262,4 @@ class Web(HyperglassModel):
|
||||
opengraph: OpenGraph = OpenGraph()
|
||||
terms: Terms = Terms()
|
||||
text: Text = Text()
|
||||
theme: Theme = Theme()
|
||||
|
Reference in New Issue
Block a user