1
0
mirror of https://github.com/checktheroads/hyperglass synced 2024-05-11 05:55:08 +00:00

197 lines
6.0 KiB
Python
Raw Normal View History

"""Validate branding configuration variables."""
2020-01-03 03:04:50 -07:00
# Standard Library Imports
2020-01-16 02:52:00 -07:00
from pathlib import Path
2020-01-03 01:25:41 -07:00
from typing import Optional
# Third Party Imports
2020-01-16 02:52:00 -07:00
from pydantic import FilePath
from pydantic import HttpUrl
2020-01-03 03:04:50 -07:00
from pydantic import StrictBool
from pydantic import StrictInt
from pydantic import StrictStr
2019-10-04 16:54:32 -07:00
from pydantic import constr
2020-01-16 02:52:00 -07:00
from pydantic import root_validator
from pydantic import validator
from pydantic.color import Color
2019-10-04 16:54:32 -07:00
# Project Imports
from hyperglass.configuration.models._utils import HyperglassModel
2019-10-04 16:54:32 -07:00
class Branding(HyperglassModel):
2019-12-31 12:10:23 -07:00
"""Validation model for params.branding."""
2019-10-04 16:54:32 -07:00
class Colors(HyperglassModel):
2019-12-31 12:10:23 -07:00
"""Validation model for params.colors."""
primary: Color = "#40798c"
secondary: Color = "#330036"
danger: Color = "#a21024"
warning: Color = "#eec643"
light: Color = "#fbfffe"
dark: Color = "#383541"
background: Color = "#fbfffe"
2019-10-04 16:54:32 -07:00
def dict(self, *args, **kwargs):
2019-12-31 12:10:23 -07:00
"""Return dict for colors only."""
2019-10-04 16:54:32 -07:00
_dict = {}
for k, v in self.__dict__.items():
_dict.update({k: v.as_hex()})
return _dict
class Credit(HyperglassModel):
2019-12-31 12:10:23 -07:00
"""Validation model for params.branding.credit."""
2020-01-03 01:25:41 -07:00
enable: StrictBool = True
2019-10-04 16:54:32 -07:00
class Font(HyperglassModel):
2019-12-31 12:10:23 -07:00
"""Validation model for params.branding.font."""
2019-12-31 12:37:11 -07:00
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()
2019-10-04 16:54:32 -07:00
class HelpMenu(HyperglassModel):
2019-12-31 12:10:23 -07:00
"""Validation model for params.branding.help_menu."""
2020-01-03 01:25:41 -07:00
enable: StrictBool = True
2020-01-16 02:52:00 -07:00
file: Optional[FilePath]
title: StrictStr = "Help"
2019-10-04 16:54:32 -07:00
class Logo(HyperglassModel):
2019-12-31 12:10:23 -07:00
"""Validation model for params.branding.logo."""
2020-01-16 02:52:00 -07:00
light: Optional[FilePath]
dark: Optional[FilePath]
2020-01-03 01:25:41 -07:00
width: StrictInt = 384
height: Optional[StrictInt]
favicons: StrictStr = "ui/images/favicons/"
@validator("favicons")
def favicons_trailing_slash(cls, value):
2019-12-31 12:10:23 -07:00
"""If the favicons path does not end in a '/', append it."""
chars = list(value)
if chars[len(chars) - 1] != "/":
chars.append("/")
return "".join(chars)
2020-01-16 02:52:00 -07:00
@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
2020-01-17 02:50:57 -07:00
/ "static/images/hyperglass-light.png"
2020-01-16 02:52:00 -07:00
)
default_logo_dark = (
Path(__file__).parent.parent.parent
2020-01-17 02:50:57 -07:00
/ "static/images/hyperglass-dark.png"
2020-01-16 02:52:00 -07:00
)
# 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:
2019-12-31 12:10:23 -07:00
"""Override pydantic config."""
fields = {"logo_path": "path"}
2020-01-16 02:52:00 -07:00
class ExternalLink(HyperglassModel):
"""Validation model for params.branding.external_link."""
2020-01-03 01:25:41 -07:00
enable: StrictBool = True
2020-01-16 02:52:00 -07:00
title: StrictStr = "PeeringDB"
url: HttpUrl = "https://www.peeringdb.com/AS{primary_asn}"
2019-10-04 16:54:32 -07:00
class Terms(HyperglassModel):
2019-12-31 12:10:23 -07:00
"""Validation model for params.branding.terms."""
2020-01-03 01:25:41 -07:00
enable: StrictBool = True
2020-01-16 02:52:00 -07:00
file: Optional[FilePath]
title: StrictStr = "Terms"
2019-10-04 16:54:32 -07:00
class Text(HyperglassModel):
2019-12-31 12:10:23 -07:00
"""Validation model for params.branding.text."""
2019-10-04 16:54:32 -07:00
title_mode: constr(regex=("logo_only|text_only|logo_title|all")) = "logo_only"
2020-01-03 01:25:41 -07:00
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"
2020-01-27 21:28:46 -07:00
fqdn_tooltip: StrictStr = "Use {protocol}"
2019-10-04 16:54:32 -07:00
class Error404(HyperglassModel):
2019-12-31 12:10:23 -07:00
"""Validation model for 404 Error Page."""
2020-01-03 01:25:41 -07:00
title: StrictStr = "Error"
subtitle: StrictStr = "{uri} isn't a thing"
button: StrictStr = "Home"
2019-10-04 16:54:32 -07:00
class Error500(HyperglassModel):
2019-12-31 12:10:23 -07:00
"""Validation model for 500 Error Page."""
2020-01-03 01:25:41 -07:00
title: StrictStr = "Error"
subtitle: StrictStr = "Something Went Wrong"
button: StrictStr = "Home"
error404: Error404 = Error404()
error500: Error500 = Error500()
colors: Colors = Colors()
credit: Credit = Credit()
font: Font = Font()
help_menu: HelpMenu = HelpMenu()
logo: Logo = Logo()
2020-01-16 02:52:00 -07:00
external_link: ExternalLink = ExternalLink()
terms: Terms = Terms()
text: Text = Text()