Added filament color code field

This commit is contained in:
Donkie
2023-06-01 19:56:24 +02:00
parent 27731974fd
commit 4e409f6bb6
8 changed files with 57 additions and 1 deletions

View File

@@ -1 +0,0 @@
Generic single-database configuration with an async dbapi.

6
migrations/README.md Normal file
View File

@@ -0,0 +1,6 @@
# Migrations
Creating a new version:
```bash
alembic revision -m "some title" --autogenerate
```

View File

@@ -0,0 +1,24 @@
"""add filament color code.
Revision ID: db385b808a20
Revises: b47376d60c6d
Create Date: 2023-06-01 19:53:44.440616
"""
import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic.
revision = "db385b808a20"
down_revision = "b47376d60c6d"
branch_labels = None
depends_on = None
def upgrade() -> None:
"""Perform the upgrade."""
op.add_column("filament", sa.Column("color_hex", sa.String(length=6), nullable=True))
def downgrade() -> None:
"""Perform the downgrade."""
op.drop_column("filament", "color_hex")

View File

@@ -73,6 +73,12 @@ class FilamentParameters(BaseModel):
description="Overridden bed temperature, in °C.",
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 #)",
example="FF0000",
)
class FilamentUpdateParameters(FilamentParameters):
@@ -164,6 +170,7 @@ async def create(
comment=body.comment,
settings_extruder_temp=body.settings_extruder_temp,
settings_bed_temp=body.settings_bed_temp,
color_hex=body.color_hex,
)
return Filament.from_db(db_item)

View File

@@ -79,6 +79,12 @@ class Filament(BaseModel):
description="Overridden bed temperature, in °C.",
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 #)",
example="FF0000",
)
@staticmethod
def from_db(item: models.Filament) -> "Filament":
@@ -98,6 +104,7 @@ class Filament(BaseModel):
comment=item.comment,
settings_extruder_temp=item.settings_extruder_temp,
settings_bed_temp=item.settings_bed_temp,
color_hex=item.color_hex,
)

View File

@@ -26,6 +26,7 @@ async def create(
comment: Optional[str] = None,
settings_extruder_temp: Optional[int] = None,
settings_bed_temp: Optional[int] = None,
color_hex: Optional[str] = None,
) -> models.Filament:
"""Add a new filament to the database."""
vendor_item: models.Vendor | None = None
@@ -45,6 +46,7 @@ async def create(
comment=comment,
settings_extruder_temp=settings_extruder_temp,
settings_bed_temp=settings_bed_temp,
color_hex=color_hex,
)
db.add(db_item)
await db.flush()

View File

@@ -41,6 +41,7 @@ class Filament(Base):
comment: Mapped[Optional[str]] = mapped_column(String(1024))
settings_extruder_temp: Mapped[Optional[int]] = mapped_column(comment="Overridden extruder temperature.")
settings_bed_temp: Mapped[Optional[int]] = mapped_column(comment="Overridden bed temperature.")
color_hex: Mapped[Optional[str]] = mapped_column(String(6))
class Spool(Base):

View File

@@ -21,6 +21,7 @@ def test_add_filament(random_vendor: dict[str, Any]):
comment = "abcdefghåäö"
settings_extruder_temp = 200
settings_bed_temp = 60
color_hex = "FF0000"
result = httpx.post(
f"{URL}/api/v1/filament",
json={
@@ -36,6 +37,7 @@ def test_add_filament(random_vendor: dict[str, Any]):
"comment": comment,
"settings_extruder_temp": settings_extruder_temp,
"settings_bed_temp": settings_bed_temp,
"color_hex": color_hex,
},
)
result.raise_for_status()
@@ -57,6 +59,7 @@ def test_add_filament(random_vendor: dict[str, Any]):
"comment": comment,
"settings_extruder_temp": settings_extruder_temp,
"settings_bed_temp": settings_bed_temp,
"color_hex": color_hex,
}
# Clean up
@@ -104,6 +107,7 @@ def test_get_filament(random_vendor: dict[str, Any]):
comment = "abcdefghåäö"
settings_extruder_temp = 200
settings_bed_temp = 60
color_hex = "FF0000"
result = httpx.post(
f"{URL}/api/v1/filament",
json={
@@ -119,6 +123,7 @@ def test_get_filament(random_vendor: dict[str, Any]):
"comment": comment,
"settings_extruder_temp": settings_extruder_temp,
"settings_bed_temp": settings_bed_temp,
"color_hex": color_hex,
},
)
result.raise_for_status()
@@ -147,6 +152,7 @@ def test_get_filament(random_vendor: dict[str, Any]):
"comment": comment,
"settings_extruder_temp": settings_extruder_temp,
"settings_bed_temp": settings_bed_temp,
"color_hex": color_hex,
}
# Clean up
@@ -347,6 +353,7 @@ def test_update_filament(random_vendor: dict[str, Any]):
"comment": "abcdefghåäö",
"settings_extruder_temp": 200,
"settings_bed_temp": 60,
"color_hex": "FF0000",
},
)
result.raise_for_status()
@@ -364,6 +371,7 @@ def test_update_filament(random_vendor: dict[str, Any]):
new_comment = "test"
new_settings_extruder_temp = 210
new_settings_bed_temp = 70
new_color_hex = "00FF00"
result = httpx.patch(
f"{URL}/api/v1/filament/{added_filament['id']}",
json={
@@ -379,6 +387,7 @@ def test_update_filament(random_vendor: dict[str, Any]):
"comment": new_comment,
"settings_extruder_temp": new_settings_extruder_temp,
"settings_bed_temp": new_settings_bed_temp,
"color_hex": new_color_hex,
},
)
result.raise_for_status()
@@ -400,6 +409,7 @@ def test_update_filament(random_vendor: dict[str, Any]):
"comment": new_comment,
"settings_extruder_temp": new_settings_extruder_temp,
"settings_bed_temp": new_settings_bed_temp,
"color_hex": new_color_hex,
}
# Clean up