2012-04-24 14:22:59 +04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2012-04-26 23:19:12 +04:00
|
|
|
"""
|
|
|
|
Main script including controller, rooting, dependancy management, and
|
|
|
|
server run.
|
|
|
|
"""
|
|
|
|
|
2012-04-24 14:22:59 +04:00
|
|
|
import os
|
2012-04-24 22:15:38 +04:00
|
|
|
import hashlib
|
2012-04-24 14:22:59 +04:00
|
|
|
|
2012-04-27 01:41:20 +04:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
2012-04-26 23:19:12 +04:00
|
|
|
from src import settings, setup_path, Paste
|
2012-04-24 14:22:59 +04:00
|
|
|
|
2012-04-26 23:19:12 +04:00
|
|
|
setup_path()
|
2012-04-24 14:22:59 +04:00
|
|
|
|
2012-04-26 23:19:12 +04:00
|
|
|
from bottle import (Bottle, route, run, abort,
|
|
|
|
static_file, debug, view, request)
|
2012-04-24 14:22:59 +04:00
|
|
|
|
2012-04-24 22:15:38 +04:00
|
|
|
|
|
|
|
app = Bottle()
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/')
|
2012-04-24 14:22:59 +04:00
|
|
|
@view('home')
|
|
|
|
def index():
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
2012-04-24 22:15:38 +04:00
|
|
|
@app.route('/paste/create', method='POST')
|
|
|
|
def create_paste():
|
|
|
|
|
|
|
|
try:
|
|
|
|
content = unicode(request.forms.get('content', ''), 'utf8')
|
|
|
|
except UnicodeDecodeError:
|
2012-04-27 01:41:20 +04:00
|
|
|
return {'status': 'error',
|
|
|
|
'message': u"Encoding error: the paste couldn't be saved."}
|
2012-04-24 22:15:38 +04:00
|
|
|
|
|
|
|
if content:
|
2012-04-26 13:38:55 +04:00
|
|
|
expiration = request.forms.get('expiration', u'burn_after_reading')
|
|
|
|
paste = Paste(expiration=expiration, content=content)
|
2012-04-24 22:15:38 +04:00
|
|
|
paste.save()
|
2012-04-27 01:41:20 +04:00
|
|
|
return {'status': 'ok',
|
|
|
|
'paste': paste.uuid}
|
2012-04-24 22:15:38 +04:00
|
|
|
|
2012-04-27 01:41:20 +04:00
|
|
|
return {'status': 'error',
|
|
|
|
'message': u"Serveur error: the paste couldn't be saved. Please try later."}
|
2012-04-24 22:15:38 +04:00
|
|
|
|
|
|
|
|
2012-04-26 13:56:13 +04:00
|
|
|
@app.route('/paste/:paste_id')
|
|
|
|
@view('paste')
|
2012-04-26 13:38:55 +04:00
|
|
|
def display_paste(paste_id):
|
2012-04-24 22:15:38 +04:00
|
|
|
|
2012-04-27 01:41:20 +04:00
|
|
|
now = datetime.now()
|
|
|
|
keep_alive = False
|
2012-04-26 13:38:55 +04:00
|
|
|
try:
|
|
|
|
paste = Paste.load(paste_id)
|
2012-04-27 01:41:20 +04:00
|
|
|
# Delete the paste if it expired:
|
|
|
|
if 'burn_after_reading' in str(paste.expiration):
|
|
|
|
# burn_after_reading contains the paste creation date
|
|
|
|
# if this read appends 10 seconds after the creation date
|
|
|
|
# we don't delete the paste because it means it's the redirection
|
|
|
|
# to the paste that happens during the paste creation
|
|
|
|
try:
|
|
|
|
keep_alive = paste.expiration.split('#')[1]
|
|
|
|
keep_alive = datetime.strptime(keep_alive,'%Y-%m-%d %H:%M:%S.%f')
|
|
|
|
keep_alive = now < keep_alive + timedelta(seconds=10)
|
|
|
|
except IndexError:
|
|
|
|
keep_alive = False
|
|
|
|
if not keep_alive:
|
|
|
|
paste.delete()
|
|
|
|
|
|
|
|
elif paste.expiration < now:
|
|
|
|
paste.delete()
|
|
|
|
raise ValueError()
|
|
|
|
|
2012-04-26 13:38:55 +04:00
|
|
|
except (TypeError, ValueError):
|
2012-04-27 01:41:20 +04:00
|
|
|
abort(404, u"This paste doesn't exist or has expired")
|
2012-04-26 13:38:55 +04:00
|
|
|
|
2012-04-27 01:41:20 +04:00
|
|
|
return {'paste': paste, 'keep_alive': keep_alive}
|
2012-04-24 22:15:38 +04:00
|
|
|
|
|
|
|
|
2012-04-26 13:56:13 +04:00
|
|
|
@app.route('/static/<filename:path>')
|
2012-04-24 14:22:59 +04:00
|
|
|
def server_static(filename):
|
2012-04-24 22:15:38 +04:00
|
|
|
return static_file(filename, root=settings.STATIC_FILES_ROOT)
|
2012-04-24 14:22:59 +04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2012-04-24 22:15:38 +04:00
|
|
|
if settings.DEBUG:
|
2012-04-24 14:22:59 +04:00
|
|
|
debug(True)
|
2012-04-27 13:01:58 +04:00
|
|
|
run(app, host='localhost', port=8000, reloader=True, server="cherrypy")
|
2012-04-24 14:22:59 +04:00
|
|
|
else:
|
2012-04-27 13:01:58 +04:00
|
|
|
run(app, host='localhost', port=8000, server="cherrypy")
|