2020-01-16 02:52:00 -07:00
|
|
|
# Standard Library Imports
|
|
|
|
from pathlib import Path
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
# Third Party Imports
|
|
|
|
import PIL.Image as PilImage
|
|
|
|
from pydantic import FilePath
|
|
|
|
from pydantic import StrictInt
|
|
|
|
from pydantic import validator
|
|
|
|
|
|
|
|
# Project Imports
|
|
|
|
from hyperglass.configuration.models._utils import HyperglassModel
|
|
|
|
|
|
|
|
|
|
|
|
class OpenGraph(HyperglassModel):
|
2020-01-28 08:59:27 -07:00
|
|
|
"""Validation model for params.opengraph."""
|
2020-01-16 02:52:00 -07:00
|
|
|
|
|
|
|
width: Optional[StrictInt]
|
|
|
|
height: Optional[StrictInt]
|
|
|
|
image: Optional[FilePath]
|
|
|
|
|
|
|
|
@validator("image")
|
|
|
|
def validate_image(cls, value, values):
|
|
|
|
"""Set default opengraph image location.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
value {FilePath} -- Path to opengraph image file.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
{Path} -- Opengraph image file path object
|
|
|
|
"""
|
|
|
|
if value is None:
|
|
|
|
value = (
|
|
|
|
Path(__file__).parent.parent.parent
|
2020-01-17 02:50:57 -07:00
|
|
|
/ "static/images/hyperglass-opengraph.png"
|
2020-01-16 02:52:00 -07:00
|
|
|
)
|
|
|
|
with PilImage.open(value) as img:
|
|
|
|
width, height = img.size
|
|
|
|
values["width"] = width
|
|
|
|
values["height"] = height
|
|
|
|
|
|
|
|
return "".join(str(value).split("static")[1::])
|