The Colors catalog manages available color options for furniture products. Each color represents a finish option that can be applied to manufactured items.
data = {"name": "Natural Oak"}color = ColorService.create(data)
Implementation from services.py:27:
@staticmethoddef create(data: dict) -> dict: name = data.get("name") if not name or not name.strip(): raise ValidationError("El nombre del color es requerido") name = name.strip() existing = Color.query.filter(func.lower(Color.name) == name.lower()).first() if existing: raise ConflictError(f"Ya existe un color con el nombre '{name}'") color = Color(name=name) db.session.add(color) db.session.commit() return color.to_dict()
The service performs case-insensitive duplicate checking using func.lower() to prevent entries like “Red” and “red”.
@staticmethoddef get_by_id(id_color: int) -> Color: color = Color.query.get(id_color) if not color: raise NotFoundError(f"No se encontró un color con ID {id_color}") return color
data = {"name": "Updated Color Name"}ColorService.update(id_color, data)
Implementation from services.py:84:
@staticmethoddef update(id_color: int, data: dict) -> dict: color = ColorService.get_by_id(id_color) name = data.get("name") if not name or not name.strip(): raise ValidationError("El nombre del color es requerido") name = name.strip() existing = ( db.session.query(Color.id_color) .filter(func.lower(Color.name) == name.lower(), Color.id_color != id_color) .first() is not None ) if existing: raise ConflictError(f"Ya existe otro color con el nombre '{name}'") color.name = name db.session.commit() return color.to_dict()
The ColorForm class in app/catalogs/colors/forms.py:10 defines the input form:
class ColorForm(FlaskForm): name = StringField( "Nombre", validators=[ DataRequired(message="El nombre del color es requerido"), Length(max=50, message="El nombre no puede exceder 50 caracteres"), ], )
from app.catalogs.colors.services import ColorService# Create a new colorcolor_data = {"name": "Walnut Brown"}try: result = ColorService.create(color_data) print(f"Created color: {result}")except ConflictError as e: print(f"Duplicate color: {e.message}")except ValidationError as e: print(f"Invalid data: {e.message}")