Added support for color_hex alpha channel

This commit is contained in:
Donkie
2023-08-12 21:32:20 +02:00
parent 6934748b14
commit 4cb2542a41
6 changed files with 69 additions and 8 deletions

View File

@@ -6,7 +6,7 @@ from typing import Annotated, Optional
from fastapi import APIRouter, Depends, Query
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field
from pydantic import BaseModel, Field, validator
from pydantic.error_wrappers import ErrorWrapper
from sqlalchemy.ext.asyncio import AsyncSession
@@ -74,12 +74,29 @@ class FilamentParameters(BaseModel):
example=60,
)
color_hex: Optional[str] = Field(
min_length=6,
max_length=6,
description="Hexadecimal color code of the filament, e.g. FF0000 for red. (no leading #)",
description="Hexadecimal color code of the filament, e.g. FF0000 for red. Supports alpha channel at the end.",
example="FF0000",
)
@validator("color_hex")
@classmethod
def color_hex_validator(cls, v: Optional[str]) -> Optional[str]: # noqa: ANN102
"""Validate the color_hex field."""
if not v:
return None
if v.startswith("#"):
v = v[1:]
v = v.upper()
for c in v:
if c not in "0123456789ABCDEF":
raise ValueError("Invalid character in color code.")
if len(v) not in (6, 8):
raise ValueError("Color code must be 6 or 8 characters long.")
return v
class FilamentUpdateParameters(FilamentParameters):
density: Optional[float] = Field(gt=0, description="The density of this filament in g/cm3.", example=1.24)

View File

@@ -82,8 +82,8 @@ class Filament(BaseModel):
)
color_hex: Optional[str] = Field(
min_length=6,
max_length=6,
description="Hexadecimal color code of the filament, e.g. FF0000 for red. (no leading #)",
max_length=8,
description="Hexadecimal color code of the filament, e.g. FF0000 for red. Supports alpha channel at the end.",
example="FF0000",
)