From b63b6e7ee7beaafb05002d0c7d49d07a560c110b Mon Sep 17 00:00:00 2001 From: Ilya Y <75578537+ilya-bkv@users.noreply.github.com> Date: Tue, 14 Nov 2023 14:56:41 +0300 Subject: [PATCH] timezones fixed once again (#107) Co-authored-by: Igor Lobanov --- orm/collection.py | 8 +++----- orm/community.py | 12 +++++++----- orm/notification.py | 7 ++++--- orm/reaction.py | 11 ++++++----- orm/shout.py | 29 ++++++++++++++++++++--------- orm/topic.py | 8 ++++---- orm/user.py | 18 +++++++++++------- 7 files changed, 55 insertions(+), 38 deletions(-) diff --git a/orm/collection.py b/orm/collection.py index 8493844c..a8078867 100644 --- a/orm/collection.py +++ b/orm/collection.py @@ -1,6 +1,4 @@ -from datetime import datetime - -from sqlalchemy import Column, DateTime, ForeignKey, String +from sqlalchemy import Column, DateTime, ForeignKey, String, func from base.orm import Base @@ -20,6 +18,6 @@ class Collection(Base): title = Column(String, nullable=False, comment="Title") body = Column(String, nullable=True, comment="Body") pic = Column(String, nullable=True, comment="Picture") - createdAt = Column(DateTime, default=datetime.now, comment="Created At") + createdAt = Column(DateTime(timezone=True), server_default=func.now(), comment="Created At") createdBy = Column(ForeignKey("user.id"), comment="Created By") - publishedAt = Column(DateTime, default=datetime.now, comment="Published At") + publishedAt = Column(DateTime(timezone=True), server_default=func.now(), comment="Published At") diff --git a/orm/community.py b/orm/community.py index 4cbfcc7a..762fe154 100644 --- a/orm/community.py +++ b/orm/community.py @@ -1,6 +1,4 @@ -from datetime import datetime - -from sqlalchemy import Column, DateTime, ForeignKey, String +from sqlalchemy import Column, DateTime, ForeignKey, String, func from base.orm import Base, local_session @@ -11,7 +9,9 @@ class CommunityFollower(Base): id = None follower: Column = Column(ForeignKey("user.id"), primary_key=True) community: Column = Column(ForeignKey("community.id"), primary_key=True) - joinedAt = Column(DateTime, nullable=False, default=datetime.now, comment="Created at") + joinedAt = Column( + DateTime(timezone=True), nullable=False, server_default=func.now(), comment="Created at" + ) # role = Column(ForeignKey(Role.id), nullable=False, comment="Role for member") @@ -22,7 +22,9 @@ class Community(Base): slug = Column(String, nullable=False, unique=True, comment="Slug") desc = Column(String, nullable=False, default="") pic = Column(String, nullable=False, default="") - createdAt = Column(DateTime, nullable=False, default=datetime.now, comment="Created at") + createdAt = Column( + DateTime(timezone=True), nullable=False, server_default=func.now(), comment="Created at" + ) @staticmethod def init_table(): diff --git a/orm/notification.py b/orm/notification.py index 429f07f2..8130b0bb 100644 --- a/orm/notification.py +++ b/orm/notification.py @@ -1,7 +1,6 @@ -from datetime import datetime from enum import Enum as Enumeration -from sqlalchemy import Boolean, Column, DateTime, Enum, ForeignKey, Integer +from sqlalchemy import Boolean, Column, DateTime, Enum, ForeignKey, Integer, func from sqlalchemy.dialects.postgresql import JSONB from base.orm import Base @@ -18,7 +17,9 @@ class Notification(Base): shout: Column = Column(ForeignKey("shout.id"), index=True) reaction: Column = Column(ForeignKey("reaction.id"), index=True) user: Column = Column(ForeignKey("user.id"), index=True) - createdAt = Column(DateTime, nullable=False, default=datetime.now, index=True) + createdAt = Column( + DateTime(timezone=True), nullable=False, server_default=func.now(), index=True + ) seen = Column(Boolean, nullable=False, default=False, index=True) type = Column(Enum(NotificationType), nullable=False) data = Column(JSONB, nullable=True) diff --git a/orm/reaction.py b/orm/reaction.py index 38520b72..d5ed55cb 100644 --- a/orm/reaction.py +++ b/orm/reaction.py @@ -1,7 +1,6 @@ -from datetime import datetime from enum import Enum as Enumeration -from sqlalchemy import Column, DateTime, Enum, ForeignKey, String +from sqlalchemy import Column, DateTime, Enum, ForeignKey, String, func from base.orm import Base @@ -27,13 +26,15 @@ class ReactionKind(Enumeration): class Reaction(Base): __tablename__ = "reaction" body = Column(String, nullable=True, comment="Reaction Body") - createdAt = Column(DateTime, nullable=False, default=datetime.now, comment="Created at") + createdAt = Column( + DateTime(timezone=True), nullable=False, server_default=func.now(), comment="Created at" + ) createdBy: Column = Column(ForeignKey("user.id"), nullable=False, index=True, comment="Sender") - updatedAt = Column(DateTime, nullable=True, comment="Updated at") + updatedAt = Column(DateTime(timezone=True), nullable=True, comment="Updated at") updatedBy: Column = Column( ForeignKey("user.id"), nullable=True, index=True, comment="Last Editor" ) - deletedAt = Column(DateTime, nullable=True, comment="Deleted at") + deletedAt = Column(DateTime(timezone=True), nullable=True, comment="Deleted at") deletedBy: Column = Column( ForeignKey("user.id"), nullable=True, index=True, comment="Deleted by" ) diff --git a/orm/shout.py b/orm/shout.py index b1300ec6..e753faa5 100644 --- a/orm/shout.py +++ b/orm/shout.py @@ -1,6 +1,13 @@ -from datetime import datetime - -from sqlalchemy import JSON, Boolean, Column, DateTime, ForeignKey, Integer, String +from sqlalchemy import ( + JSON, + Boolean, + Column, + DateTime, + ForeignKey, + Integer, + String, + func, +) from sqlalchemy.orm import column_property, relationship from base.orm import Base, local_session @@ -24,8 +31,10 @@ class ShoutReactionsFollower(Base): follower: Column = Column(ForeignKey("user.id"), primary_key=True, index=True) shout: Column = Column(ForeignKey("shout.id"), primary_key=True, index=True) auto = Column(Boolean, nullable=False, default=False) - createdAt = Column(DateTime, nullable=False, default=datetime.now, comment="Created at") - deletedAt = Column(DateTime, nullable=True) + createdAt = Column( + DateTime(timezone=True), nullable=False, server_default=func.now(), comment="Created at" + ) + deletedAt = Column(DateTime(timezone=True), nullable=True) class ShoutAuthor(Base): @@ -41,10 +50,12 @@ class Shout(Base): __tablename__ = "shout" # timestamps - createdAt = Column(DateTime, nullable=False, default=datetime.now, comment="Created at") - updatedAt = Column(DateTime, nullable=True, comment="Updated at") - publishedAt = Column(DateTime, nullable=True) - deletedAt = Column(DateTime, nullable=True) + createdAt = Column( + DateTime(timezone=True), nullable=False, server_default=func.now(), comment="Created at" + ) + updatedAt = Column(DateTime(timezone=True), nullable=True, comment="Updated at") + publishedAt = Column(DateTime(timezone=True), nullable=True) + deletedAt = Column(DateTime(timezone=True), nullable=True) createdBy: Column = Column(ForeignKey("user.id"), comment="Created By") deletedBy: Column = Column(ForeignKey("user.id"), nullable=True) diff --git a/orm/topic.py b/orm/topic.py index 375d5834..0b42d3cb 100644 --- a/orm/topic.py +++ b/orm/topic.py @@ -1,6 +1,4 @@ -from datetime import datetime - -from sqlalchemy import Boolean, Column, DateTime, ForeignKey, String +from sqlalchemy import Boolean, Column, DateTime, ForeignKey, String, func from base.orm import Base @@ -11,7 +9,9 @@ class TopicFollower(Base): id = None follower: Column = Column(ForeignKey("user.id"), primary_key=True, index=True) topic: Column = Column(ForeignKey("topic.id"), primary_key=True, index=True) - createdAt = Column(DateTime, nullable=False, default=datetime.now, comment="Created at") + createdAt = Column( + DateTime(timezone=True), nullable=False, server_default=func.now(), comment="Created at" + ) auto = Column(Boolean, nullable=False, default=False) diff --git a/orm/user.py b/orm/user.py index 5379b586..b95891a7 100644 --- a/orm/user.py +++ b/orm/user.py @@ -1,7 +1,5 @@ -from datetime import datetime - from sqlalchemy import JSON as JSONType -from sqlalchemy import Boolean, Column, DateTime, ForeignKey, Integer, String +from sqlalchemy import Boolean, Column, DateTime, ForeignKey, Integer, String, func from sqlalchemy.orm import relationship from base.orm import Base, local_session @@ -35,7 +33,9 @@ class AuthorFollower(Base): id = None follower: Column = Column(ForeignKey("user.id"), primary_key=True, index=True) author: Column = Column(ForeignKey("user.id"), primary_key=True, index=True) - createdAt = Column(DateTime, nullable=False, default=datetime.now, comment="Created at") + createdAt = Column( + DateTime(timezone=True), nullable=False, server_default=func.now(), comment="Created at" + ) auto = Column(Boolean, nullable=False, default=False) @@ -53,9 +53,13 @@ class User(Base): slug = Column(String, unique=True, comment="User's slug") muted = Column(Boolean, default=False) emailConfirmed = Column(Boolean, default=False) - createdAt = Column(DateTime, nullable=False, default=datetime.now, comment="Created at") - lastSeen = Column(DateTime, nullable=False, default=datetime.now, comment="Was online at") - deletedAt = Column(DateTime, nullable=True, comment="Deleted at") + createdAt = Column( + DateTime(timezone=True), nullable=False, server_default=func.now(), comment="Created at" + ) + lastSeen = Column( + DateTime(timezone=True), nullable=False, server_default=func.now(), comment="Was online at" + ) + deletedAt = Column(DateTime(timezone=True), nullable=True, comment="Deleted at") links = Column(JSONType, nullable=True, comment="Links") oauth = Column(String, nullable=True) ratings = relationship(UserRating, foreign_keys=UserRating.user)