Updated spool UIs (create/edit)

This commit is contained in:
Matt Gerega
2024-03-27 14:47:05 -04:00
parent 3aed735cea
commit 3ea4937f24
7 changed files with 349 additions and 80 deletions

View File

@@ -187,6 +187,18 @@ class Spool(BaseModel):
),
example=500.6,
)
initial_weight: Optional[float] = Field(
default=None,
ge=0,
description=("Initial weight of the filament and spool (gross weight)"),
example=1246,
)
empty_weight: Optional[float] = Field(
default=None,
ge=0,
description=("Weight of an empty spool (tare weight)."),
example=246,
)
used_weight: float = Field(ge=0, description="Consumed weight of filament from the spool in grams.", example=500.3)
remaining_length: Optional[float] = Field(
default=None,
@@ -228,7 +240,17 @@ class Spool(BaseModel):
remaining_weight: Optional[float] = None
remaining_length: Optional[float] = None
if filament.weight is not None:
if item.initial_weight is not None:
filament_spool_weight = item.filament.spool_weight if item.filament.spool_weight is not None else 0
spool_weight = item.empty_weight if item.empty_weight is not None else filament_spool_weight
remaining_weight = max(item.initial_weight - spool_weight - item.used_weight, 0)
remaining_length = length_from_weight(
weight=remaining_weight,
density=filament.density,
diameter=filament.diameter,
)
elif filament.weight is not None:
remaining_weight = max(filament.weight - item.used_weight, 0)
remaining_length = length_from_weight(
weight=remaining_weight,
@@ -249,6 +271,8 @@ class Spool(BaseModel):
last_used=item.last_used,
filament=filament,
price=item.price,
initial_weight=item.initial_weight,
empty_weight=item.empty_weight,
used_weight=item.used_weight,
used_length=used_length,
remaining_weight=remaining_weight,

View File

@@ -42,12 +42,12 @@ class SpoolParameters(BaseModel):
)
initial_weight: Optional[float] = Field(
ge=0,
description="The initial total weight of the spool.",
description="The initial total weight of the filament and spool. (gross weight)",
example=200,
)
empty_weight: Optional[float] = Field(
ge=0,
description="The weight of an empty spool.",
description="The weight of an empty spool. (tare weight)",
example=200,
)
remaining_weight: Optional[float] = Field(
@@ -85,6 +85,10 @@ class SpoolUseParameters(BaseModel):
use_weight: Optional[float] = Field(description="Filament weight to reduce by, in g.", example=5.3)
class SpoolMeasureParameters(BaseModel):
weight: float = Field(description="Current gross weight of the spool.", example=200)
@router.get(
"",
name="Find spool",
@@ -492,3 +496,23 @@ async def use( # noqa: ANN201
status_code=400,
content={"message": "Either use_weight or use_length must be specified."},
)
@router.put(
"/{spool_id}/measure",
name="Use spool filament based on the current weight measurement",
description=("Use some weight of filament from the spool. Specify the current gross weight of the spool."),
response_model_exclude_none=True,
response_model=Spool,
responses={
400: {"model": Message},
404: {"model": Message},
},
)
async def measure( # noqa: ANN201
db: Annotated[AsyncSession, Depends(get_db_session)],
spool_id: int,
body: SpoolMeasureParameters,
):
db_item = await spool.measure(db, spool_id, body.weight)
return Spool.from_db(db_item)

View File

@@ -323,6 +323,53 @@ async def use_length(db: AsyncSession, spool_id: int, length: float) -> models.S
return spool
async def measure(db: AsyncSession, spool_id: int, weight: float) -> models.Spool:
"""Record usage based on current gross weight of spool.
Increases the used_weight attribute of the spool.
Updates the first_used and last_used attributes where appropriate.
Args:
db (AsyncSession): Database session
spool_id (int): Spool ID
weight (float): Length of filament to consume, in mm
Returns:
models.Spool: Updated spool object
"""
spool_result = await db.execute(
sqlalchemy.select(models.Spool.initial_weight, models.Spool.used_weight).where(models.Spool.id == spool_id),
)
try:
spool_info = spool_result.one()
except NoResultFound as exc:
raise ItemNotFoundError("Spool not found.") from exc
initial_weight = spool_info[0]
if initial_weight is None:
# Get filament weight and spool_weight
result = await db.execute(
sqlalchemy.select(models.Filament.weight, models.Filament.spool_weight)
.join(models.Spool, models.Spool.filament_id == models.Filament.id)
.where(models.Spool.id == spool_id),
)
try:
filament_info = result.one()
except NoResultFound as exc:
raise ItemNotFoundError("Filament not found for spool.") from exc
initial_weight = filament_info[0] + filament_info[1]
# Calculate the current gross weight (initial_weight - used_weight)
current_use = initial_weight - spool_info[1]
# Calculate the weight used since last measure
weight_to_use = current_use - weight
return await use_weight(db, spool_id, weight_to_use)
async def find_locations(
*,
db: AsyncSession,