Commit 9eda3049a5bee5293fa0143dfd5b80dce3f3d4cf
- Diff rendering mode:
- inline
- side by side
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 | """ | ||
10 | from __future__ import with_statement | ||
11 | from pymongo import Connection | ||
12 | from bson.objectid import ObjectId | ||
13 | from bson.errors import InvalidId | ||
14 | from flask import Flask, request, session, g, redirect, url_for, abort, \ | ||
15 | render_template, flash, _app_ctx_stack | ||
16 | |||
17 | # configuration | ||
18 | DATABASE = 'alipiBlog' | ||
19 | COLLECTION_NAME = 'posts' | ||
20 | DEBUG = True | ||
21 | SECRET_KEY = 'development key' | ||
22 | USERNAME = 'admin' | ||
23 | PASSWORD = 'default' | ||
24 | DB_PORT = 27017 | ||
25 | DB_HOST = 'localhost' | ||
26 | |||
27 | # create our little application :) | ||
28 | app = Flask(__name__) | ||
29 | app.config.from_object(__name__) | ||
30 | app.config.from_envvar('FLASKR_SETTINGS', silent=True) | ||
31 | |||
32 | |||
33 | @app.before_request | ||
34 | def 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 | ||
41 | def close_db(exception): | ||
42 | g.connection.disconnect() | ||
43 | |||
44 | |||
45 | @app.errorhandler(400) | ||
46 | def page_not_found(e): | ||
47 | return render_template('404.html'), 404 | ||
48 | |||
49 | |||
50 | @app.route('/') | ||
51 | def 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']) | ||
58 | def 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']) | ||
67 | def 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']) | ||
82 | def 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') | ||
95 | def logout(): | ||
96 | session.pop('logged_in', None) | ||
97 | flash('You were logged out') | ||
98 | return redirect(url_for('show_entries')) | ||
99 | |||
100 | def 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 | |||
109 | if __name__ == '__main__': | ||
110 | app.run() |
static/bootstrap.min.css
(1 / 1)
  | |||
220 | 220 | table .span10{float:none;width:764px;margin-left:0;} | |
221 | 221 | table .span11{float:none;width:844px;margin-left:0;} | |
222 | 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;} | ||
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 | 224 | .icon-white{background-image:url("../img/glyphicons-halflings-white.png");} | |
225 | 225 | .icon-glass{background-position:0 0;} | |
226 | 226 | .icon-music{background-position:-24px 0;} |
Binary files differ
templates/show_posts.html
(1 / 1)
  | |||
12 | 12 | <body> | |
13 | 13 | <ul class=entries> | |
14 | 14 | {% 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> | ||
16 | 16 | <p> {{ entry.text|safe }} </p> | |
17 | 17 | {% endfor %} | |
18 | 18 | </ul> |