askrobots / dbbasic-content
Public
WordPress-style content management with block-based pages, stored as JSON files on disk instead of MySQL.
flask_integration.py · 1.4 KB · 60 lines
| 1 | """ |
| 2 | Flask integration example for dbbasic-content. |
| 3 | |
| 4 | Shows how to build a simple blog with Flask. |
| 5 | """ |
| 6 | |
| 7 | from flask import Flask, render_template, abort |
| 8 | from dbbasic_content import ContentDB |
| 9 | |
| 10 | app = Flask(__name__) |
| 11 | content = ContentDB('/var/app/content') |
| 12 | |
| 13 | |
| 14 | @app.route('/') |
| 15 | def index(): |
| 16 | """Homepage - list recent posts.""" |
| 17 | posts = content.get_posts(status='published', limit=10) |
| 18 | return render_template('index.html', posts=posts) |
| 19 | |
| 20 | |
| 21 | @app.route('/<slug>/') |
| 22 | def post(slug): |
| 23 | """Individual post page.""" |
| 24 | post = content.get_post(slug) |
| 25 | |
| 26 | if not post: |
| 27 | abort(404) |
| 28 | |
| 29 | return render_template('post.html', post=post) |
| 30 | |
| 31 | |
| 32 | @app.route('/category/<category>/') |
| 33 | def category(category): |
| 34 | """Posts by category.""" |
| 35 | posts = content.get_posts(categories=[category]) |
| 36 | return render_template('category.html', category=category, posts=posts) |
| 37 | |
| 38 | |
| 39 | @app.route('/tag/<tag>/') |
| 40 | def tag(tag): |
| 41 | """Posts by tag.""" |
| 42 | posts = content.get_posts(tags=[tag]) |
| 43 | return render_template('tag.html', tag=tag, posts=posts) |
| 44 | |
| 45 | |
| 46 | @app.route('/search/') |
| 47 | def search(): |
| 48 | """Search posts.""" |
| 49 | from flask import request |
| 50 | query = request.args.get('q', '') |
| 51 | |
| 52 | if not query: |
| 53 | return render_template('search.html', results=[], query='') |
| 54 | |
| 55 | results = content.search(query) |
| 56 | return render_template('search.html', results=results, query=query) |
| 57 | |
| 58 | |
| 59 | if __name__ == '__main__': |
| 60 | app.run(debug=True) |