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

241 lines
6.3 KiB
Python
Raw Normal View History

"""Validate branding configuration variables."""
2020-02-03 02:35:11 -07:00
# Standard Library
2020-01-03 01:25:41 -07:00
from typing import Optional
2020-02-03 02:35:11 -07:00
# Third Party
from pydantic import (
HttpUrl,
FilePath,
StrictInt,
StrictStr,
StrictBool,
constr,
validator,
root_validator,
)
from pydantic.color import Color
2020-02-03 02:35:11 -07:00
# Project
from hyperglass.constants import DNS_OVER_HTTPS, FUNC_COLOR_MAP
from hyperglass.configuration.models._utils import HyperglassModel, validate_image
from hyperglass.configuration.models.opengraph import OpenGraph
2019-10-04 16:54:32 -07:00
2020-02-08 00:58:32 -07:00
class HyperglassLevel3(HyperglassModel):
"""Automatic docs sorting subclass."""
class Config:
"""Pydantic model configuration."""
schema_extra = {"level": 3}
class HyperglassLevel4(HyperglassModel):
"""Automatic docs sorting subclass."""
class Config:
"""Pydantic model configuration."""
schema_extra = {"level": 4}
class Analytics(HyperglassLevel3):
2020-01-28 12:03:47 -07:00
"""Validation model for Google Analytics."""
2020-01-28 12:03:47 -07:00
enable: StrictBool = False
id: Optional[StrictStr]
2020-01-28 10:03:51 -07:00
2020-01-28 12:03:47 -07:00
@validator("id")
def validate_id(cls, value, values):
"""Ensure ID is set if analytics is enabled.
2020-01-28 10:03:51 -07:00
2020-01-28 12:03:47 -07:00
Arguments:
value {str|None} -- Google Analytics ID
values {[type]} -- Already-validated model parameters
2020-01-28 10:03:51 -07:00
2020-01-28 12:03:47 -07:00
Raises:
ValueError: Raised if analytics is enabled but no ID is set.
2020-01-28 10:03:51 -07:00
2020-01-28 12:03:47 -07:00
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
2020-01-28 10:03:51 -07:00
2020-02-08 00:58:32 -07:00
class Credit(HyperglassLevel3):
2020-01-28 12:03:47 -07:00
"""Validation model for developer credit."""
2020-01-28 12:03:47 -07:00
enable: StrictBool = True
2019-10-04 16:54:32 -07:00
2020-02-08 00:58:32 -07:00
class ExternalLink(HyperglassLevel3):
2020-01-28 12:03:47 -07:00
"""Validation model for external link."""
2020-01-28 12:03:47 -07:00
enable: StrictBool = True
title: StrictStr = "PeeringDB"
2020-03-19 17:33:52 -07:00
url: HttpUrl = "https://www.peeringdb.com/asn/{primary_asn}"
2020-02-08 00:58:32 -07:00
class HelpMenu(HyperglassLevel3):
2020-01-28 12:03:47 -07:00
"""Validation model for generic help menu."""
2020-01-28 12:03:47 -07:00
enable: StrictBool = True
file: Optional[FilePath]
title: StrictStr = "Help"
2020-02-08 00:58:32 -07:00
class Logo(HyperglassLevel3):
2020-01-28 12:03:47 -07:00
"""Validation model for logo configuration."""
2020-01-16 02:52:00 -07:00
2020-02-14 16:29:11 -07:00
light: StrictStr = "images/hyperglass-light.png"
dark: StrictStr = "images/hyperglass-dark.png"
2020-01-28 12:03:47 -07:00
width: StrictInt = 384
height: Optional[StrictInt]
favicons: StrictStr = "ui/images/favicons/"
2020-01-16 02:52:00 -07:00
2020-01-28 12:03:47 -07:00
@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)
2020-01-16 02:52:00 -07:00
2020-01-28 12:03:47 -07:00
@validator("light", "dark")
def validate_logos(cls, value):
"""Convert file path to URL path.
Arguments:
value {FilePath} -- Path to logo file.
2020-01-16 02:52:00 -07:00
2020-01-28 12:03:47 -07:00
Returns:
{str} -- Formatted logo path
"""
return validate_image(value)
2019-12-31 12:10:23 -07:00
2020-01-28 12:03:47 -07:00
class Config:
"""Override pydantic config."""
2020-01-28 12:03:47 -07:00
fields = {"logo_path": "path"}
2020-02-08 00:58:32 -07:00
class Terms(HyperglassLevel3):
2020-01-28 12:03:47 -07:00
"""Validation model for terms & conditions."""
2020-01-28 12:03:47 -07:00
enable: StrictBool = True
file: Optional[FilePath]
title: StrictStr = "Terms"
2020-02-08 00:58:32 -07:00
class Text(HyperglassLevel3):
2020-01-28 12:03:47 -07:00
"""Validation model for params.branding.text."""
2020-01-28 12:03:47 -07:00
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}."
2020-02-08 00:58:32 -07:00
class ThemeColors(HyperglassLevel4):
"""Validation model for theme colors."""
black: Color = "#262626"
white: Color = "#f7f7f7"
gray: Color = "#c1c7cc"
red: Color = "#d84b4b"
2020-02-10 09:06:20 -07:00
orange: Color = "#ff6b35"
2020-02-08 00:58:32 -07:00
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.
2020-02-10 09:06:20 -07:00
2020-02-08 00:58:32 -07:00
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
2020-02-08 00:58:32 -07:00
def dict(self, *args, **kwargs):
"""Return dict for colors only."""
return {k: v.as_hex() for k, v in self.__dict__.items()}
2020-01-28 12:03:47 -07:00
2020-02-08 00:58:32 -07:00
class ThemeFonts(HyperglassLevel4):
"""Validation model for theme fonts."""
2020-01-28 12:03:47 -07:00
2020-02-08 00:58:32 -07:00
body: StrictStr = "Nunito"
mono: StrictStr = "Fira Code"
2020-01-28 12:03:47 -07:00
2020-02-08 00:58:32 -07:00
class Theme(HyperglassLevel3):
2020-01-28 12:03:47 -07:00
"""Validation model for theme variables."""
2020-02-08 00:58:32 -07:00
colors: ThemeColors = ThemeColors()
fonts: ThemeFonts = ThemeFonts()
class DnsOverHttps(HyperglassLevel3):
"""Validation model for DNS over HTTPS resolution."""
name: constr(regex="|".join(DNS_OVER_HTTPS.keys())) = "cloudflare"
2020-03-21 15:00:37 -07:00
url: StrictStr = ""
@root_validator
def validate_dns(cls, values):
"""Assign url field to model based on selected provider.
Arguments:
values {dict} -- Dict of selected provider
Returns:
{dict} -- Dict with url attribute
"""
provider = values["name"]
values["url"] = DNS_OVER_HTTPS[provider]
return values
2020-01-28 12:03:47 -07:00
class Web(HyperglassModel):
"""Validation model for all web/browser-related configuration."""
credit: Credit = Credit()
dns_provider: DnsOverHttps = DnsOverHttps()
external_link: ExternalLink = ExternalLink()
help_menu: HelpMenu = HelpMenu()
logo: Logo = Logo()
opengraph: OpenGraph = OpenGraph()
terms: Terms = Terms()
text: Text = Text()
2020-01-28 12:03:47 -07:00
theme: Theme = Theme()
2020-02-08 00:58:32 -07:00
class Config:
"""Pydantic model configuration."""
schema_extra = {"level": 2}