add: webhook
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
import os
|
||||
import subprocess
|
||||
from contextlib import contextmanager
|
||||
from sqlalchemy.schema import CreateTable
|
||||
|
||||
@@ -21,15 +19,6 @@ def get_db():
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
# def dump_db():
|
||||
# try:
|
||||
# os.environ['MYSQL_PWD'] = DB_PASSWORD
|
||||
# dump_cmd = f"mysqldump --no-tablespaces -h {DB_HOST} -P {DB_PORT} -u {DB_USER} {DB_NAME} > /app/dump/db_dump.sql"
|
||||
# subprocess.run(dump_cmd, shell=True, check=True)
|
||||
# except subprocess.CalledProcessError as e:
|
||||
# print(f"Failed to dump database: {e}")
|
||||
# raise e
|
||||
|
||||
def clear_db():
|
||||
with engine.connect() as conn:
|
||||
conn.execute(text("SET FOREIGN_KEY_CHECKS = 0;"))
|
||||
@@ -60,9 +49,9 @@ def init_payload():
|
||||
for model in table_models:
|
||||
print(str(CreateTable(model.__table__)))
|
||||
|
||||
print(f"MODEL -- {model}, {hasattr(model, '__pay_load__')}")
|
||||
if hasattr(model, "__pay_load__"):
|
||||
payload = model.__pay_load__[ENVIRONMENT]
|
||||
print(f"MODEL -- {model}, {hasattr(model, '__payload__')}")
|
||||
if hasattr(model, "__payload__"):
|
||||
payload = model.__payload__[ENVIRONMENT]
|
||||
print(f"- - [ - ] hasattr, {ENVIRONMENT} - {payload}")
|
||||
stmt = insert(model.__table__).values(payload).prefix_with("IGNORE")
|
||||
print(f"- - [ - ] {stmt}\n")
|
||||
@@ -73,11 +62,6 @@ def init_payload():
|
||||
|
||||
def setup_db():
|
||||
if DB_SCHEMA_UPDATED:
|
||||
# try:
|
||||
# dump_db()
|
||||
# print("[ x ] db dumped")
|
||||
# except Exception as e:
|
||||
# print(f"[ x ] Failed to dump database: {e}")
|
||||
clear_db()
|
||||
print("[ x ] db cleared")
|
||||
create_all()
|
||||
|
||||
@@ -9,7 +9,7 @@ class Markdown(Base):
|
||||
id = Column(Integer, primary_key=True)
|
||||
title = Column(String(255), nullable=False)
|
||||
content = Column(Text, nullable=False)
|
||||
created_at = Column(DateTime, default=datetime.datetime.utcnow)
|
||||
created_at = Column(DateTime, default=datetime.datetime.now(datetime.UTC))
|
||||
path_id = Column(Integer, ForeignKey('path.id'), nullable=False)
|
||||
order = Column(String(36), default=lambda: str(uuid.uuid4()))
|
||||
shortcut = Column(String(36), default="")
|
||||
@@ -23,7 +23,7 @@ class Markdown(Base):
|
||||
'order': self.order,
|
||||
'shortcut': self.shortcut,
|
||||
}
|
||||
__pay_load__ = {
|
||||
__payload__ = {
|
||||
'dev': [
|
||||
{'id': 1, 'title': 'index', 'content': ' ', 'created_at': datetime.datetime.utcnow, 'path_id': 1 },
|
||||
],
|
||||
|
||||
@@ -19,7 +19,7 @@ class Path(Base):
|
||||
"order": self.order,
|
||||
}
|
||||
|
||||
__pay_load__ = {
|
||||
__payload__ = {
|
||||
'dev': [
|
||||
{'id': 1, 'name': '', 'parent_id': None},
|
||||
{'id': 2, 'name': 'test', 'parent_id': 1},
|
||||
|
||||
16
db/models/Webhook.py
Normal file
16
db/models/Webhook.py
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
from sqlalchemy import Column, String, Integer, ForeignKey, UniqueConstraint, text, Boolean
|
||||
from db.models import Base
|
||||
|
||||
|
||||
class Webhook(Base):
|
||||
__tablename__ = 'webhook'
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
hook_url = Column(String(100), nullable=False)
|
||||
__table_args__ = (UniqueConstraint('hook_url'),)
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
"id": self.id,
|
||||
"hook_url": self.hook_url
|
||||
}
|
||||
26
db/models/WebhookSetting.py
Normal file
26
db/models/WebhookSetting.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from sqlalchemy import Column, String, Integer, ForeignKey, UniqueConstraint, Boolean
|
||||
from db.models import Base
|
||||
|
||||
|
||||
class WebhookSetting(Base):
|
||||
__tablename__ = 'webhook_setting'
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
path_id = Column(Integer, ForeignKey('path.id'), nullable=False)
|
||||
webhook_id = Column(Integer, ForeignKey('webhook.id'), nullable=False)
|
||||
recursive = Column(Boolean, default=False)
|
||||
additional_header = Column(String(500), nullable=True)
|
||||
enabled = Column(Boolean, default=True)
|
||||
on_events = Column(Integer, default=0)
|
||||
|
||||
__table_args__ = (UniqueConstraint('webhook_id', 'path_id', name='_webhook_path_uc'),)
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
"id": self.id,
|
||||
"path_id": self.path_id,
|
||||
"webhook_id": self.webhook_id,
|
||||
"recursive": self.recursive,
|
||||
"additional_header": self.additional_header,
|
||||
"enabled": self.enabled,
|
||||
"on_events": self.on_events,
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
#db/models/__init__.py
|
||||
|
||||
import pkgutil
|
||||
import importlib
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import declarative_base
|
||||
Base = declarative_base()
|
||||
package_name = "db.models"
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
#db/utils.py
|
||||
from db import get_db
|
||||
|
||||
def insert_log(log_entry):
|
||||
|
||||
Reference in New Issue
Block a user