Commit de07eb494c75073a958f09ad2ca8d6859395dc95
- Diff rendering mode:
- inline
- side by side
swtr.py
(18 / 11)
  | |||
12 | 12 | from bson.objectid import ObjectId | |
13 | 13 | from bson.errors import InvalidId | |
14 | 14 | from flask import Flask, request, session, g, redirect, url_for, abort, \ | |
15 | render_template, flash, _app_ctx_stack | ||
16 | |||
15 | render_template, flash, _app_ctx_stack, make_response, jsonify | ||
16 | from urllib import unquote_plus | ||
17 | import json | ||
17 | 18 | # configuration | |
18 | 19 | DATABASE = 'alipiBlog' | |
19 | 20 | COLLECTION_NAME = 'posts' | |
… | … | ||
24 | 24 | PASSWORD = 'default' | |
25 | 25 | DB_PORT = 27017 | |
26 | 26 | DB_HOST = 'localhost' | |
27 | |||
27 | URL = "http://localhost:5000" | ||
28 | 28 | # create our little application :) | |
29 | 29 | app = Flask(__name__) | |
30 | 30 | app.config.from_object(__name__) | |
… | … | ||
43 | 43 | g.connection.disconnect() | |
44 | 44 | ||
45 | 45 | ||
46 | @app.errorhandler(400) | ||
46 | @app.errorhandler(404) | ||
47 | 47 | def page_not_found(e): | |
48 | 48 | return render_template('404.html'), 404 | |
49 | 49 | ||
… | … | ||
61 | 61 | ||
62 | 62 | @app.route('/add', methods=['POST']) | |
63 | 63 | def add_entry(): | |
64 | if not session.get('logged_in'): | ||
65 | abort(401) | ||
66 | g.collection.insert({'user':request.form['user'],'title':request.form['title'], 'text':request.form['text']}) | ||
67 | flash('New entry was successfully posted') | ||
68 | return redirect(url_for('show_entries')) | ||
64 | response = make_response() | ||
65 | response.headers['Access-Control-Allow-Origin'] = '*' | ||
66 | data = {} | ||
67 | data_list = [] | ||
68 | escaped_form_data = json.loads(unquote_plus(request.form['data'])) | ||
69 | for i in escaped_form_data: | ||
70 | id = g.collection.insert({'user':i['user'],'title':i['title'], 'text':i['text'],'top':i['top'],'bottom':i['bottom'],'left':i['left'],'right':i['right']}) | ||
71 | data['permalink'] = app.config['URL']+'/posts/'+str(ObjectId(id)) | ||
72 | data_list.append(data) | ||
73 | response.data = json.dumps(data_list) | ||
74 | return response | ||
69 | 75 | ||
70 | 76 | ||
71 | 77 | @app.route('/login', methods=['GET', 'POST']) | |
… | … | ||
97 | 97 | entries = make_list(res) | |
98 | 98 | return render_template('show_posts.html', entries=entries, str=str) | |
99 | 99 | else: | |
100 | abort(400) | ||
100 | abort(404) | ||
101 | 101 | except InvalidId: | |
102 | abort(400) | ||
102 | abort(404) | ||
103 | 103 | ||
104 | 104 | ||
105 | 105 | @app.route('/posts/delete/', methods=['POST']) |