Commit 9eda3049a5bee5293fa0143dfd5b80dce3f3d4cf

  • avatar
  • arvind
  • Tue Jan 08 16:28:24 IST 2013
Making correct use of bootstrap
			 - Changed the delete button from image to a icon.
			 - Changed the url for glyphicons in bootstrap.css
flaskr.py
(0 / 110)
  
1# -*- coding: utf-8 -*-
2"""
3 swtr
4 ~~~~~~
5
6 http://swtr.us
7
8 :license: BSD, see LICENSE for more details.
9"""
10from __future__ import with_statement
11from pymongo import Connection
12from bson.objectid import ObjectId
13from bson.errors import InvalidId
14from flask import Flask, request, session, g, redirect, url_for, abort, \
15 render_template, flash, _app_ctx_stack
16
17# configuration
18DATABASE = 'alipiBlog'
19COLLECTION_NAME = 'posts'
20DEBUG = True
21SECRET_KEY = 'development key'
22USERNAME = 'admin'
23PASSWORD = 'default'
24DB_PORT = 27017
25DB_HOST = 'localhost'
26
27# create our little application :)
28app = Flask(__name__)
29app.config.from_object(__name__)
30app.config.from_envvar('FLASKR_SETTINGS', silent=True)
31
32
33@app.before_request
34def init_db():
35 g.connection = Connection(app.config['DB_HOST'], app.config['DB_PORT'])
36 db = g.connection[app.config['DATABASE']]
37 g.collection = db[app.config["COLLECTION_NAME"]]
38
39
40@app.teardown_request
41def close_db(exception):
42 g.connection.disconnect()
43
44
45@app.errorhandler(400)
46def page_not_found(e):
47 return render_template('404.html'), 404
48
49
50@app.route('/')
51def show_entries():
52 res = g.collection.find().sort('_id',direction=-1)
53 entries = make_list(res)
54 return render_template('show_entries.html', entries=entries)
55
56
57@app.route('/add', methods=['POST'])
58def add_entry():
59 if not session.get('logged_in'):
60 abort(401)
61 g.collection.insert({'title':request.form['title'], 'text':request.form['text']})
62 flash('New entry was successfully posted')
63 return redirect(url_for('show_entries'))
64
65
66@app.route('/login', methods=['GET', 'POST'])
67def login():
68 error = None
69 if request.method == 'POST':
70 if request.form['username'] != app.config['USERNAME']:
71 error = 'Invalid username'
72 elif request.form['password'] != app.config['PASSWORD']:
73 error = 'Invalid password'
74 else:
75 session['logged_in'] = True
76 flash('You were logged in')
77 return redirect(url_for('show_entries'))
78 return render_template('login.html', error=error)
79
80
81@app.route('/posts/<post_id>',methods=['GET'])
82def show_specific_entry(post_id):
83 try:
84 res = g.collection.find({'_id':ObjectId(post_id)});
85 if(res.count() > 0):
86 entries = make_list(res)
87 return render_template('show_posts.html', entries=entries)
88 else:
89 abort(400)
90 except InvalidId:
91 abort(400)
92
93
94@app.route('/logout')
95def logout():
96 session.pop('logged_in', None)
97 flash('You were logged out')
98 return redirect(url_for('show_entries'))
99
100def make_list(res):
101 entries = []
102 for row in res:
103 d = dict()
104 d['text'] = row['text']
105 d["title"] = row["title"]
106 entries.append(d)
107 return entries
108
109if __name__ == '__main__':
110 app.run()
  
220220table .span10{float:none;width:764px;margin-left:0;}
221221table .span11{float:none;width:844px;margin-left:0;}
222222table .span12{float:none;width:924px;margin-left:0;}
223[class^="icon-"],[class*=" icon-"]{display:inline-block;width:14px;height:14px;line-height:14px;vertical-align:text-top;background-image:url("../img/glyphicons-halflings.png");background-position:14px 14px;background-repeat:no-repeat;*margin-right:.3em;}[class^="icon-"]:last-child,[class*=" icon-"]:last-child{*margin-left:0;}
223[class^="icon-"],[class*=" icon-"]{display:inline-block;width:14px;height:14px;line-height:14px;vertical-align:text-top;background-image:url("img/glyphicons-halflings.png");background-position:14px 14px;background-repeat:no-repeat;*margin-right:.3em;}[class^="icon-"]:last-child,[class*=" icon-"]:last-child{*margin-left:0;}
224224.icon-white{background-image:url("../img/glyphicons-halflings-white.png");}
225225.icon-glass{background-position:0 0;}
226226.icon-music{background-position:-24px 0;}
Binary files differ
  
1212<body>
1313<ul class=entries>
1414 {% for entry in entries %}
15 <li><h2>{{ entry.title }}</h2>@<b>{{entry.user}}</b> <a href=""><img style="height:15px;" src="{{ url_for('static', filename='bin.png') }}" onclick=deletePost({{ entry.id|string|tojson|safe }});></img></a>
15 <li><h2>{{ entry.title }}</h2>@<b>{{entry.user}}</b> <a href=""><i class="icon-trash" onclick=deletePost({{ entry.id|string|tojson|safe }});></i></a>
1616 <p> {{ entry.text|safe }} </p>
1717 {% endfor %}
1818</ul>