Add services to db if they don't exist

This commit is contained in:
2025-09-03 15:43:28 +02:00
parent bb8f3e6c71
commit 87aacaa08a
4 changed files with 59 additions and 6 deletions

14
app.py
View File

@@ -5,6 +5,7 @@ from mem import services, app, db
import threading
from flask_migrate import upgrade, stamp
from pathlib import Path
from models import service
# Init and upgrade
@@ -17,6 +18,19 @@ with app.app_context():
# Upgrade db if any new migrations exist
upgrade()
with app.app_context():
if not db.session.query(service).first():
for s in services:
db.session.add(
service(
url=s.url,
label=s.label,
public_access=s.public,
ping_method=s.ping_type,
)
)
db.session.commit()
@app.route("/")
def homepage():

View File

@@ -1,5 +0,0 @@
from app import app
from flask_migrate import migrate
with app.app_context():
migrate()

View File

@@ -0,0 +1,35 @@
"""empty message
Revision ID: f04407e8e466
Revises: d7d380435347
Create Date: 2025-09-03 15:40:30.413166
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'f04407e8e466'
down_revision = 'd7d380435347'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('service',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('url', sa.String(), nullable=False),
sa.Column('label', sa.String(length=15), nullable=False),
sa.Column('public_access', sa.Boolean(), nullable=False),
sa.Column('ping_method', sa.Integer(), nullable=False),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('service')
# ### end Alembic commands ###

View File

@@ -1,5 +1,6 @@
from mem import db
from datetime import datetime, timezone
from validators import url as is_url
class log(db.Model):
@@ -19,5 +20,13 @@ class service(db.Model):
public_access: bool = db.Column(db.Boolean, nullable=False)
ping_method: int = db.Column(db.Integer, nullable=False)
def __init__(self):
def __init__(
self, url: str, label: str, public_access: bool, ping_method: int
):
if not is_url(url):
raise Exception("URL IS NOT A VALID URL")
super().__init__()
self.url = url
self.label = label
self.public_access = public_access
self.ping_method = ping_method