def page(page): if page < 2: return flask.redirect(flask.url_for('index'), 301) else: total_pages = Post.total_pages() if page > total_pages: return flask.redirect(flask.url_for('index'), 301) else: posts = Post.posts_by_page(page) return flask.render_template('index.html', posts = posts, page = page, total_pages = total_pages, nextPageUrl = flask.url_for('page', page = page + 1), previousPageUrl = flask.url_for('page', page = page - 1))
def postSave(): app.open_session(flask.request) post = Post() post.init_from_dict(flask.request.form) import re post.body = re.sub(r'(<!--.*?-->|<[^>]*>)', '', post.body) post.ip = flask.request.remote_addr try: post.save() return flask.redirect(flask.url_for('read', slug = post.slug)) except (mongoengine.ValidationError) as validationError: flask.flash(validationError, category = "error") return flask.render_template('post/new.html', post = post, messages = flask.get_flashed_messages(with_categories = True)) except (mongoengine.OperationError) as operationError: flask.flash("A post with this title already exists.", category = "error") return flask.render_template('post/new.html', post = post, messages = flask.get_flashed_messages(with_categories = True)) except Exception as uh_oh: flask.flash("Uh oh. An unexpected error has occurred.", category = "error") logging.error(uh_oh) return flask.render_template('post/new.html', post = post, messages = flask.get_flashed_messages(with_categories = True))
def read(slug): post = Post.objects(slug = slug)[0] return flask.render_template('post.html', post = post)
def search(): #this is just a stub page = 1 post = Post.objects[0] total_pages = Post.total_pages() return flask.render_template('index.html', post = post, page = page, nextPageUrl = flask.url_for('page', page = page + 1), previousPageUrl = flask.url_for('page', page = page - 1))
def before_request(): """Setup some common daos so we can use them across the board""" flask.g.connect = mongoengine.connect('blogizzle') flask.g.now = datetime.datetime.now() flask.g.post_count = len(Post.objects())
from blogizzle.Post import Post from mongoengine import connect connect('blogizzle', host='Hippo.local') post = Post(title="Hello World!", body="Blah blah blah", ip="127.0.0.1") try: post.save() except: post._id = Post.objects(slug=post.slug).first()._id post.save()