-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
310 lines (234 loc) · 8 KB
/
app.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
from bottle import Bottle, run, static_file, request, redirect, response
import os
from pymongo import Connection
from bson import ObjectId
import markdown
from functools import wraps
import pbkdf2
import requests
import re
import json
from bs4 import BeautifulSoup
import random
import string
import yaml
import datetime
import hashlib
from jinja2 import Environment, FileSystemLoader
from time import time
jinjaenv = Environment(loader=FileSystemLoader('templates'))
app = Bottle()
MONGOLAB_URI = os.environ.get('MONGOLAB_URI', 'MONGOLAB_URI goes here')
CONNECTION = Connection(MONGOLAB_URI)
db = CONNECTION[MONGOLAB_URI.rsplit('/',1)[1]]
COOKIE_KEY = "random characacters go here"
COOKIE_NAME = 'pick a cookie name'
SESSION_CUTOFF = 60*60*24 #timeout expiration
BROWSER_ID_AUDIENCES = ['hostnames','that','will', 'be', 'used', 'against', 'browserid', 'auth']
def bad(msg="Error"):
return dict(status="error", msg=msg)
def good(**kwargs):
return dict(status="good",**kwargs)
def user_required(f):
@wraps(f)
def df(*args, **kwargs):
if not request.environ['auth']:
return bad("Not logged in")
else:
return f(*args, **kwargs)
return df
def cookier(n=None):
response.set_cookie(COOKIE_NAME, n,
secret=COOKIE_KEY,
path='/',
httponly=True)
def unicodecleaner(txt):
try:
txt = txt.decode('utf-8', 'ignore')
except UnicodeDecodeError:
pass
except UnicodeEncodeError:
pass
txt = txt.encode('ascii', 'ignore')
return txt
def generate_session(length=50):
return ''.join(random.choice(string.ascii_letters + string.digits) for x in range(length))
def render_idea(idea):
doc = idea['doc'] if 'doc' in idea else ''
ret = []
meta = {}
meta['title'] = idea['txt']
section = doc.strip().split('\n\n',1)
if len(section) == 2:
newmeta, content = section
newmeta = yaml.load(newmeta)
if newmeta:
meta.update()
else:
content = section[0]
ret.append('<h1>' + meta['title'] + '</h1>')
if 'date' in meta:
ret.append('<h2>' + meta['date'] + '</h2>')
ret.append(markdown.markdown(content,
extensions=['fenced_code', 'codehilite']))
return ''.join(ret)
@app.hook('before_request')
def before_request():
auth_cookie = request.get_cookie(COOKIE_NAME, secret=COOKIE_KEY)
session = db.sessions.find_one({"session_id": auth_cookie})
if auth_cookie and session:
request.environ['auth'] = True
request.environ['auth.email'] = session['email']
db.sessions.update({'_id': session['_id']}, {'$set': {'atime': time()}}, safe=True)
else:
request.environ['auth'] = False
request.environ['auth.email'] = ''
db.sessions.remove({'atime': {'$lt': SESSION_CUTOFF}})
@app.get('/')
def index():
#idea = db.ideas.find_one({'_id': ObjectId('xxxxxxxxxxxxxxxxxxxxxxxxx')})
idea = "yay it worked!"
return jinjaenv.get_template('index.html').render(intro="")
@app.get('/styles.css')
def styles():
return static_file('styles.css', root=os.path.dirname(__file__))
@app.get('/script.js')
def script():
return static_file('script.js', root=os.path.dirname(__file__))
@app.get('/favicon.ico')
def favicon():
return static_file('favicon.ico', root=os.path.dirname(__file__))
@app.get('/robots.txt')
def robots():
response.content_type = 'text/plain; charset=utf-8'
return "User-agent: *\nAllow: /\n"
@app.get('/idea')
@user_required
def get_ideas():
ideas = db.ideas.find({'email':request.environ['auth.email']}, sort=[('mtime',1)])
def myfilter(x):
return dict(
txt=x['txt'],
id=str(x['_id']),
wc=x['wc'] if 'wc' in x else 0,
published=x['published'])
ret = map(myfilter, ideas)
return good(ideas=list(ret))
@app.get('/idea/<idea_id>')
def get_idea(idea_id):
idea = db.ideas.find_one(dict(
_id=ObjectId(idea_id),
email=request.environ['auth.email']))
idea = db.ideas.find_one({'$or': [
{'_id': ObjectId(idea_id), 'email': request.environ['auth.email'] },
{'_id': ObjectId(idea_id), 'published': 1 }
]})
if 'published' not in idea:
idea['published'] = 0
if idea:
doc = idea['doc'] if 'doc' in idea else ''
return good(id=str(idea['_id']), txt=idea['txt'], doc=doc, published=idea['published'], wc=idea['wc'] if 'wc' in idea else 0)
return bad()
@app.post('/idea')
@user_required
def add_idea():
txt = request.forms.get('txt')
date = datetime.datetime.now().strftime("%Y/%m/%d")
doc = "type: post\ndate: {1}\n\n...".format(txt, date)
wc = 0
md5 = hashlib.md5()
try:
httpr = requests.get(txt.split('#')[0])
soup = BeautifulSoup(httpr.text)
title = ''.join(soup.title.stripped_strings)
title = title.encode('utf-8', 'ignore')
doc = "type: link\ndate: {2}\n\n[{0}]({1})".format(title, txt, date)
wc = len(doc.split())
txt = title
except Exception as e:
pass
md5.update(unicodecleaner(txt))
md5.update(unicodecleaner(doc))
md5.update(str(0))
new_id = db.ideas.insert(dict(
txt=txt,
doc=doc,
wc=wc,
published=0,
email=request.environ['auth.email'],
mtime=time(),
hash=md5.hexdigest()
))
return good(id=str(new_id), txt=txt)
@app.put('/idea/<idea_id>')
@user_required
def update_idea(idea_id):
idea = db.ideas.find_one(dict(
_id=ObjectId(idea_id),
email=request.environ['auth.email']))
if not idea:
return bad()
txt = request.forms['txt'] if 'txt' in request.forms else idea['txt']
doc = request.forms['doc'] if 'doc' in request.forms else idea['doc']
published = int(request.forms['published']) if 'published' in request.forms else idea['published']
md5 = hashlib.md5()
wordcount = len(doc.split())
if txt is not None:
if not txt:
txt = "untitled"
md5.update(unicodecleaner(txt))
md5.update(unicodecleaner(doc))
md5.update(str(published))
hsh = md5.hexdigest()
if hsh != idea['hash']:
db.ideas.update(dict(_id=ObjectId(idea_id)), {"$set": dict(
txt=txt,
doc=doc,
wc=wordcount,
published=published,
mtime=time(),
hash=hsh
)})
return good()
@app.delete('/idea/<idea_id>')
@user_required
def delete_idea(idea_id):
db.ideas.remove(ObjectId(idea_id))
return good()
@app.get('/<idea_id>.<format>')
@app.get('/<idea_id>')
def get_md_idea_html(idea_id, format='html'):
idea = db.ideas.find_one({'$or': [
{'_id': ObjectId(idea_id), 'email': request.environ['auth.email'] },
{'_id': ObjectId(idea_id), 'published': 1 }
]})
if not idea: return bad()
#return ''.join(ret)
return jinjaenv.get_template('base.html').render(content=render_idea(idea))
@app.post('/auth')
def authbrowserid():
assertion = request.forms['assertion']
for audience in BROWSER_ID_AUDIENCES:
payload = dict(assertion=assertion, audience=audience)
r = requests.post('https://browserid.org/verify', data=payload)
data = json.loads(r.content) if r else None
if data and data['status'] == 'okay':
email = data['email']
session_id = generate_session()
db.sessions.insert(dict(email=email, session_id=session_id, atime=time()))
cookier(session_id)
return redirect('/')
return redirect('/')
@app.get('/auth')
def getauth():
if request.environ['auth']:
return good(email=request.environ['auth.email'])
return bad()
@app.get('/logout')
@app.post('/logout')
def logout():
response.delete_cookie("auth")
db.sessions.remove({"email": request.environ['auth.email']})
return redirect('/')
if __name__ == '__main__':
run(app, reloader=True, host='0.0.0.0', port=9999)