From 0186a95dd426c483e5644e2869c0c39989e6b88f Mon Sep 17 00:00:00 2001 From: hzhang Date: Thu, 17 Apr 2025 21:44:45 +0100 Subject: [PATCH] fix: template defects --- api/markdown.py | 6 +++++- api/setting/markdown/__init__.py | 2 +- api/setting/markdown/template.py | 1 + api/template/markdown/__init__.py | 9 ++++----- api/template/markdown/type.py | 9 --------- app.py | 4 +--- db/__init__.py | 1 - db/models/Log.py | 1 - db/models/MarkdownSetting.py | 2 +- db/models/MarkdownTemplate.py | 4 ++-- 10 files changed, 15 insertions(+), 24 deletions(-) delete mode 100644 api/template/markdown/type.py diff --git a/api/markdown.py b/api/markdown.py index 399f5e9..9d189db 100644 --- a/api/markdown.py +++ b/api/markdown.py @@ -69,9 +69,10 @@ def create_markdown(): content = data.get('content') path_id = data.get('path_id') shortcut = data.get('shortcut', "") + setting_id = data.get('setting_id', None) if not title or not content: 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: try: if shortcut != "": @@ -110,6 +111,7 @@ def update_markdown(markdown_id): markdown.content = data.get('content') markdown.path_id = data.get('path_id') markdown.shortcut = data.get('shortcut', '') + markdown.setting_id = data.get('setting_id', None) elif request.method == "PATCH": if 'title' in data: markdown.title = data.get('title') @@ -119,6 +121,8 @@ def update_markdown(markdown_id): markdown.path_id = data.get('path_id') if 'shortcut' in data: markdown.shortcut = data.get('shortcut') + if 'setting_id' in data: + markdown.setting_id = data.get('setting_id') session.commit() markdown_updated.send(None, payload=markdown.to_dict()) return jsonify(markdown.to_dict()), 200 diff --git a/api/setting/markdown/__init__.py b/api/setting/markdown/__init__.py index 5322001..5b3b97e 100644 --- a/api/setting/markdown/__init__.py +++ b/api/setting/markdown/__init__.py @@ -45,7 +45,7 @@ def update_markdown_setting(setting_id): setting.template_setting_id = template_setting_id session.commit() return jsonify(setting.to_dict()), 200 - except Exception: + except Exception as e: return jsonify({"error": "failed to update setting"}), 500 diff --git a/api/setting/markdown/template.py b/api/setting/markdown/template.py index 343e4ce..8719b1b 100644 --- a/api/setting/markdown/template.py +++ b/api/setting/markdown/template.py @@ -32,6 +32,7 @@ def create_template_setting(): new_setting = MarkdownTemplateSetting(template_id=template_id) with get_db() as session: session.add(new_setting) + session.commit() return jsonify(new_setting.to_dict()), 201 diff --git a/api/template/markdown/__init__.py b/api/template/markdown/__init__.py index ab69a04..374ff77 100644 --- a/api/template/markdown/__init__.py +++ b/api/template/markdown/__init__.py @@ -1,4 +1,3 @@ -import json from flask import jsonify, request from api import etag_response, require_auth from api.template import template_bp @@ -7,8 +6,7 @@ from db.models.MarkdownTemplate import MarkdownTemplate cached_templates = {} def inflate_template(template): - template.parameters = json.loads(template.parameters) - for parameter in template.parameters: + for parameter in template.get('parameters'): if parameter.get('type', {}).get('base_type') == 'template': sub_template_id = parameter.get('type', {}).get('definition', {}).get('template', {}).get('id', 0) if sub_template_id in cached_templates.keys(): @@ -17,7 +15,7 @@ def inflate_template(template): with get_db() as session: sub_template = session.query(MarkdownTemplate).get(sub_template_id) parameter['type']['definition']['template'] = inflate_template(sub_template) - cached_templates[template.id] = template + cached_templates[template['id']] = template return template @@ -28,6 +26,7 @@ def get_markdown_template(template_id): template = session.query(MarkdownTemplate).get(template_id) if template is None: return jsonify({}), 204 + print(inflate_template(template.to_dict())) return jsonify(inflate_template(template.to_dict())), 200 @template_bp.route('/markdown/', methods=['GET']) @@ -52,7 +51,7 @@ def create_markdown_template(): with get_db() as session: session.add(template) session.commit() - return template.to_dict(), 200 + return jsonify(template.to_dict()), 200 except Exception as e: return jsonify({"error": "failed to create markdown template"}), 400 diff --git a/api/template/markdown/type.py b/api/template/markdown/type.py deleted file mode 100644 index 8d63e74..0000000 --- a/api/template/markdown/type.py +++ /dev/null @@ -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 - diff --git a/app.py b/app.py index 5ba9097..f199180 100644 --- a/app.py +++ b/app.py @@ -50,9 +50,7 @@ def log_request(): logger.info(f"Request received: {request.method} {request.path} from {request.remote_addr}") - - if __name__ == '__main__': api.init_rate_limits(app) 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) diff --git a/db/__init__.py b/db/__init__.py index 9c329ea..013b5c1 100644 --- a/db/__init__.py +++ b/db/__init__.py @@ -7,7 +7,6 @@ from sqlalchemy.orm import sessionmaker from sqlalchemy.dialects.mysql import insert 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 -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}") SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) diff --git a/db/models/Log.py b/db/models/Log.py index ea79a89..104ff15 100644 --- a/db/models/Log.py +++ b/db/models/Log.py @@ -1,4 +1,3 @@ -#db/models/Log.py from sqlalchemy import Column, Integer, String, DateTime, Text from db.models import Base import datetime diff --git a/db/models/MarkdownSetting.py b/db/models/MarkdownSetting.py index 31614d0..8517514 100644 --- a/db/models/MarkdownSetting.py +++ b/db/models/MarkdownSetting.py @@ -6,7 +6,7 @@ from db.models import Base class MarkdownSetting(Base): __tablename__ = 'markdown_setting' 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): return { diff --git a/db/models/MarkdownTemplate.py b/db/models/MarkdownTemplate.py index 036b0d7..f15feb1 100644 --- a/db/models/MarkdownTemplate.py +++ b/db/models/MarkdownTemplate.py @@ -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 class MarkdownTemplate(Base): __tablename__ = 'markdown_template' id = Column(Integer, primary_key=True) title = Column(String(255), nullable=False) - parameters = Column(Text, nullable=True) + parameters = Column(JSON, nullable=True) layout = Column(Text, nullable=True) __table_args__ = (UniqueConstraint("title", name="unique_title"),)