-
Notifications
You must be signed in to change notification settings - Fork 0
/
ss.py
60 lines (44 loc) · 1.63 KB
/
ss.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
from flask import Flask, request, render_template, redirect, url_for, g
from flask_pymongo import PyMongo
import flask_pymongo
from flask_babel import Babel
from storyzer import storyze, format_story
from bson.objectid import ObjectId
app = Flask(__name__)
app.debug = True
babel = Babel(app)
app.config['MONGO_URI'] = os.environ.get('MONGOLAB_URI')
app.config['GSE_API_KEY'] = os.environ.get('GSE_API_KEY')
app.config['GSE_CX_ID'] = os.environ.get('GSE_CX_ID')
mongo = PyMongo(app)
@app.before_request
def before():
if request.view_args and 'lang_code' in request.view_args:
g.current_lang = request.view_args['lang_code']
request.view_args.pop('lang_code')
@babel.localeselector
def get_locale():
return g.get('current_lang', 'en')
@app.route('/')
def root():
return redirect(url_for('home', lang_code='pt_br'))
@app.route('/<lang_code>', methods=['GET', 'POST'])
def home():
if request.method == 'POST':
# TODO: sanitize lines
st = storyze(request.form['title'], request.form['text'], request.form['author'])
story_id = mongo.db.stories.insert(st)
return redirect(get_locale() + '/story/' + str(story_id))
stories = mongo.db.stories.find().sort("_id", flask_pymongo.DESCENDING)[:15]
return render_template('home.html', stories=stories)
@app.route('/<lang_code>/story/<story_id>')
def story(story_id):
st = mongo.db.stories.find_one({"_id": ObjectId(story_id)})
st = format_story(st)
return render_template('story.html', story=st)
@app.route('/<lang_code>/about')
def about():
return render_template('about.html')
if __name__ == "__main__":
app.run()