diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3f68b56f..514991df 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -57,6 +57,7 @@ repos: "types-requests", "sqlmodel", "types-Markdown", + types-tzlocal, ] args: ["--check-untyped-defs", "--ignore-missing-imports"] exclude: "^templates/" diff --git a/libs/ktem/ktem/db/base_models.py b/libs/ktem/ktem/db/base_models.py index 7d8b3e50..0c6004bd 100644 --- a/libs/ktem/ktem/db/base_models.py +++ b/libs/ktem/ktem/db/base_models.py @@ -1,11 +1,10 @@ import datetime import uuid from typing import Optional -from zoneinfo import ZoneInfo from sqlalchemy import JSON, Column from sqlmodel import Field, SQLModel -from theflow.settings import settings as flowsettings +from tzlocal import get_localzone class BaseConversation(SQLModel): @@ -26,9 +25,9 @@ class BaseConversation(SQLModel): default_factory=lambda: uuid.uuid4().hex, primary_key=True, index=True ) name: str = Field( - default_factory=lambda: datetime.datetime.now( - ZoneInfo(getattr(flowsettings, "TIME_ZONE", "UTC")) - ).strftime("%Y-%m-%d %H:%M:%S") + default_factory=lambda: datetime.datetime.now(get_localzone()).strftime( + "%Y-%m-%d %H:%M:%S" + ) ) user: int = Field(default=0) # For now we only have one user @@ -37,8 +36,12 @@ class BaseConversation(SQLModel): # contains messages + current files + chat_suggestions data_source: dict = Field(default={}, sa_column=Column(JSON)) - date_created: datetime.datetime = Field(default_factory=datetime.datetime.utcnow) - date_updated: datetime.datetime = Field(default_factory=datetime.datetime.utcnow) + date_created: datetime.datetime = Field( + default_factory=lambda: datetime.datetime.now(get_localzone()) + ) + date_updated: datetime.datetime = Field( + default_factory=lambda: datetime.datetime.now(get_localzone()) + ) class BaseUser(SQLModel): diff --git a/libs/ktem/ktem/index/file/index.py b/libs/ktem/ktem/index/file/index.py index 896bc812..f202a6a8 100644 --- a/libs/ktem/ktem/index/file/index.py +++ b/libs/ktem/ktem/index/file/index.py @@ -1,4 +1,5 @@ import uuid +from datetime import datetime from typing import Any, Optional, Type from ktem.components import filestorage_path, get_docstore, get_vectorstore @@ -7,9 +8,9 @@ from sqlalchemy import JSON, Column, DateTime, Integer, String, UniqueConstraint from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.mutable import MutableDict -from sqlalchemy.sql import func from theflow.settings import settings as flowsettings from theflow.utils.modules import import_dotted_string +from tzlocal import get_localzone from kotaemon.storages import BaseDocumentStore, BaseVectorStore @@ -73,7 +74,7 @@ def _setup_resources(self): "path": Column(String), "size": Column(Integer, default=0), "date_created": Column( - DateTime(timezone=True), server_default=func.now() + DateTime(timezone=True), default=datetime.now(get_localzone()) ), "user": Column(Integer, default=1), "note": Column( @@ -98,7 +99,7 @@ def _setup_resources(self): "path": Column(String), "size": Column(Integer, default=0), "date_created": Column( - DateTime(timezone=True), server_default=func.now() + DateTime(timezone=True), default=datetime.now(get_localzone()) ), "user": Column(Integer, default=1), "note": Column( @@ -126,7 +127,7 @@ def _setup_resources(self): "__tablename__": f"index__{self.id}__group", "id": Column(Integer, primary_key=True, autoincrement=True), "date_created": Column( - DateTime(timezone=True), server_default=func.now() + DateTime(timezone=True), default=datetime.now(get_localzone()) ), "name": Column(String, unique=True), "user": Column(Integer, default=1), diff --git a/libs/ktem/pyproject.toml b/libs/ktem/pyproject.toml index b3890214..1fc420bb 100644 --- a/libs/ktem/pyproject.toml +++ b/libs/ktem/pyproject.toml @@ -29,6 +29,7 @@ dependencies = [ "gradio>=4.31.0,<5", "python-multipart==0.0.12", # required for gradio, pinning to avoid yanking issues with micropip (fixed in gradio >= 5.4.0) "markdown>=3.6,<4", + "tzlocal>=5.0", ] authors = [ { name = "@trducng", email = "john@cinnamon.is" }, diff --git a/libs/ktem/requirements.txt b/libs/ktem/requirements.txt index 67fd014b..c2e28315 100644 --- a/libs/ktem/requirements.txt +++ b/libs/ktem/requirements.txt @@ -1 +1,2 @@ platformdirs +tzlocal diff --git a/scripts/migrate/migrate_chroma_db.py b/scripts/migrate/migrate_chroma_db.py index adffca0f..1503906f 100644 --- a/scripts/migrate/migrate_chroma_db.py +++ b/scripts/migrate/migrate_chroma_db.py @@ -1,4 +1,5 @@ import uuid +from datetime import datetime import chromadb from ktem.index.models import Index @@ -15,7 +16,7 @@ from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.mutable import MutableDict from sqlalchemy.orm import Session -from sqlalchemy.sql import func +from tzlocal import get_localzone def _init_resource(private: bool = True, id: int = 1): @@ -41,7 +42,7 @@ def _init_resource(private: bool = True, id: int = 1): "path": Column(String), "size": Column(Integer, default=0), "date_created": Column( - DateTime(timezone=True), server_default=func.now() + DateTime(timezone=True), default=datetime.now(get_localzone()) ), "user": Column(Integer, default=1), "note": Column( @@ -66,7 +67,7 @@ def _init_resource(private: bool = True, id: int = 1): "path": Column(String), "size": Column(Integer, default=0), "date_created": Column( - DateTime(timezone=True), server_default=func.now() + DateTime(timezone=True), default=datetime.now(get_localzone()) ), "user": Column(Integer, default=1), "note": Column(