flask pages

This commit is contained in:
2021-11-19 21:07:45 +00:00
parent e48ea09a14
commit c04c101e53
2 changed files with 95 additions and 0 deletions

39
flaskpages/__init__.py Normal file
View File

@@ -0,0 +1,39 @@
import os
from flask import Flask
from flaskpages import pages
import logging
def create_app(test_config=None):
"""Create and configure an instance of the Flask application."""
app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
# a default secret that should be overridden by instance config
SECRET_KEY="dev",
URL_PREFIX = "/"
)
if not test_config:
# load the instance config, if it exists, when not testing
app.config.from_pyfile("cfg.py")
else:
# load the test config if passed in
app.config.update(test_config)
# ensure the instance folder exists
try:
os.makedirs(app.instance_path)
except OSError:
pass
if app.config["DEBUG"]:
app.logger.setLevel(logging.DEBUG)
@app.route("/hello")
def hello():
return "Hello, World!"
app.logger.debug(app.config)
app.register_blueprint(pages.create_bp(app), url_prefix = app.config["URL_PREFIX"], static_folder='static')
app.add_url_rule("/", endpoint="index")
return app

56
flaskpages/pages.py Normal file
View File

@@ -0,0 +1,56 @@
from flask import Flask, Blueprint,render_template, send_from_directory,jsonify, url_for
from flatpages_index import FlatPagesIndex
import flatpages_index
def create_bp(app):
flatpages = FlatPagesIndex(app)
flatpages_index.Links.endpoint="stuff.page"
flatpages_index.Links.url=(lambda s,x: url_for(s.endpoint, name=x))
#flatpages_index.Links.image_url=(lambda s,x: url_for('stuff.page', name=x))
flatpages_index.Links.file_url=(lambda s,x: url_for('stuff.page', name=x))
flatpages_index.Links.thumb_url=(lambda s,x: url_for('stuff.thumb', size=128,name=x))
flatpages.get('index')
app.logger.debug('flatpages loaded %d pages' % len(flatpages._pages))
app.logger.debug("Data directory is: %s" % flatpages.root)
page_blueprint = Blueprint('pages', __name__)
@page_blueprint.route('/thumb<int:size>/<path:name>/')
def thumb(size=64,name=''):
pass
@page_blueprint.route('/<path:name>/')
@page_blueprint.route('/')
def page(name='index'):
page = flatpages.get(name)
if page:
page["has_img"]=True
page.links.endpoint='stuff.page'
return render_template(page["template"], post=page,
pth=page["dirpath"])
if os.path.exists(os.path.join(FLATPAGES_ROOT,name)):
return send_from_directory(FLATPAGES_ROOT,name)
else:
return send_from_directory('static',name)
@page_blueprint.route('/<path:name>.json',strict_slashes=False)
def pagejson(name='index'):
page = flatpages.get(name)
if not page is None:
page["has_img"]=False
page.links.endpoint='stuff.pagejson'
# page.links.file_url=lambda n: url_for('intern.post', name=n)
return jsonify(page=dict(page))
if os.path.exists(u'{}/{}'.format(FLATPAGES_ROOT,path)):
return send_from_directory(FLATPAGES_ROOT,path)
else:
return send_from_directory('static',path)
return page_blueprint