diff --git a/flaskpages/__init__.py b/flaskpages/__init__.py new file mode 100644 index 0000000..7a7ebe2 --- /dev/null +++ b/flaskpages/__init__.py @@ -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 \ No newline at end of file diff --git a/flaskpages/pages.py b/flaskpages/pages.py new file mode 100644 index 0000000..befdf8f --- /dev/null +++ b/flaskpages/pages.py @@ -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//') + def thumb(size=64,name=''): + pass + + @page_blueprint.route('//') + @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('/.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 \ No newline at end of file