Exemplo n.º 1
0
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))
Exemplo n.º 2
0
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()