Commit af3c1ff7da92f26dd57e9bc44b126b1bd370d3eb
- Diff rendering mode:
- inline
- side by side
swtr.py
(112 / 0)
  | |||
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({'user':request.form['user'],'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['id'] = str(row['_id']) | ||
105 | d['text'] = row['text'] | ||
106 | d["title"] = row["title"] | ||
107 | d["user"] = row["user"] | ||
108 | entries.append(d) | ||
109 | return entries | ||
110 | |||
111 | if __name__ == '__main__': | ||
112 | app.run() |
templates/layout.html
(2 / 2)
  | |||
1 | 1 | <!doctype html> | |
2 | <title>Flaskr</title> | ||
2 | <title>Swtr</title> | ||
3 | 3 | <link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}"> | |
4 | 4 | <div class=page> | |
5 | <h1>Flaskr</h1> | ||
5 | <h1>Swtrr</h1> | ||
6 | 6 | <div class=metanav> | |
7 | 7 | {% if not session.logged_in %} | |
8 | 8 | <a href="{{ url_for('login') }}">log in</a> |
templates/show_entries.html
(4 / 1)
  | |||
3 | 3 | {% if session.logged_in %} | |
4 | 4 | <form action="{{ url_for('add_entry') }}" method=post class=add-entry> | |
5 | 5 | <dl> | |
6 | <dt>Name: | ||
7 | <dd><input type=text name=user> | ||
6 | 8 | <dt>Title: | |
7 | 9 | <dd><input type=text size=30 name=title> | |
8 | 10 | <dt>Text: | |
… | … | ||
15 | 15 | {% endif %} | |
16 | 16 | <ul class=entries> | |
17 | 17 | {% for entry in entries %} | |
18 | <li><h2>{{ entry.title }}</h2>{{ entry.text|safe }} | ||
18 | <li><a href={{ "/posts/"+entry.id }}><h2>{{ entry.title }}</h2></a>@<b>{{ entry.user }}</b> | ||
19 | {{ entry.text|safe }} | ||
19 | 20 | {% else %} | |
20 | 21 | <li><em>Unbelievable. No entries here so far</em> | |
21 | 22 | {% endfor %} |
templates/show_posts.html
(2 / 1)
  | |||
2 | 2 | <body> | |
3 | 3 | <ul class=entries> | |
4 | 4 | {% for entry in entries %} | |
5 | <li><h2>{{ entry.title }}</h2>{{ entry.text|safe }} | ||
5 | <li><h2>{{ entry.title }}</h2>@<b>{{entry.user}}</b> | ||
6 | {{ entry.text|safe }} | ||
6 | 7 | {% endfor %} | |
7 | 8 | </ul> | |
8 | 9 | </body> |