Skip to content

Commit

Permalink
model: add UTCDateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
utnapischtim committed Dec 16, 2024
1 parent 7308cd9 commit c154dd5
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions invenio_banners/records/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,35 @@

"""Models."""

from datetime import datetime
from datetime import datetime, timezone

import sqlalchemy as sa
from flask import current_app
from invenio_db import db
from sqlalchemy import or_
from sqlalchemy.sql import text
from sqlalchemy.types import DateTime, TypeDecorator
from sqlalchemy_utils.models import Timestamp

from ..services.errors import BannerNotExistsError


class UTCDateTime(TypeDecorator):
"""Custom UTC datetime type."""

impl = DateTime

def process_bind_param(self, value, dialect):
if isinstance(value, datetime):
return value.replace(tzinfo=None)
return value

def process_result_value(self, value, dialect):
if isinstance(value, datetime):
return value.replace(tzinfo=None)
return value


class BannerModel(db.Model, Timestamp):
"""Defines a message to show to users."""

Expand All @@ -36,10 +53,12 @@ class BannerModel(db.Model, Timestamp):
category = db.Column(db.String(20), nullable=False)
"""Category of the message, for styling messages per category."""

start_datetime = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
start_datetime = db.Column(
UTCDateTime, nullable=False, default=lambda: datetime.now(timezone.utc)
)
"""Start date and time (UTC), can be immediate or delayed."""

end_datetime = db.Column(db.DateTime, nullable=True)
end_datetime = db.Column(UTCDateTime, nullable=True)
"""End date and time (UTC), must be after `start` or forever if null."""

active = db.Column(db.Boolean(name="active"), nullable=False, default=True)
Expand Down

0 comments on commit c154dd5

Please sign in to comment.