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