From 00fde81ad27eebf5ca4aff29a10fc60532a1bef6 Mon Sep 17 00:00:00 2001 From: Akashdeep Dhar Date: Tue, 18 Jun 2024 11:21:46 +0530 Subject: [PATCH] Add database models for basic entities Signed-off-by: Akashdeep Dhar --- webhook_to_fedora_messaging/models/apikey.py | 21 ++++++++++ webhook_to_fedora_messaging/models/service.py | 19 +++++++++ webhook_to_fedora_messaging/models/user.py | 16 ++++++++ webhook_to_fedora_messaging/models/util.py | 40 +++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 webhook_to_fedora_messaging/models/apikey.py create mode 100644 webhook_to_fedora_messaging/models/service.py create mode 100644 webhook_to_fedora_messaging/models/user.py create mode 100644 webhook_to_fedora_messaging/models/util.py diff --git a/webhook_to_fedora_messaging/models/apikey.py b/webhook_to_fedora_messaging/models/apikey.py new file mode 100644 index 0000000..a218e4a --- /dev/null +++ b/webhook_to_fedora_messaging/models/apikey.py @@ -0,0 +1,21 @@ +# SPDX-FileCopyrightText: Contributors to the Fedora Project +# +# SPDX-License-Identifier: GPL-3.0-or-later + +from sqlalchemy import Column, Integer, UnicodeText, Boolean, ForeignKey, DateTime +from webhook_to_fedora_messaging.models.util import UUIDCreatableMixin, CreatableMixin + +from webhook_to_fedora_messaging.database import Base + +from uuid import uuid4 + + +class APIKey(Base, UUIDCreatableMixin, CreatableMixin): + __tablename__ = "apikeys" + + id = Column(Integer, primary_key=True, nullable=False) + user_id = Column(Integer, ForeignKey("user.id", ondelete="CASCADE"), unique=False, nullable=False) + name = Column(UnicodeText, nullable=False) + token = Column(UnicodeText, unique=True, nullable=False, default=uuid4().hex) + expiry_date = Column(DateTime, nullable=True) + disabled = Column(Boolean, nullable=False, default=False) diff --git a/webhook_to_fedora_messaging/models/service.py b/webhook_to_fedora_messaging/models/service.py new file mode 100644 index 0000000..62ad780 --- /dev/null +++ b/webhook_to_fedora_messaging/models/service.py @@ -0,0 +1,19 @@ +# SPDX-FileCopyrightText: Contributors to the Fedora Project +# +# SPDX-License-Identifier: GPL-3.0-or-later + +from sqlalchemy import Column, Integer, UnicodeText, Boolean, ForeignKey +from webhook_to_fedora_messaging.models.util import UUIDCreatableMixin, CreatableMixin + +from webhook_to_fedora_messaging.database import Base + + +class Service(Base, UUIDCreatableMixin, CreatableMixin): + __tablename__ = "services" + + id = Column(Integer, primary_key=True, nullable=False) + user_id = Column(Integer, ForeignKey("user.id", ondelete="CASCADE"), unique=False, nullable=False) + name = Column(UnicodeText, nullable=False) + type = Column(UnicodeText, nullable=False) + desc = Column(UnicodeText, nullable=False) + disabled = Column(Boolean, nullable=False, default=False) diff --git a/webhook_to_fedora_messaging/models/user.py b/webhook_to_fedora_messaging/models/user.py new file mode 100644 index 0000000..a3fc400 --- /dev/null +++ b/webhook_to_fedora_messaging/models/user.py @@ -0,0 +1,16 @@ +# SPDX-FileCopyrightText: Contributors to the Fedora Project +# +# SPDX-License-Identifier: GPL-3.0-or-later + +from sqlalchemy import Column, Integer, UnicodeText, Boolean +from webhook_to_fedora_messaging.models.util import UUIDCreatableMixin, CreatableMixin + +from webhook_to_fedora_messaging.database import Base + + +class User(Base, UUIDCreatableMixin, CreatableMixin): + __tablename__ = "users" + + id = Column(Integer, primary_key=True, nullable=False) + username = Column(UnicodeText, unique=True, nullable=False) + is_admin = Column(Boolean, nullable=False, default=False) diff --git a/webhook_to_fedora_messaging/models/util.py b/webhook_to_fedora_messaging/models/util.py new file mode 100644 index 0000000..38e52a0 --- /dev/null +++ b/webhook_to_fedora_messaging/models/util.py @@ -0,0 +1,40 @@ +# SPDX-FileCopyrightText: Contributors to the Fedora Project +# +# SPDX-License-Identifier: GPL-3.0-or-later + + +from sqlalchemy import Column, UnicodeText +from sqlalchemy.ext.compiler import compiles +from sqlalchemy.sql.expression import FunctionElement +from sqlalchemy.types import DateTime as SQLDateTime + +from uuid import uuid4 + + +class utcnow(FunctionElement): + """ + Current timestamp in UTC for SQL expressions + """ + type = SQLDateTime + inherit_cache = True + + +@compiles(utcnow, "postgresql") +def _postgresql_utcnow(element, compiler, **kwargs): + return "(NOW() AT TIME ZONE 'utc')" + + +class UUIDCreatableMixin: + """ + An SQLAlchemy mixin to automatically generate a custom 8-digit UUID string + """ + + uuid = Column("uuid", UnicodeText, unique=True, nullable=False, default=uuid4().hex[0:8]) + + +class CreatableMixin: + """ + An SQLAlchemy mixin to store the time when an entity was created + """ + + creation_date = Column("creation_date", SQLDateTime, nullable=False, server_default=utcnow())