def removepost(id=None): if Post.objects().count() > 1: post = Post.objects(id=id) post.delete() print session.get("page") flash("Remove success..",'success') return redirect(url_for("main.postlist", page=session.get("page"))) else: flash("Need to be at least one post..", 'error')
def tags(tag = None): posts = Post.objects(tag=tag) tags = Post._get_collection().aggregate([ {'$group':{'_id':'$tag','count':{'$sum':1}}}, {'$sort':{'count': -1}}, {'$limit': 10} ]) tags = tags["result"] return render_template("tags.html", title = "Tags | "+ BLOG_TITLE, posts = posts, tags = tags)
def newpost(): if request.method == "POST": posttitle = request.form.get("title") posttag = request.form.get("tag") posthtml = request.form.get("content") author = User.objects(email=g.user).first() if request.form.get("post-id"): post = Post.objects(id=request.form.get("post-id")).first() post.title = posttitle post.tag = posttag post.content = posthtml post.author = author post.created_at = datetime.datetime.now post.save() flash("Update success..","success") else: post = Post(title=posttitle, content = posthtml, tag=posttag, author = author) post.save() flash("Add success..","success") return render_template("newpost.html", title = "New Post | "+BLOG_TITLE)
def editpost(id=None): post = Post.objects(id=id).first() return render_template("editpost.html", title="Editpost | "+BLOG_TITLE, post=post)
from flask import Flask from flask.ext.mongoengine import MongoEngine from . import config app = Flask(__name__) app.config.from_object(config) db = MongoEngine(app) from helper_functions import generate_csrf_token from pytype.models import Post from pytype.models import User from .main import main as main_blueprint from .auth import auth as auth_blueprint app.register_blueprint(main_blueprint) app.register_blueprint(auth_blueprint, url_prefix="/auth") app.jinja_env.globals['csrf_token'] = generate_csrf_token app.jinja_env.globals['tags'] = Post._get_collection().aggregate([{'$group':{'_id':'$tag','count':{'$sum':1}}},{'$sort':{'count': -1}},{'$limit': 10}])['result'] app.jinja_env.globals['recent_posts'] = Post.objects[:5] app.jinja_env.globals['about_me'] = User.objects.get(email="*****@*****.**")