Updated based on PR requests

This commit is contained in:
Matt Gerega
2024-04-08 17:38:18 -04:00
parent 37804c14a8
commit 23cfb42ba8
4 changed files with 69 additions and 10 deletions

View File

@@ -42,12 +42,12 @@ class SpoolParameters(BaseModel):
)
initial_weight: Optional[float] = Field(
ge=0,
description="The initial total weight of the filament and spool. (gross weight)",
description="The initial total weight of the filament and spool, in g. (gross weight)",
example=200,
)
empty_weight: Optional[float] = Field(
ge=0,
description="The weight of an empty spool. (tare weight)",
description="The weight of an empty spool, in g. (tare weight)",
example=200,
)
remaining_weight: Optional[float] = Field(
@@ -86,7 +86,7 @@ class SpoolUseParameters(BaseModel):
class SpoolMeasureParameters(BaseModel):
weight: float = Field(description="Current gross weight of the spool.", example=200)
weight: float = Field(description="Current gross weight of the spool, in g.", example=200)
@router.get(

View File

@@ -35,7 +35,7 @@ class VendorParameters(BaseModel):
)
empty_spool_weight: Optional[float] = Field(
ge=0,
description="The weight of an empty spool.",
description="The weight of an empty spool, in g.",
example=200,
)
extra: Optional[dict[str, str]] = Field(

View File

@@ -56,12 +56,15 @@ async def create(
# Calculate initial_weight if not provided
if initial_weight is None and filament_item.weight is not None:
initial_weight = filament_item.weight - (empty_weight if empty_weight is not None else 0)
initial_weight = filament_item.weight + (empty_weight if empty_weight is not None else 0)
if used_weight is None:
if remaining_weight is not None:
if initial_weight is None or initial_weight == 0:
raise ItemCreateError("remaining_weight can only be used if the initial_weight is defined.")
raise ItemCreateError(
"remaining_weight can only be used if the initial_weight is "
"defined or the filament has a weight set.",
)
used_weight = max(initial_weight - empty_weight - remaining_weight, 0)
else:
used_weight = 0
@@ -352,7 +355,7 @@ async def measure(db: AsyncSession, spool_id: int, weight: float) -> models.Spoo
initial_weight = spool_info[0]
empty_weight = spool_info[2]
if initial_weight is None or empty_weight is None:
if initial_weight is None or initial_weight == 0 or empty_weight is None or empty_weight == 0:
# Get filament weight and spool_weight
result = await db.execute(
sqlalchemy.select(models.Filament.weight, models.Filament.spool_weight)
@@ -363,11 +366,15 @@ async def measure(db: AsyncSession, spool_id: int, weight: float) -> models.Spoo
filament_info = result.one()
except NoResultFound as exc:
raise ItemNotFoundError("Filament not found for spool.") from exc
if empty_weight is None:
if empty_weight is None or empty_weight == 0:
empty_weight = filament_info[1]
if initial_weight is None:
initial_weight = filament_info[0] + empty_weight
if initial_weight is None or initial_weight == 0:
initial_weight = (filament_info[0] if filament_info[0] is not None else 0) + empty_weight
if initial_weight is None or initial_weight == 0:
raise SpoolMeasureError("Initial weight is not set.")
# Calculate the current gross weight (initial_weight - used_weight)
current_use = initial_weight - spool_info[1]