Add backend support for notifications
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
from sqlalchemy import create_engine, Column, Integer, String, Text, ForeignKey
|
||||
from sqlalchemy import create_engine, Column, Integer, String, Text, ForeignKey, Boolean
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.sql.functions import func
|
||||
from sqlalchemy.sql.sqltypes import DateTime
|
||||
from sqlalchemy.types import Boolean
|
||||
|
||||
# Initialize the database
|
||||
DATABASE_URL = "sqlite:///./project_monitor.db"
|
||||
@@ -50,7 +49,54 @@ class Settings(Base):
|
||||
requirements = Column(String, nullable=False)
|
||||
environment = Column(String, nullable=False)
|
||||
user = Column(String, nullable=False)
|
||||
ntfy_url = Column(String, nullable=True)
|
||||
|
||||
|
||||
class Subscription(Base):
|
||||
__tablename__ = "subscriptions"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
topic = Column(String, nullable=False, unique=True)
|
||||
last_message_id = Column(String, nullable=True)
|
||||
created_at = Column(
|
||||
DateTime(timezone=True), nullable=False, server_default=func.now()
|
||||
)
|
||||
|
||||
|
||||
class Notification(Base):
|
||||
__tablename__ = "notifications"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
title = Column(String(255), nullable=False)
|
||||
message = Column(Text, nullable=False)
|
||||
priority = Column(Integer, nullable=False, default=3)
|
||||
viewed = Column(Boolean, default=False)
|
||||
sent = Column(Boolean, default=False)
|
||||
|
||||
subscription_id = Column(Integer, ForeignKey("subscriptions.id"), nullable=False)
|
||||
created_at = Column(
|
||||
DateTime(timezone=True), nullable=False, server_default=func.now()
|
||||
)
|
||||
|
||||
|
||||
# Create the database tables
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
|
||||
# Ensure a default setting line exists
|
||||
def ensure_default_setting():
|
||||
db = SessionLocal()
|
||||
default_setting = db.query(Settings).filter(Settings.user == "default").first()
|
||||
if not default_setting:
|
||||
new_setting = Settings(
|
||||
requirements="",
|
||||
environment="",
|
||||
user="default",
|
||||
ntfy_url="https://ntfy.abzk.fr",
|
||||
)
|
||||
db.add(new_setting)
|
||||
db.commit()
|
||||
db.close()
|
||||
|
||||
|
||||
ensure_default_setting()
|
||||
|
||||
Reference in New Issue
Block a user