Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
Define generate_timestamp utils func
Browse files Browse the repository at this point in the history
  • Loading branch information
Sinclert committed Jan 25, 2024
1 parent d4fd8c5 commit 17c27ee
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/dialect_map_core/models/__utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import uuid

from datetime import datetime
from datetime import timezone


def generate_id() -> str:
"""
Expand All @@ -10,3 +13,12 @@ def generate_id() -> str:
"""

return str(uuid.uuid4()).replace("-", "")


def generate_timestamp() -> datetime:
"""
Generates a UTC timestamp
:return: UTC timestamp
"""

return datetime.now(timezone.utc)
11 changes: 6 additions & 5 deletions src/dialect_map_core/models/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-

from abc import abstractmethod
from datetime import datetime

from sqlalchemy import Boolean
from sqlalchemy import DateTime
Expand All @@ -11,6 +10,8 @@
from sqlalchemy.orm import mapped_column as Column
from sqlalchemy.orm import validates

from .__utils import generate_timestamp


class Base(DeclarativeBase):
"""Base class for all the Python data models"""
Expand Down Expand Up @@ -82,7 +83,7 @@ def created_at(self):

@declared_attr
def audited_at(self):
return Column(DateTime, nullable=True, index=False, default=datetime.utcnow)
return Column(DateTime, nullable=True, index=False, default=generate_timestamp)

@validates("audited_at")
def check_audited(self, key, val):
Expand Down Expand Up @@ -133,7 +134,7 @@ def archived_at(self):

@declared_attr
def audited_at(self):
return Column(DateTime, nullable=True, index=False, default=datetime.utcnow)
return Column(DateTime, nullable=True, index=False, default=generate_timestamp)

@validates("archived_at")
def check_archived(self, key, val):
Expand Down Expand Up @@ -189,11 +190,11 @@ def created_at(self):

@declared_attr
def updated_at(self):
return Column(DateTime, nullable=False, index=True, onupdate=datetime.utcnow)
return Column(DateTime, nullable=False, index=True, onupdate=generate_timestamp)

@declared_attr
def audited_at(self):
return Column(DateTime, nullable=True, index=False, default=datetime.utcnow)
return Column(DateTime, nullable=True, index=False, default=generate_timestamp)

@validates("audited_at")
def check_audited(self, key, val):
Expand Down

0 comments on commit 17c27ee

Please sign in to comment.