Review comments. - Sponsor: Fundación Karisma

pull/2898/head
Byron H. 8 months ago
parent e83c1ce860
commit b3d913d254

@ -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"

@ -1,6 +1,6 @@
import flask
import json
from flask import make_response,abort
from flask import Blueprint, jsonify, make_response,abort
from flask_login import current_user, login_required
from functools import wraps
from flask_babel import gettext as _
@ -8,7 +8,7 @@ from flask_babel import gettext as _
from .render_template import render_title_template
from . import ub, db
listpages = flask.Blueprint('listpages', __name__)
listpages = Blueprint('listpages', __name__)
def edit_required(f):
@wraps(f)
@ -26,15 +26,3 @@ def show_list():
pages = ub.session.query(ub.Page).order_by(ub.Page.position).order_by(ub.Page.order).all()
return render_title_template('list_pages.html', title=_("Pages List"), page="book_table", pages=pages)
@listpages.route("/ajax/listpages")
@login_required
@edit_required
def list_pages():
pages = ub.session.query(ub.Page).order_by(ub.Page.position).order_by(ub.Page.order).all()
table_entries = {'totalNotFiltered': len(pages), 'total': len(pages), "rows": pages}
js_list = json.dumps(table_entries, cls=db.AlchemyEncoder)
response = make_response(js_list)
response.headers["Content-Type"] = "application/json; charset=utf-8"
return response

@ -4,6 +4,7 @@ import markdown
from flask import abort
from pathlib import Path
from flask_babel import gettext as _
from werkzeug.exceptions import NotFound
from . import logger, config, ub
from .render_template import render_title_template
@ -20,20 +21,18 @@ def get_page(file):
.filter(ub.Page.is_enabled)\
.first()
if page:
dir_config_path = os.path.join(_CONFIG_DIR, 'pages')
file_name = Path(file + '.md')
file_path = dir_config_path / file_name
if not page:
log.error(f"'{file}' was accessed but is not enabled or it's not in database.")
abort(404)
if file_path.is_file():
with open(file_path, 'r') as f:
temp_md = f.read()
body = markdown.markdown(temp_md)
try:
dir_config_path = Path(_CONFIG_DIR) / 'pages'
file_path = dir_config_path / f"{file}.md"
with open(file_path, 'r') as f:
temp_md = f.read()
body = markdown.markdown(temp_md)
return render_title_template('page.html', body=body, title=page.title, page=page.name)
else:
log.error("'%s' was accessed but file doesn't exists." % file)
abort(404)
else:
log.error("'%s' was accessed but is not enabled or it's not in database." % file)
return render_title_template('page.html', body=body, title=page.title, page=page.name)
except NotFound:
log.error("'%s' was accessed but file doesn't exists." % file)
abort(404)

@ -36,10 +36,10 @@
</div>
<div class="form-group">
<label for="order">{{_('Order')}}</label>
<input type="text" class="form-control" name="order" id="order" value="{{ order }}" required>
<input type="number" class="form-control" name="order" id="order" value="{{ order }}" required>
</div>
<button type="submit" name="submit" id="page_submit" class="btn btn-default">{{_('Save')}}</button>
<a href="{{ url_for('admin.admin') }}" id="config_back" class="btn btn-default">{{_('Cancel')}}</a>
</form>
</div>
{% endblock %}
{% endblock %}

@ -7,30 +7,34 @@
{% block body %}
<h2 class="{{page}}">{{_(title)}}</h2>
<table class="table table-striped" id="table_user">
<tr>
<th>{{_('Name')}}</th>
<th>{{_('Title')}}</th>
<th>{{_('Icon')}}</th>
<th>{{_('Position')}}</th>
<th>{{_('Enabled')}}</th>
<th>{{_('Order')}}</th>
</tr>
<thead>
<tr>
<th>{{_('Name')}}</th>
<th>{{_('Title')}}</th>
<th>{{_('Icon')}}</th>
<th>{{_('Position')}}</th>
<th>{{_('Enabled')}}</th>
<th>{{_('Order')}}</th>
</tr>
</thead>
<tbody>
{% for page in pages %}
<tr>
<td><a class="session" href="{{url_for('editpage.edit_page', file=page.id)}}">{{page.name}}</a></td>
<td>{{page.title}}</td>
<td>{{page.icon}}</td>
<td>{{_('bottom') if page.position == "0" else _('top')}}</td>
<td>
{% if page.is_enabled %}
<tr>
<td><a class="session" href="{{url_for('editpage.edit_page', file=page.id)}}">{{page.name}}</a></td>
<td>{{page.title}}</td>
<td>{{page.icon}}</td>
<td>{{_('bottom') if page.position == "0" else _('top')}}</td>
<td>
{% if page.is_enabled %}
<span class="glyphicon glyphicon-ok"></span>
{% else %}
{% else %}
<span class="glyphicon glyphicon-remove"></span>
{% endif %}
</td>
<td>{{page.order}}</td>
</tr>
{% endif %}
</td>
<td>{{page.order}}</td>
</tr>
{% endfor %}
</tbody>
</table>
<a class="session" href="{{url_for('editpage.edit_page', file="new")}}">{{_('New Page')}}</a>
{% endblock %}
@ -45,4 +49,4 @@
charset="UTF-8"></script>
{% endif %}
<script src="{{ url_for('static', filename='js/table.js') }}"></script>
{% endblock %}
{% endblock %}

@ -1,4 +1,4 @@
{% extends "layout.html" %}
{% block body %}
<div>{{body|safe}}</div>
{% endblock %}
{% endblock %}

Loading…
Cancel
Save