Now correctly handles datetimes with timezone set
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
"""Helper functions for interacting with spool database objects."""
|
"""Helper functions for interacting with spool database objects."""
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import UTC, datetime
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
@@ -12,6 +12,11 @@ from spoolman.exceptions import ItemCreateError, ItemNotFoundError
|
|||||||
from spoolman.math import weight_from_length
|
from spoolman.math import weight_from_length
|
||||||
|
|
||||||
|
|
||||||
|
def utc_timezone_naive(dt: datetime) -> datetime:
|
||||||
|
"""Convert a datetime object to UTC and remove timezone info."""
|
||||||
|
return dt.astimezone(UTC).replace(tzinfo=None)
|
||||||
|
|
||||||
|
|
||||||
async def create(
|
async def create(
|
||||||
*,
|
*,
|
||||||
db: AsyncSession,
|
db: AsyncSession,
|
||||||
@@ -34,6 +39,12 @@ async def create(
|
|||||||
else:
|
else:
|
||||||
used_weight = 0
|
used_weight = 0
|
||||||
|
|
||||||
|
# Convert datetime values to UTC and remove timezone info
|
||||||
|
if first_used is not None:
|
||||||
|
first_used = utc_timezone_naive(first_used)
|
||||||
|
if last_used is not None:
|
||||||
|
last_used = utc_timezone_naive(last_used)
|
||||||
|
|
||||||
db_item = models.Spool(
|
db_item = models.Spool(
|
||||||
filament=filament_item,
|
filament=filament_item,
|
||||||
used_weight=used_weight,
|
used_weight=used_weight,
|
||||||
@@ -112,6 +123,8 @@ async def update(
|
|||||||
if spool.filament.weight is None:
|
if spool.filament.weight is None:
|
||||||
raise ItemCreateError("remaining_weight can only be used if the filament type has a weight set.")
|
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)
|
spool.used_weight = max(spool.filament.weight - v, 0)
|
||||||
|
elif isinstance(v, datetime):
|
||||||
|
setattr(spool, k, utc_timezone_naive(v))
|
||||||
else:
|
else:
|
||||||
setattr(spool, k, v)
|
setattr(spool, k, v)
|
||||||
await db.flush()
|
await db.flush()
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ services:
|
|||||||
- SPOOLMAN_DB_PORT=26257
|
- SPOOLMAN_DB_PORT=26257
|
||||||
- SPOOLMAN_DB_NAME=spoolman
|
- SPOOLMAN_DB_NAME=spoolman
|
||||||
- SPOOLMAN_DB_USERNAME=john
|
- SPOOLMAN_DB_USERNAME=john
|
||||||
- SPOOLMAN_LOGGING_LEVEL=DEBUG
|
- SPOOLMAN_LOGGING_LEVEL=INFO
|
||||||
depends_on:
|
depends_on:
|
||||||
db:
|
db:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ services:
|
|||||||
- SPOOLMAN_DB_NAME=spoolman
|
- SPOOLMAN_DB_NAME=spoolman
|
||||||
- SPOOLMAN_DB_USERNAME=john
|
- SPOOLMAN_DB_USERNAME=john
|
||||||
- SPOOLMAN_DB_PASSWORD=abc
|
- SPOOLMAN_DB_PASSWORD=abc
|
||||||
- SPOOLMAN_LOGGING_LEVEL=DEBUG
|
- SPOOLMAN_LOGGING_LEVEL=INFO
|
||||||
depends_on:
|
depends_on:
|
||||||
db:
|
db:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ services:
|
|||||||
- SPOOLMAN_DB_NAME=postgres
|
- SPOOLMAN_DB_NAME=postgres
|
||||||
- SPOOLMAN_DB_USERNAME=postgres
|
- SPOOLMAN_DB_USERNAME=postgres
|
||||||
- SPOOLMAN_DB_PASSWORD=abc
|
- SPOOLMAN_DB_PASSWORD=abc
|
||||||
- SPOOLMAN_LOGGING_LEVEL=DEBUG
|
- SPOOLMAN_LOGGING_LEVEL=INFO
|
||||||
depends_on:
|
depends_on:
|
||||||
- db
|
- db
|
||||||
tester:
|
tester:
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ services:
|
|||||||
spoolman:
|
spoolman:
|
||||||
image: donkie/spoolman:test
|
image: donkie/spoolman:test
|
||||||
environment:
|
environment:
|
||||||
- SPOOLMAN_LOGGING_LEVEL=DEBUG
|
- SPOOLMAN_LOGGING_LEVEL=INFO
|
||||||
tester:
|
tester:
|
||||||
image: donkie/spoolman-tester:latest
|
image: donkie/spoolman-tester:latest
|
||||||
volumes:
|
volumes:
|
||||||
|
|||||||
@@ -12,8 +12,6 @@ URL = "http://spoolman:8000"
|
|||||||
def test_add_spool_remaining_weight(random_filament: dict[str, Any]):
|
def test_add_spool_remaining_weight(random_filament: dict[str, Any]):
|
||||||
"""Test adding a spool to the database."""
|
"""Test adding a spool to the database."""
|
||||||
# Execute
|
# Execute
|
||||||
first_used = "2023-01-01T00:00:00"
|
|
||||||
last_used = "2023-01-02T00:00:00"
|
|
||||||
remaining_weight = 750
|
remaining_weight = 750
|
||||||
location = "The Pantry"
|
location = "The Pantry"
|
||||||
lot_nr = "123456789"
|
lot_nr = "123456789"
|
||||||
@@ -21,8 +19,8 @@ def test_add_spool_remaining_weight(random_filament: dict[str, Any]):
|
|||||||
result = httpx.post(
|
result = httpx.post(
|
||||||
f"{URL}/api/v1/spool",
|
f"{URL}/api/v1/spool",
|
||||||
json={
|
json={
|
||||||
"first_used": first_used,
|
"first_used": "2023-01-02T12:00:00+01:00",
|
||||||
"last_used": last_used,
|
"last_used": "2023-01-02T11:00:00Z",
|
||||||
"filament_id": random_filament["id"],
|
"filament_id": random_filament["id"],
|
||||||
"remaining_weight": remaining_weight,
|
"remaining_weight": remaining_weight,
|
||||||
"location": location,
|
"location": location,
|
||||||
@@ -37,8 +35,8 @@ def test_add_spool_remaining_weight(random_filament: dict[str, Any]):
|
|||||||
assert spool == {
|
assert spool == {
|
||||||
"id": spool["id"],
|
"id": spool["id"],
|
||||||
"registered": spool["registered"],
|
"registered": spool["registered"],
|
||||||
"first_used": first_used,
|
"first_used": "2023-01-02T11:00:00",
|
||||||
"last_used": last_used,
|
"last_used": "2023-01-02T11:00:00",
|
||||||
"filament": random_filament,
|
"filament": random_filament,
|
||||||
"remaining_weight": pytest.approx(remaining_weight),
|
"remaining_weight": pytest.approx(remaining_weight),
|
||||||
"used_weight": pytest.approx(random_filament["weight"] - remaining_weight),
|
"used_weight": pytest.approx(random_filament["weight"] - remaining_weight),
|
||||||
@@ -362,8 +360,8 @@ def test_update_spool(random_filament: dict[str, Any]):
|
|||||||
spool = result.json()
|
spool = result.json()
|
||||||
|
|
||||||
# Execute
|
# Execute
|
||||||
first_used = "2023-01-01T00:00:00"
|
first_used = "2023-01-01T12:00:00+02:00"
|
||||||
last_used = "2023-01-02T00:00:00"
|
last_used = "2023-01-02T12:00:00+02:00"
|
||||||
remaining_weight = 750
|
remaining_weight = 750
|
||||||
location = "Living Room"
|
location = "Living Room"
|
||||||
lot_nr = "987654321"
|
lot_nr = "987654321"
|
||||||
@@ -383,8 +381,8 @@ def test_update_spool(random_filament: dict[str, Any]):
|
|||||||
|
|
||||||
# Verify
|
# Verify
|
||||||
spool = result.json()
|
spool = result.json()
|
||||||
assert spool["first_used"] == first_used
|
assert spool["first_used"] == "2023-01-01T10:00:00"
|
||||||
assert spool["last_used"] == last_used
|
assert spool["last_used"] == "2023-01-02T10:00:00"
|
||||||
assert spool["remaining_weight"] == pytest.approx(remaining_weight)
|
assert spool["remaining_weight"] == pytest.approx(remaining_weight)
|
||||||
assert spool["used_weight"] == pytest.approx(random_filament["weight"] - remaining_weight)
|
assert spool["used_weight"] == pytest.approx(random_filament["weight"] - remaining_weight)
|
||||||
assert spool["location"] == location
|
assert spool["location"] == location
|
||||||
|
|||||||
Reference in New Issue
Block a user