|
|
|
@ -1,9 +1,10 @@
|
|
|
|
|
import os
|
|
|
|
|
import flask
|
|
|
|
|
from flask import Flask, abort, request
|
|
|
|
|
from flask import Blueprint, Flask, abort, request
|
|
|
|
|
from functools import wraps
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from flask_login import current_user, login_required
|
|
|
|
|
from werkzeug.exceptions import NotFound
|
|
|
|
|
|
|
|
|
|
from .render_template import render_title_template
|
|
|
|
|
from . import logger, config, ub
|
|
|
|
@ -11,7 +12,7 @@ from .constants import CONFIG_DIR as _CONFIG_DIR
|
|
|
|
|
|
|
|
|
|
log = logger.create()
|
|
|
|
|
|
|
|
|
|
editpage = flask.Blueprint('editpage', __name__)
|
|
|
|
|
editpage = Blueprint('editpage', __name__)
|
|
|
|
|
|
|
|
|
|
def edit_required(f):
|
|
|
|
|
@wraps(f)
|
|
|
|
@ -42,13 +43,17 @@ def edit_page(file):
|
|
|
|
|
position = "0"
|
|
|
|
|
|
|
|
|
|
page = ub.session.query(ub.Page).filter(ub.Page.id == file).first()
|
|
|
|
|
if page:
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
title = page.title
|
|
|
|
|
name = page.name
|
|
|
|
|
icon = page.icon
|
|
|
|
|
is_enabled = page.is_enabled
|
|
|
|
|
order = page.order
|
|
|
|
|
position = page.position
|
|
|
|
|
except AttributeError:
|
|
|
|
|
if file != "new":
|
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
|
|
if request.method == "POST":
|
|
|
|
|
to_save = request.form.to_dict()
|
|
|
|
@ -56,8 +61,7 @@ def edit_page(file):
|
|
|
|
|
name = to_save.get("name", "").strip()
|
|
|
|
|
icon = to_save.get("icon", "").strip()
|
|
|
|
|
position = to_save.get("position", "").strip()
|
|
|
|
|
order = to_save.get("order", 0)
|
|
|
|
|
order = int(order)
|
|
|
|
|
order = int(to_save.get("order", 0))
|
|
|
|
|
content = to_save.get("content", "").strip()
|
|
|
|
|
is_enabled = _get_checkbox(to_save, "is_enabled", True)
|
|
|
|
|
|
|
|
|
@ -79,12 +83,8 @@ def edit_page(file):
|
|
|
|
|
dir_config_path = os.path.join(_CONFIG_DIR, 'pages')
|
|
|
|
|
file_name = Path(name + '.md')
|
|
|
|
|
file_path = dir_config_path / file_name
|
|
|
|
|
is_path = os.path.exists(dir_config_path)
|
|
|
|
|
if not is_path:
|
|
|
|
|
try:
|
|
|
|
|
os.makedirs(dir_config_path)
|
|
|
|
|
except Exception as ex:
|
|
|
|
|
log.error(ex)
|
|
|
|
|
os.makedirs(dir_config_path, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
with open(file_path, 'w') as f:
|
|
|
|
|
f.write(content)
|
|
|
|
@ -93,12 +93,15 @@ def edit_page(file):
|
|
|
|
|
log.error(ex)
|
|
|
|
|
|
|
|
|
|
if file != "new":
|
|
|
|
|
dir_config_path = os.path.join(_CONFIG_DIR, 'pages')
|
|
|
|
|
file_name = Path(name + '.md')
|
|
|
|
|
file_path = dir_config_path / file_name
|
|
|
|
|
if file_path.is_file():
|
|
|
|
|
try:
|
|
|
|
|
dir_config_path = Path(_CONFIG_DIR) / 'pages'
|
|
|
|
|
file_path = dir_config_path / f"{name}.md"
|
|
|
|
|
|
|
|
|
|
with open(file_path, 'r') as f:
|
|
|
|
|
doc = f.read()
|
|
|
|
|
except NotFound:
|
|
|
|
|
log.error("'%s' was accessed but file doesn't exists." % file)
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
doc = "## New file\n\nInformation"
|
|
|
|
|
|
|
|
|
|