-
Notifications
You must be signed in to change notification settings - Fork 0
/
webapp.py
82 lines (70 loc) · 2.28 KB
/
webapp.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
'''
codegloo@gmail.com
'''
import os
import web
import time
import tempfile
from random import sample, randrange
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
from settings import db, render, setup_session
urls = (
'/', 'index',
'/(\d+)', 'view',
'/(\d+)/copy', 'copy',
'/about', 'about',
)
def now_time():
now = time.localtime(time.time())
return time.strftime("%Y-%m-%d %H:%M:%S %Z", now)
def pygmentize(source, syntax):
lexer = get_lexer_by_name(syntax, stripall=True)
formatter = HtmlFormatter(linenos=True, cssclass="syntax")
return highlight(source, lexer, formatter)
class about:
def GET(self):
return render.about()
class index:
def GET(self):
print "SESSION : ", dir(session)
name = session.get('name', '')
title = session.get('title', '')
return render.index(src=None,title=title,name=name,cache=False)
def POST(self):
i = web.input()
#store values in session as "remember me"
session.name = i.name
session.title = i.title
session.name = i.name
syntax = i.syntax.lower()
if not syntax: syntax = 'text'
html = pygmentize(i.source, syntax)
n = db.insert('pastes', source=i.source, title=i.title,
name=i.name, syntax=i.syntax, html=html, created=now_time(),
ipaddr=web.ctx.env['REMOTE_ADDR'])
return web.seeother('/%s' % (n,))
class view:
def GET(self, n):
res = db.select('pastes', where='id=$n', vars=locals())
db.update('pastes', where='id=$n',vars=locals(), lastview=now_time())
if res:
return render.view(res[0],n)
else:
return web.notfound()
class copy:
def GET(self,n):
title = session.get('title', '')
name = session.get('name', '')
print 'title, name: ', title, name, '<--'
res = db.select('pastes', where='id=$n', vars=locals())
if res:
return render.index(src=res[0].source, title=title, name=name)
else:
return web.notfound()
app = web.application(urls, globals())
setup_session(app)
application = app.wsgifunc()
if __name__ == "__main__":
app.run()