fix: template defects

This commit is contained in:
h z
2025-04-17 21:44:45 +01:00
parent fa855bc7bb
commit 0186a95dd4
10 changed files with 15 additions and 24 deletions

View File

@@ -69,9 +69,10 @@ def create_markdown():
content = data.get('content') content = data.get('content')
path_id = data.get('path_id') path_id = data.get('path_id')
shortcut = data.get('shortcut', "") shortcut = data.get('shortcut', "")
setting_id = data.get('setting_id', None)
if not title or not content: if not title or not content:
return jsonify({"error": "missing required fields"}), 400 return jsonify({"error": "missing required fields"}), 400
new_markdown = Markdown(title=title, content=content, path_id=path_id, shortcut=shortcut) new_markdown = Markdown(title=title, content=content, path_id=path_id, shortcut=shortcut, setting_id=setting_id)
with get_db() as session: with get_db() as session:
try: try:
if shortcut != "": if shortcut != "":
@@ -110,6 +111,7 @@ def update_markdown(markdown_id):
markdown.content = data.get('content') markdown.content = data.get('content')
markdown.path_id = data.get('path_id') markdown.path_id = data.get('path_id')
markdown.shortcut = data.get('shortcut', '') markdown.shortcut = data.get('shortcut', '')
markdown.setting_id = data.get('setting_id', None)
elif request.method == "PATCH": elif request.method == "PATCH":
if 'title' in data: if 'title' in data:
markdown.title = data.get('title') markdown.title = data.get('title')
@@ -119,6 +121,8 @@ def update_markdown(markdown_id):
markdown.path_id = data.get('path_id') markdown.path_id = data.get('path_id')
if 'shortcut' in data: if 'shortcut' in data:
markdown.shortcut = data.get('shortcut') markdown.shortcut = data.get('shortcut')
if 'setting_id' in data:
markdown.setting_id = data.get('setting_id')
session.commit() session.commit()
markdown_updated.send(None, payload=markdown.to_dict()) markdown_updated.send(None, payload=markdown.to_dict())
return jsonify(markdown.to_dict()), 200 return jsonify(markdown.to_dict()), 200

View File

@@ -45,7 +45,7 @@ def update_markdown_setting(setting_id):
setting.template_setting_id = template_setting_id setting.template_setting_id = template_setting_id
session.commit() session.commit()
return jsonify(setting.to_dict()), 200 return jsonify(setting.to_dict()), 200
except Exception: except Exception as e:
return jsonify({"error": "failed to update setting"}), 500 return jsonify({"error": "failed to update setting"}), 500

View File

@@ -32,6 +32,7 @@ def create_template_setting():
new_setting = MarkdownTemplateSetting(template_id=template_id) new_setting = MarkdownTemplateSetting(template_id=template_id)
with get_db() as session: with get_db() as session:
session.add(new_setting) session.add(new_setting)
session.commit()
return jsonify(new_setting.to_dict()), 201 return jsonify(new_setting.to_dict()), 201

View File

@@ -1,4 +1,3 @@
import json
from flask import jsonify, request from flask import jsonify, request
from api import etag_response, require_auth from api import etag_response, require_auth
from api.template import template_bp from api.template import template_bp
@@ -7,8 +6,7 @@ from db.models.MarkdownTemplate import MarkdownTemplate
cached_templates = {} cached_templates = {}
def inflate_template(template): def inflate_template(template):
template.parameters = json.loads(template.parameters) for parameter in template.get('parameters'):
for parameter in template.parameters:
if parameter.get('type', {}).get('base_type') == 'template': if parameter.get('type', {}).get('base_type') == 'template':
sub_template_id = parameter.get('type', {}).get('definition', {}).get('template', {}).get('id', 0) sub_template_id = parameter.get('type', {}).get('definition', {}).get('template', {}).get('id', 0)
if sub_template_id in cached_templates.keys(): if sub_template_id in cached_templates.keys():
@@ -17,7 +15,7 @@ def inflate_template(template):
with get_db() as session: with get_db() as session:
sub_template = session.query(MarkdownTemplate).get(sub_template_id) sub_template = session.query(MarkdownTemplate).get(sub_template_id)
parameter['type']['definition']['template'] = inflate_template(sub_template) parameter['type']['definition']['template'] = inflate_template(sub_template)
cached_templates[template.id] = template cached_templates[template['id']] = template
return template return template
@@ -28,6 +26,7 @@ def get_markdown_template(template_id):
template = session.query(MarkdownTemplate).get(template_id) template = session.query(MarkdownTemplate).get(template_id)
if template is None: if template is None:
return jsonify({}), 204 return jsonify({}), 204
print(inflate_template(template.to_dict()))
return jsonify(inflate_template(template.to_dict())), 200 return jsonify(inflate_template(template.to_dict())), 200
@template_bp.route('/markdown/', methods=['GET']) @template_bp.route('/markdown/', methods=['GET'])
@@ -52,7 +51,7 @@ def create_markdown_template():
with get_db() as session: with get_db() as session:
session.add(template) session.add(template)
session.commit() session.commit()
return template.to_dict(), 200 return jsonify(template.to_dict()), 200
except Exception as e: except Exception as e:
return jsonify({"error": "failed to create markdown template"}), 400 return jsonify({"error": "failed to create markdown template"}), 400

View File

@@ -1,9 +0,0 @@
from flask import jsonify
from api.template import template_bp
@template_bp.route('/markdown/type/', methods=['GET'])
def get_types():
return jsonify(["template", "text", "string", "list", "enum"]), 200

4
app.py
View File

@@ -50,9 +50,7 @@ def log_request():
logger.info(f"Request received: {request.method} {request.path} from {request.remote_addr}") logger.info(f"Request received: {request.method} {request.path} from {request.remote_addr}")
if __name__ == '__main__': if __name__ == '__main__':
api.init_rate_limits(app) api.init_rate_limits(app)
pprint(env_provider.summerize()) pprint(env_provider.summerize())
app.run(host='0.0.0.0', port=5000) app.run(host='0.0.0.0', port=5000, debug=True, use_reloader=True)

View File

@@ -7,7 +7,6 @@ from sqlalchemy.orm import sessionmaker
from sqlalchemy.dialects.mysql import insert from sqlalchemy.dialects.mysql import insert
from sqlalchemy import create_engine, text, inspect from sqlalchemy import create_engine, text, inspect
from env_provider import DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASSWORD, DB_SCHEMA_UPDATED, ENVIRONMENT from env_provider import DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASSWORD, DB_SCHEMA_UPDATED, ENVIRONMENT
print(f"mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}")
engine = create_engine(f"mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}") engine = create_engine(f"mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}")
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

View File

@@ -1,4 +1,3 @@
#db/models/Log.py
from sqlalchemy import Column, Integer, String, DateTime, Text from sqlalchemy import Column, Integer, String, DateTime, Text
from db.models import Base from db.models import Base
import datetime import datetime

View File

@@ -6,7 +6,7 @@ from db.models import Base
class MarkdownSetting(Base): class MarkdownSetting(Base):
__tablename__ = 'markdown_setting' __tablename__ = 'markdown_setting'
id = Column(Integer, primary_key=True, autoincrement=True) id = Column(Integer, primary_key=True, autoincrement=True)
template_setting_id = Column(Integer, ForeignKey('markdown_template.id'), nullable=True) template_setting_id = Column(Integer, ForeignKey('markdown_template_setting.id'), nullable=True)
def to_dict(self): def to_dict(self):
return { return {

View File

@@ -1,11 +1,11 @@
from sqlalchemy import Column, Text, Integer, String, UniqueConstraint from sqlalchemy import Column, Text, Integer, String, UniqueConstraint,JSON
from db.models import Base from db.models import Base
class MarkdownTemplate(Base): class MarkdownTemplate(Base):
__tablename__ = 'markdown_template' __tablename__ = 'markdown_template'
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
title = Column(String(255), nullable=False) title = Column(String(255), nullable=False)
parameters = Column(Text, nullable=True) parameters = Column(JSON, nullable=True)
layout = Column(Text, nullable=True) layout = Column(Text, nullable=True)
__table_args__ = (UniqueConstraint("title", name="unique_title"),) __table_args__ = (UniqueConstraint("title", name="unique_title"),)