Added initial_weight and empty_weight to spool

This commit is contained in:
Matt Gerega
2024-03-26 13:55:44 -04:00
parent a68e0d59bd
commit 1d6830d769
7 changed files with 207 additions and 7 deletions

View File

@@ -107,6 +107,9 @@ export const SpoolCreate: React.FC<IResourceComponentsProps & CreateOrCloneProps
});
filamentOptions?.sort((a, b) => a.label.localeCompare(b.label, undefined, { sensitivity: "base" }));
const [defaultEmptySpoolWeight, setDefaultEmptySpoolWeight] = useState(0);
const [defaultInitialTotalWeight, setDefaultInitialTotalWeight] = useState(0);
const [weightToEnter, setWeightToEnter] = useState(1);
const [usedWeight, setUsedWeight] = useState(0);
@@ -124,6 +127,9 @@ export const SpoolCreate: React.FC<IResourceComponentsProps & CreateOrCloneProps
const newFilamentWeight = newSelectedFilament?.weight || 0;
const newSpoolWeight = newSelectedFilament?.spool_weight || 0;
setDefaultEmptySpoolWeight(newSpoolWeight);
setDefaultInitialTotalWeight(newFilamentWeight + newSpoolWeight);
if (weightToEnter >= 3) {
if (!(newFilamentWeight && newSpoolWeight)) {
setWeightToEnter(2);
@@ -254,6 +260,42 @@ export const SpoolCreate: React.FC<IResourceComponentsProps & CreateOrCloneProps
parser={numberParser}
/>
</Form.Item>
<Form.Item
label={t("spool.fields.initial_weight")}
help={t("spool.fields_help.initial_weight")}
name={["initial_weight"]}
rules={[
{
required: false,
type: "number",
min: 0,
},
]}
>
<InputNumber
addonAfter="g"
precision={1}
defaultValue={defaultInitialTotalWeight} />
</Form.Item>
<Form.Item
label={t("spool.fields.empty_weight")}
help={t("spool.fields_help.empty_weight")}
name={["empty_weight"]}
rules={[
{
required: false,
type: "number",
min: 0,
},
]}
>
<InputNumber
addonAfter="g"
precision={1}
defaultValue={defaultEmptySpoolWeight} />
</Form.Item>
<Form.Item hidden={true} name={["used_weight"]} initialValue={0}>
<InputNumber value={usedWeight} />
</Form.Item>

View File

@@ -56,6 +56,9 @@ export const SpoolEdit: React.FC<IResourceComponentsProps> = () => {
formProps.initialValues = ParsedExtras(formProps.initialValues);
}
const [defaultEmptySpoolWeight, setDefaultEmptySpoolWeight] = useState(0);
const [defaultInitialTotalWeight, setDefaultInitialTotalWeight] = useState(0);
// Override the form's onFinish method to stringify the extra fields
const originalOnFinish = formProps.onFinish;
formProps.onFinish = (allValues: ISpoolParsedExtras) => {
@@ -107,6 +110,13 @@ export const SpoolEdit: React.FC<IResourceComponentsProps> = () => {
const newSelectedFilament = filamentOptions?.find((obj) => {
return obj.value === newID;
});
const newFilamentWeight = newSelectedFilament?.weight || 0;
const newSpoolWeight = newSelectedFilament?.spool_weight || 0;
setDefaultEmptySpoolWeight(newSpoolWeight);
setDefaultInitialTotalWeight(newFilamentWeight + newSpoolWeight);
const filamentHasWeight = newSelectedFilament?.weight || 0;
const filamentHasSpoolWeight = newSelectedFilament?.spool_weight || 0;
@@ -238,6 +248,42 @@ export const SpoolEdit: React.FC<IResourceComponentsProps> = () => {
parser={numberParser}
/>
</Form.Item>
<Form.Item
label={t("spool.fields.initial_weight")}
help={t("spool.fields_help.initial_weight")}
name={["initial_weight"]}
rules={[
{
required: false,
type: "number",
min: 0,
},
]}
>
<InputNumber
addonAfter="g"
precision={1}
defaultValue={defaultInitialTotalWeight} />
</Form.Item>
<Form.Item
label={t("spool.fields.empty_weight")}
help={t("spool.fields_help.empty_weight")}
name={["empty_weight"]}
rules={[
{
required: false,
type: "number",
min: 0,
},
]}
>
<InputNumber
addonAfter="g"
precision={1}
defaultValue={defaultEmptySpoolWeight} />
</Form.Item>
<Form.Item hidden={true} name={["used_weight"]} initialValue={0}>
<InputNumber value={usedWeight} />
</Form.Item>

View File

@@ -0,0 +1,30 @@
"""spool weights.
Revision ID: aafcd7fb0e84
Revises: b8881bdb716c
Create Date: 2024-03-26 09:48:09.930022
"""
import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic.
revision = 'aafcd7fb0e84'
down_revision = 'b8881bdb716c'
branch_labels = None
depends_on = None
def upgrade() -> None:
"""Perform the upgrade."""
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('spool', sa.Column('initial_weight', sa.Float(), nullable=True, comment="The initial total weight of the spool (gross weight)."))
op.add_column('spool', sa.Column('empty_weight', sa.Float(), nullable=True, comment="The weight of the empty spool (tare weight)."))
# ### end Alembic commands ###
def downgrade() -> None:
"""Perform the downgrade."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('spool', 'empty_weight')
op.drop_column('spool', 'initial_weight')
# ### end Alembic commands ###

View File

@@ -0,0 +1,46 @@
"""spool weight population.
Revision ID: 304a32906234
Revises: aafcd7fb0e84
Create Date: 2024-03-26 13:49:26.594399
"""
import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic.
revision = '304a32906234'
down_revision = 'aafcd7fb0e84'
branch_labels = None
depends_on = None
def upgrade() -> None:
filament = sa.Table('filament',
sa.MetaData(),
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('weight', sa.Float(), nullable=True),
sa.Column('spool_weight', sa.Float(), nullable=True))
spool = sa.Table(
'spool',
sa.MetaData(),
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('filament_id', sa.Integer),
sa.Column('initial_weight', sa.Float(), nullable=False, comment="The initial total weight of the spool (gross weight)."),
sa.Column('empty_weight', sa.Float(), nullable=False, comment="The weight of the empty spool (tare weight).")
)
initial_weight = sa.select((filament.c.weight + filament.c.spool_weight).label("initial_weight")).where(filament.c.id == spool.c.filament_id).scalar_subquery()
empty_weight = sa.select(filament.c.spool_weight).where(filament.c.id == spool.c.filament_id).scalar_subquery()
set_initial_weight = sa.update(spool).values(initial_weight=initial_weight)
op.execute(set_initial_weight)
set_empty_weight = sa.update(spool).values(empty_weight=empty_weight)
op.execute(set_empty_weight)
pass
def downgrade() -> None:
"""Perform the downgrade."""
pass

View File

@@ -40,6 +40,16 @@ class SpoolParameters(BaseModel):
description="The price of this filament in the system configured currency.",
example=20.0,
)
initial_weight: Optional[float] = Field(
ge=0,
description="The initial total weight of the spool.",
example=200,
)
empty_weight: Optional[float] = Field(
ge=0,
description="The weight of an empty spool.",
example=200,
)
remaining_weight: Optional[float] = Field(
ge=0,
description=(
@@ -354,6 +364,8 @@ async def create( # noqa: ANN201
db=db,
filament_id=body.filament_id,
price=body.price,
initial_weight=body.initial_weight,
empty_weight=body.empty_weight,
remaining_weight=body.remaining_weight,
used_weight=body.used_weight,
first_used=body.first_used,

View File

@@ -64,6 +64,8 @@ class Spool(Base):
price: Mapped[Optional[float]] = mapped_column()
filament_id: Mapped[int] = mapped_column(ForeignKey("filament.id"))
filament: Mapped["Filament"] = relationship(back_populates="spools")
initial_weight: Mapped[float] = mapped_column()
empty_weight: Mapped[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

@@ -35,6 +35,8 @@ async def create(
db: AsyncSession,
filament_id: int,
remaining_weight: Optional[float] = None,
initial_weight: Optional[float] = None,
empty_weight: Optional[float] = None,
used_weight: Optional[float] = None,
first_used: Optional[datetime] = None,
last_used: Optional[datetime] = None,
@@ -47,11 +49,19 @@ async def create(
) -> models.Spool:
"""Add a new spool to the database. Leave weight empty to assume full spool."""
filament_item = await filament.get_by_id(db, filament_id)
if empty_weight is None:
empty_weight = filament_item.spool_weight if filament_item.spool_weight is not None else 0
# Calculate initial_weight if not provided
if initial_weight is None:
initial_weight = (filament_item.weight if filament_item.weight is not None else 0) + empty_weight
if used_weight is None:
if remaining_weight is not None:
if filament_item.weight is None:
raise ItemCreateError("remaining_weight can only be used if the filament type has a weight set.")
used_weight = max(filament_item.weight - remaining_weight, 0)
if initial_weight is None or initial_weight == 0:
raise ItemCreateError("remaining_weight can only be used if the initial_weight is defined.")
used_weight = max(initial_weight - empty_weight - remaining_weight, 0)
else:
used_weight = 0
@@ -64,6 +74,8 @@ async def create(
spool = models.Spool(
filament=filament_item,
registered=datetime.utcnow().replace(microsecond=0),
initial_weight=initial_weight,
empty_weight=empty_weight,
used_weight=used_weight,
price=price,
first_used=first_used,
@@ -150,7 +162,7 @@ async def find(
for fieldstr, order in sort_by.items():
sorts = []
if fieldstr in {"remaining_weight", "remaining_length"}:
sorts.append(models.Filament.weight - models.Spool.used_weight)
sorts.append(models.Spool.initial_weight - models.Spool.empty_weight - models.Spool.used_weight)
elif fieldstr == "filament.combined_name":
sorts.append(models.Vendor.name)
sorts.append(models.Filament.name)
@@ -181,10 +193,20 @@ async def update(
for k, v in data.items():
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
elif k == "remaining_weight":
if spool.filament.weight is None:
raise ItemCreateError("remaining_weight can only be used if the filament type has a weight set.")
spool.used_weight = max(spool.filament.weight - v, 0)
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)
elif isinstance(v, datetime):
setattr(spool, k, utc_timezone_naive(v))
elif k == "extra":