from flask import Flask, Blueprint,render_template, send_from_directory,jsonify, url_for, abort from flatpages_index import FlatPagesIndex import flatpages_index import os def create_bp(app): """Create Blueprint to register with Flask""" flatpages = FlatPagesIndex(app) # Init with the Flask App as argument flatpages_index.Links.endpoint="pages.page" #set the name of the endpoint for Links to pages flatpages_index.Links.url=(lambda s,x: url_for(s.endpoint, name=x)) # shorthand expects a function that creates urls for pages #flatpages_index.Links.image_url=(lambda s,x: url_for('stuff.page', name=x)) flatpages_index.Links.file_url=(lambda s,x: url_for('pages.page', name=x)) # shorthand to create a link to a file flatpages_index.Links.thumb_url=(lambda s,x: url_for('pages.thumb', size=128,name=x)) # shorthand for thumbnails that may be used with pictures flatpages.get('index') # this ensures, that the pages are loaded 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//') def thumb(size=64,name=''): pass @page_blueprint.route('//') @page_blueprint.route('/') def page(name='index'): """This is the main endpoint of the application for all flatpages""" page = flatpages.get(name) if page: page["has_img"]=True page.links.endpoint='pages.page' return render_template(page["template"], post=page, pth=page["dirpath"]) app.logger.debug(f"delivering static file: {name}") # search different loctions for static file paths = [ app.config['FLATPAGES_ROOT'], os.path.abspath(app.config['STATIC_ROOT']) ] for p in paths: if os.path.exists(os.path.join(p,name)): return send_from_directory(p,name) app.logger.debug(f"Static file {name} not found in : {paths}") return abort(404) @page_blueprint.route('/.json',strict_slashes=False) def pagejson(name='index'): page = flatpages.get(name) if not page is None: page["has_img"]=False page.links.endpoint='pages.pagejson' # page.links.file_url=lambda n: url_for('intern.post', name=n) return jsonify(page=dict(page)) # search different loctions for static file paths = [ app.config['FLATPAGES_ROOT'], os.path.abspath(app.config['STATIC_ROOT']) ] for p in paths: if os.path.exists(os.path.join(p,name)): return send_from_directory(p,name) app.logger.debug(f"Static file {name} not found in : {paths}") return abort(404) return page_blueprint