location fully covered

This commit is contained in:
2024-08-29 14:47:18 +02:00
parent b8faf975bf
commit f1d8f53142
3 changed files with 111 additions and 9 deletions

View File

@@ -1,5 +1,5 @@
import datetime
from dataclasses import dataclass
from datetime import UTC, datetime
from sqlalchemy import REAL, TEXT, insert, text
from sqlalchemy.ext.asyncio import AsyncConnection, create_async_engine
@@ -45,12 +45,15 @@ class LocationRecorder:
async def dispose_db_engine(self) -> None:
await self._engine.dispose()
async def insert_location(self, person: str, datetime: str, location: LocationData) -> None:
async def insert_location(self, person: str, date_time: datetime, location: LocationData) -> None:
if date_time.tzinfo != UTC:
date_time = date_time.astimezone(UTC)
date_time_str = date_time.strftime("%Y-%m-%dT%H:%M:%S%z")
async with self._engine.connect() as conn:
await conn.execute(
insert(Location).values(
person=person,
datetime=datetime,
datetime=date_time_str,
latitude=location.latitude,
longitude=location.longitude,
altitude=location.altitude,
@@ -60,9 +63,8 @@ class LocationRecorder:
await conn.aclose()
async def insert_location_now(self, person: str, location: LocationData) -> None:
now = datetime.datetime.now(tz=datetime.UTC)
now_str = now.strftime("%Y-%m-%dT%H:%M:%S%z")
await self.insert_location(person, now_str, location)
now_utc = datetime.now(tz=UTC)
await self.insert_location(person, now_utc, location)
async def _get_user_version(self, conn: AsyncConnection) -> int:
return (await conn.execute(text("PRAGMA user_version"))).first()[0]