Source

FlaskKit / flaskkit / views.py

# -*- encoding:utf-8 -*-
import markdown
from flask import render_template,session,abort,flash,redirect,url_for,jsonify,request

from flaskkit import app,connection
from flaskkit.models import Entry,Log

@app.route('/')
def show_entries():
    entries = connection.flaskkit.entries.find()
    return render_template('show_entries.html', entries=entries)

@app.route('/add', methods=['POST'])
def add_entry():
    if not session.get('logged_in'):
        abort(401)
    entry = connection.flaskkit.entries.Entry()
    entry.title = request.form['title']
    entry.body = request.form['body']
    entry.save()
    log = connection.logdb.logs.Log()
    log.text = u"added entry: %s "%entry.title
    log.save()
    flash('New entry was successfully posted')
    return redirect(url_for('show_entries'))

@app.route('/login', methods=['GET', 'POST'])
def login():
    error = None
    if request.method == 'POST':
        if request.form['username'] != app.config['USERNAME']:
            error = 'Invalid username'
        elif request.form['password'] != app.config['PASSWORD']:
            error = 'Invalid password'
        else:
            session['logged_in'] = True
            flash('You have been logged in')
            return redirect(url_for('show_entries'))
    return render_template('login.html', error=error)

@app.route('/logout')
def logout():
    session.pop('logged_in', None)
    flash('You have been logged out')
    return redirect(url_for('show_entries'))


@app.template_filter('markdown')
def to_markdown(value):
    """Converts a string into valid Markdown."""
    return markdown.markdown(value)