Changed initial_weight to be net weight, not gross weight

This commit is contained in:
Matt Gerega
2024-04-10 11:00:46 -04:00
parent dad446621f
commit c6f9abeced
14 changed files with 159 additions and 203 deletions

View File

@@ -190,10 +190,10 @@ class Spool(BaseModel):
initial_weight: Optional[float] = Field(
default=None,
ge=0,
description=("Initial weight of the filament and spool (gross weight)"),
description=("The initial weight, in grams, of the filament on the spool (net weight)."),
example=1246,
)
empty_weight: Optional[float] = Field(
spool_weight: Optional[float] = Field(
default=None,
ge=0,
description=("Weight of an empty spool (tare weight)."),
@@ -242,9 +242,7 @@ class Spool(BaseModel):
remaining_length: Optional[float] = 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_weight = max(item.initial_weight - item.used_weight, 0)
remaining_length = length_from_weight(
weight=remaining_weight,
density=filament.density,
@@ -272,7 +270,7 @@ class Spool(BaseModel):
filament=filament,
price=item.price,
initial_weight=item.initial_weight,
empty_weight=item.empty_weight,
spool_weight=item.spool_weight,
used_weight=item.used_weight,
used_length=used_length,
remaining_weight=remaining_weight,

View File

@@ -42,10 +42,10 @@ class SpoolParameters(BaseModel):
)
initial_weight: Optional[float] = Field(
ge=0,
description="The initial total weight of the filament and spool, in grams. (gross weight)",
description="The initial weight of the filament on the spool, in grams. (net weight)",
example=200,
)
empty_weight: Optional[float] = Field(
spool_weight: Optional[float] = Field(
ge=0,
description="The weight of an empty spool, in grams. (tare weight)",
example=200,
@@ -369,7 +369,7 @@ async def create( # noqa: ANN201
filament_id=body.filament_id,
price=body.price,
initial_weight=body.initial_weight,
empty_weight=body.empty_weight,
spool_weight=body.spool_weight,
remaining_weight=body.remaining_weight,
used_weight=body.used_weight,
first_used=body.first_used,

View File

@@ -66,7 +66,7 @@ class Spool(Base):
filament_id: Mapped[int] = mapped_column(ForeignKey("filament.id"))
filament: Mapped["Filament"] = relationship(back_populates="spools")
initial_weight: Mapped[Optional[float]] = mapped_column()
empty_weight: Mapped[Optional[float]] = mapped_column()
spool_weight: Mapped[Optional[float]] = mapped_column()
used_weight: Mapped[float] = mapped_column()
location: Mapped[Optional[str]] = mapped_column(String(64))
lot_nr: Mapped[Optional[str]] = mapped_column(String(64))

View File

@@ -36,7 +36,7 @@ async def create(
filament_id: int,
remaining_weight: Optional[float] = None,
initial_weight: Optional[float] = None,
empty_weight: Optional[float] = None,
spool_weight: Optional[float] = None,
used_weight: Optional[float] = None,
first_used: Optional[datetime] = None,
last_used: Optional[datetime] = None,
@@ -50,13 +50,13 @@ async def create(
"""Add a new spool to the database. Leave weight empty to assume full spool."""
filament_item = await filament.get_by_id(db, filament_id)
# Set empty_weight to spool_weight if spool_weight is not null and empty_weight not provided
if empty_weight is None and filament_item.spool_weight is not None:
empty_weight = filament_item.spool_weight
# Set spool_weight to spool_weight if spool_weight is not null and spool_weight not provided
if spool_weight is None and filament_item.spool_weight is not None:
spool_weight = filament_item.spool_weight
# 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
if used_weight is None:
if remaining_weight is not None:
@@ -65,7 +65,7 @@ async def create(
"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)
used_weight = max(initial_weight - remaining_weight, 0)
else:
used_weight = 0
@@ -79,7 +79,7 @@ async def create(
filament=filament_item,
registered=datetime.utcnow().replace(microsecond=0),
initial_weight=initial_weight,
empty_weight=empty_weight,
spool_weight=spool_weight,
used_weight=used_weight,
price=price,
first_used=first_used,
@@ -166,7 +166,7 @@ async def find(
for fieldstr, order in sort_by.items():
sorts = []
if fieldstr in {"remaining_weight", "remaining_length"}:
sorts.append(models.Spool.initial_weight - models.Spool.empty_weight - models.Spool.used_weight)
sorts.append(models.Spool.initial_weight - models.Spool.spool_weight - models.Spool.used_weight)
elif fieldstr == "filament.combined_name":
sorts.append(models.Vendor.name)
sorts.append(models.Filament.name)
@@ -198,15 +198,13 @@ async def update(
if k == "filament_id":
spool.filament = await filament.get_by_id(db, v)
# If there is no initial_weight, calculate it from the filament weight
if spool.initial_weight is None and spool.empty_weight is None and spool.filament.weight is not None:
spool_weight = spool.empty_weight if spool.empty_weight is not None else 0
spool.initial_weight = spool.filament.weight + spool_weight
spool.empty_weight = spool_weight
if spool.initial_weight is None and spool.filament.weight is not None:
spool.initial_weight = spool.filament.weight
elif k == "remaining_weight":
if spool.initial_weight is None:
raise ItemCreateError("remaining_weight can only be used if initial_weight is set.")
spool.used_weight = max((spool.initial_weight - spool.empty_weight) - v, 0)
spool.used_weight = max(spool.initial_weight - v, 0)
elif isinstance(v, datetime):
setattr(spool, k, utc_timezone_naive(v))
elif k == "extra":
@@ -343,7 +341,7 @@ async def measure(db: AsyncSession, spool_id: int, weight: float) -> models.Spoo
"""
spool_result = await db.execute(
sqlalchemy.select(models.Spool.initial_weight, models.Spool.used_weight, models.Spool.empty_weight).where(
sqlalchemy.select(models.Spool.initial_weight, models.Spool.used_weight, models.Spool.spool_weight).where(
models.Spool.id == spool_id,
),
)
@@ -354,8 +352,8 @@ async def measure(db: AsyncSession, spool_id: int, weight: float) -> models.Spoo
raise SpoolMeasureError("Spool not found.") from exc
initial_weight = spool_info[0]
empty_weight = spool_info[2]
if initial_weight is None or initial_weight == 0 or empty_weight is None or empty_weight == 0:
spool_weight = spool_info[2]
if initial_weight is None or initial_weight == 0 or spool_weight is None or spool_weight == 0:
# Get filament weight and spool_weight
result = await db.execute(
sqlalchemy.select(models.Filament.weight, models.Filament.spool_weight)
@@ -367,17 +365,19 @@ async def measure(db: AsyncSession, spool_id: int, weight: float) -> models.Spoo
except NoResultFound as exc:
raise ItemNotFoundError("Filament not found for spool.") from exc
if empty_weight is None or empty_weight == 0:
empty_weight = filament_info[1]
if spool_weight is None or spool_weight == 0:
spool_weight = filament_info[1]
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
initial_weight = filament_info[0] if filament_info[0] is not None else 0
if initial_weight is None or initial_weight == 0:
raise SpoolMeasureError("Initial weight is not set.")
initial_gross_weight = initial_weight + spool_weight
# Calculate the current gross weight (initial_weight - used_weight)
current_use = initial_weight - spool_info[1]
current_use = initial_gross_weight - spool_info[1]
# if the measurement is greater than the initial weight, raise an error
if weight > current_use:
@@ -387,8 +387,8 @@ async def measure(db: AsyncSession, spool_id: int, weight: float) -> models.Spoo
weight_to_use = current_use - weight
# If the measured weight is less than the empty weight, use the rest of the spool
if (initial_weight - weight_to_use) < empty_weight:
weight_to_use = current_use - empty_weight
if (initial_gross_weight - weight_to_use) < spool_weight:
weight_to_use = current_use - spool_weight
return await use_weight(db, spool_id, weight_to_use)