57 lines
2.5 KiB
Python
57 lines
2.5 KiB
Python
|
|
from flask import Flask, Blueprint,render_template, send_from_directory,jsonify, url_for
|
|
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<int:size>/<path:name>/')
|
|
def thumb(size=64,name=''):
|
|
pass
|
|
|
|
@page_blueprint.route('/<path:name>/')
|
|
@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"])
|
|
|
|
if os.path.exists(os.path.join(app.config['FLATPAGES_ROOT'],name)):
|
|
return send_from_directory(app.config['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='pages.pagejson'
|
|
# page.links.file_url=lambda n: url_for('intern.post', name=n)
|
|
return jsonify(page=dict(page))
|
|
|
|
if os.path.exists(u'{}/{}'.format(app.config['FLATPAGES_ROOT'],path)):
|
|
return send_from_directory(app.config['FLATPAGES_ROOT'],path)
|
|
else:
|
|
return send_from_directory('static',path)
|
|
return page_blueprint
|
|
|
|
|