Created
June 20, 2017 13:27
-
-
Save gouthambs/3b6bf45b18ebe33ab05e1f98e4de7ca3 to your computer and use it in GitHub Desktop.
Example of Flask-Blogging working with Flask-Admin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, render_template_string, redirect | |
from flask_sqlalchemy import SQLAlchemy | |
from flask_login import UserMixin, LoginManager, login_user, logout_user | |
from flask_blogging import SQLAStorage, BloggingEngine | |
from flask_admin import Admin | |
from flask_admin.contrib.sqla import ModelView | |
db = SQLAlchemy() | |
app = Flask(__name__) | |
app.config["SECRET_KEY"] = "secret" # for WTF-forms and login | |
app.config["BLOGGING_URL_PREFIX"] = "/blog" | |
app.config["BLOGGING_DISQUS_SITENAME"] = "test" | |
app.config["BLOGGING_SITEURL"] = "http://localhost:8000" | |
app.config["BLOGGING_SITENAME"] = "My Site" | |
app.config["FILEUPLOAD_IMG_FOLDER"] = "fileupload" | |
app.config["FILEUPLOAD_PREFIX"] = "/fileupload" | |
app.config["FILEUPLOAD_ALLOWED_EXTENSIONS"] = ["png", "jpg", "jpeg", "gif"] | |
app.config["SQLALCHEMY_DATABASE_URI"] = 'sqlite:////tmp/blog.db' | |
db.init_app(app) | |
with app.app_context(): | |
#db.create_all() | |
# extensions | |
sql_storage = SQLAStorage(db=db) | |
blog_engine = BloggingEngine(app, sql_storage) | |
login_manager = LoginManager(app) | |
admin = Admin(app, name="Flask-Blogging", template_mode='bootstrap3') | |
Post = sql_storage.post_model | |
Tag = sql_storage.tag_model | |
admin.add_view(ModelView(Post, db.session)) | |
admin.add_view(ModelView(Tag, db.session)) | |
class User(UserMixin): | |
def __init__(self, user_id): | |
self.id = user_id | |
def get_name(self): | |
return "Paul Dirac" # typically the user's name | |
@login_manager.user_loader | |
@blog_engine.user_loader | |
def load_user(user_id): | |
return User(user_id) | |
index_template = """ | |
<!DOCTYPE html> | |
<html> | |
<head> </head> | |
<body> | |
{% if current_user.is_authenticated %} | |
<a href="/logout/"> Logout </a> | |
{% else %} | |
<a href="/login/"> Login </a> | |
{% endif %} | |
  <a href="/blog/"> Blog </a> | |
  <a href="/blog/sitemap.xml">Sitemap</a> | |
  <a href="/blog/feeds/all.atom.xml">ATOM</a> | |
  <a href="/fileupload/">FileUpload</a> | |
</body> | |
</html> | |
""" | |
@app.route("/") | |
def index(): | |
return render_template_string(index_template) | |
@app.route("/login/") | |
def login(): | |
user = User("testuser") | |
login_user(user) | |
return redirect("/blog") | |
@app.route("/logout/") | |
def logout(): | |
logout_user() | |
return redirect("/") | |
if __name__ == "__main__": | |
app.run(debug=True, port=8000, use_reloader=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment