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

Implement device description and avatar

This commit is contained in:
thatmattlove
2021-09-24 01:04:28 -07:00
parent 89568dc8e5
commit 22ae6a97e8
7 changed files with 55 additions and 12 deletions

View File

@@ -7,7 +7,7 @@ from pathlib import Path
from ipaddress import IPv4Address, IPv6Address
# Third Party
from pydantic import StrictInt, StrictStr, StrictBool, validator
from pydantic import FilePath, StrictInt, StrictStr, StrictBool, validator
# Project
from hyperglass.log import log
@@ -43,6 +43,8 @@ class Device(HyperglassModelWithId, extra="allow"):
id: StrictStr
name: StrictStr
description: Optional[StrictStr]
avatar: Optional[FilePath]
address: Union[IPv4Address, IPv6Address, StrictStr]
group: Optional[StrictStr]
credential: Credential
@@ -157,6 +159,28 @@ class Device(HyperglassModelWithId, extra="allow"):
)
return value
@validator("avatar")
def validate_avatar(
cls, value: Union[FilePath, None], values: Dict[str, Any]
) -> Union[FilePath, None]:
"""Migrate avatar to static directory."""
if value is not None:
# Standard Library
import shutil
# Third Party
from PIL import Image
target = Settings.static_path / "images" / value.name
copied = shutil.copy2(value, target)
log.debug("Copied {} avatar from {!r} to {!r}", values["name"], str(value), str(target))
with Image.open(copied) as src:
if src.width > 512:
src.thumbnail((512, 512 * src.height / src.width))
src.save(target)
return value
@validator("structured_output", pre=True, always=True)
def validate_structured_output(cls, value: bool, values: Dict) -> bool:
"""Validate structured output is supported on the device & set a default."""
@@ -300,9 +324,13 @@ class Devices(MultiModel, model=Device, unique_by="id"):
"group": group,
"locations": [
{
"group": group,
"id": device.id,
"name": device.name,
"group": group,
"avatar": f"/images/{device.avatar.name}"
if device.avatar is not None
else None,
"description": device.description,
"directives": [d.frontend(params) for d in device.directives],
}
for device in self