52 lines
2.1 KiB
Python
52 lines
2.1 KiB
Python
"""
|
|
Validadores comunes para los endpoints de imagen.
|
|
"""
|
|
import base64
|
|
|
|
from app.schemas.image_standard import ImageEvaluationRubric, ImageEvaluationCriteria
|
|
|
|
def json_to_rubric(json_data: dict) -> ImageEvaluationRubric:
|
|
"""Convierte un diccionario JSON en un objeto ImageEvaluationRubric mapeando los campos correctos."""
|
|
|
|
def parse_criteria(criteria_list: list) -> list:
|
|
parsed_criteria = []
|
|
for item in criteria_list:
|
|
# Convertir el valor a float si existe
|
|
score_value = float(item["value"]) if item.get("value") is not None else None
|
|
description = str(item["description"]) if item.get("description") is not None else None
|
|
|
|
# Procesar subcriterios recursivamente si existen
|
|
sub_raw = item.get("sub_criteria")
|
|
sub_parsed = parse_criteria(sub_raw) if sub_raw else None
|
|
|
|
parsed_criteria.append(ImageEvaluationCriteria(
|
|
name=item["name"],
|
|
description=description,
|
|
score=score_value,
|
|
subcriteria=sub_parsed
|
|
))
|
|
return parsed_criteria
|
|
|
|
# Jerarquía de la categoría
|
|
category_info = json_data.get("category", {})
|
|
hierarchy_list = category_info.get("hierarchy", [])
|
|
|
|
# Ordenamos por nivel para asegurar la lógica: Nivel 0 -> Nivel 1 -> Nivel 2
|
|
sorted_hierarchy = sorted(hierarchy_list, key=lambda x: x.get("level", 0))
|
|
|
|
# Unimos los nombres con ' > '
|
|
specialization_path = " > ".join([item["name"] for item in sorted_hierarchy]) if sorted_hierarchy else None
|
|
|
|
# Extraer la información principal de la rúbrica
|
|
rubric_info = json_data.get("rubric", {})
|
|
|
|
return ImageEvaluationRubric(
|
|
name=rubric_info.get("name", "Sin nombre"),
|
|
description=str(rubric_info["description"]) if rubric_info.get("description") is not None else None,
|
|
category=specialization_path,
|
|
criteria=parse_criteria(json_data.get("criteria", []))
|
|
)
|
|
|
|
def encode_image_from_bytes(image_bytes: bytes) -> str:
|
|
"""Codifica imagen desde bytes a base64"""
|
|
return base64.b64encode(image_bytes).decode('utf-8') |