From 31f43d59ad462a9281fad3be299bf9153cac9457 Mon Sep 17 00:00:00 2001 From: Arvind Date: Mon, 7 Jan 2013 17:19:00 +0530 Subject: [PATCH] Initial commit --- flaskr.py | 110 +++++++++++++++++++++++++++++++++++++++++++ schema.sql | 6 +++ static/style.css | 18 +++++++ templates/404.html | 4 ++ templates/layout.html | 17 +++++++ templates/login.html | 14 ++++++ templates/show_entries.html | 21 +++++++++ templates/show_posts.html | 8 ++++ 8 files changed, 198 insertions(+) create mode 100644 flaskr.py create mode 100644 schema.sql create mode 100644 static/style.css create mode 100644 templates/404.html create mode 100644 templates/layout.html create mode 100644 templates/login.html create mode 100644 templates/show_entries.html create mode 100644 templates/show_posts.html diff --git a/flaskr.py b/flaskr.py new file mode 100644 index 0000000..2881a06 --- /dev/null +++ b/flaskr.py @@ -0,0 +1,110 @@ +# -*- coding: utf-8 -*- +""" + swtr + ~~~~~~ + + http://swtr.us + + :license: BSD, see LICENSE for more details. +""" +from __future__ import with_statement +from pymongo import Connection +from bson.objectid import ObjectId +from bson.errors import InvalidId +from flask import Flask, request, session, g, redirect, url_for, abort, \ + render_template, flash, _app_ctx_stack + +# configuration +DATABASE = 'alipiBlog' +COLLECTION_NAME = 'posts' +DEBUG = True +SECRET_KEY = 'development key' +USERNAME = 'admin' +PASSWORD = 'default' +DB_PORT = 27017 +DB_HOST = 'localhost' + +# create our little application :) +app = Flask(__name__) +app.config.from_object(__name__) +app.config.from_envvar('FLASKR_SETTINGS', silent=True) + + +@app.before_request +def init_db(): + g.connection = Connection(app.config['DB_HOST'], app.config['DB_PORT']) + db = g.connection[app.config['DATABASE']] + g.collection = db[app.config["COLLECTION_NAME"]] + + +@app.teardown_request +def close_db(exception): + g.connection.disconnect() + + +@app.errorhandler(400) +def page_not_found(e): + return render_template('404.html'), 404 + + +@app.route('/') +def show_entries(): + res = g.collection.find().sort('_id',direction=-1) + entries = make_list(res) + return render_template('show_entries.html', entries=entries) + + +@app.route('/add', methods=['POST']) +def add_entry(): + if not session.get('logged_in'): + abort(401) + g.collection.insert({'title':request.form['title'], 'text':request.form['text']}) + 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 were logged in') + return redirect(url_for('show_entries')) + return render_template('login.html', error=error) + + +@app.route('/posts/',methods=['GET']) +def show_specific_entry(post_id): + try: + res = g.collection.find({'_id':ObjectId(post_id)}); + if(res.count() > 0): + entries = make_list(res) + return render_template('show_posts.html', entries=entries) + else: + abort(400) + except InvalidId: + abort(400) + + +@app.route('/logout') +def logout(): + session.pop('logged_in', None) + flash('You were logged out') + return redirect(url_for('show_entries')) + +def make_list(res): + entries = [] + for row in res: + d = dict() + d['text'] = row['text'] + d["title"] = row["title"] + entries.append(d) + return entries + +if __name__ == '__main__': + app.run() diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000..dbb0631 --- /dev/null +++ b/schema.sql @@ -0,0 +1,6 @@ +drop table if exists entries; +create table entries ( + id integer primary key autoincrement, + title text not null, + text text not null +); diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..4f3b71d --- /dev/null +++ b/static/style.css @@ -0,0 +1,18 @@ +body { font-family: sans-serif; background: #eee; } +a, h1, h2 { color: #377BA8; } +h1, h2 { font-family: 'Georgia', serif; margin: 0; } +h1 { border-bottom: 2px solid #eee; } +h2 { font-size: 1.2em; } + +.page { margin: 2em auto; width: 35em; border: 5px solid #ccc; + padding: 0.8em; background: white; } +.entries { list-style: none; margin: 0; padding: 0; } +.entries li { margin: 0.8em 1.2em; } +.entries li h2 { margin-left: -1em; } +.add-entry { font-size: 0.9em; border-bottom: 1px solid #ccc; } +.add-entry dl { font-weight: bold; } +.metanav { text-align: right; font-size: 0.8em; padding: 0.3em; + margin-bottom: 1em; background: #fafafa; } +.flash { background: #CEE5F5; padding: 0.5em; + border: 1px solid #AACBE2; } +.error { background: #F0D6D6; padding: 0.5em; } diff --git a/templates/404.html b/templates/404.html new file mode 100644 index 0000000..80b3b09 --- /dev/null +++ b/templates/404.html @@ -0,0 +1,4 @@ + + +Bummer. The page you were looking for is not found. + diff --git a/templates/layout.html b/templates/layout.html new file mode 100644 index 0000000..cbdb965 --- /dev/null +++ b/templates/layout.html @@ -0,0 +1,17 @@ + +Flaskr + +
+

Flaskr

+
+ {% if not session.logged_in %} + log in + {% else %} + log out + {% endif %} +
+ {% for message in get_flashed_messages() %} +
{{ message }}
+ {% endfor %} + {% block body %}{% endblock %} +
diff --git a/templates/login.html b/templates/login.html new file mode 100644 index 0000000..6f70bb7 --- /dev/null +++ b/templates/login.html @@ -0,0 +1,14 @@ +{% extends "layout.html" %} +{% block body %} +

Login

+ {% if error %}

Error: {{ error }}{% endif %} +

+
+
Username: +
+
Password: +
+
+
+
+{% endblock %} diff --git a/templates/show_entries.html b/templates/show_entries.html new file mode 100644 index 0000000..fabe65e --- /dev/null +++ b/templates/show_entries.html @@ -0,0 +1,21 @@ +{% extends "layout.html" %} +{% block body %} + {% if session.logged_in %} +
+
+
Title: +
+
Text: +
+
+
+
+ {% endif %} + +{% endblock %} diff --git a/templates/show_posts.html b/templates/show_posts.html new file mode 100644 index 0000000..cd3b326 --- /dev/null +++ b/templates/show_posts.html @@ -0,0 +1,8 @@ + + + + -- 1.7.10.4