-
Notifications
You must be signed in to change notification settings - Fork 0
/
__main__.py
155 lines (111 loc) · 3.92 KB
/
__main__.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
import beaker.middleware
from dominate import document
from dominate.util import raw
from dominate.tags import *
from pony.orm import *
from bottle import *
db = Database()
db.bind(provider="mysql", host="localhost", user="root", passwd="", db="paste")
app = beaker.middleware.SessionMiddleware(
app(),
{
"session.type": "file",
"session.data_dir": "./session/",
"session.auto": True,
"session.cookie_expires": True,
},
)
class Paste(db.Entity):
id = PrimaryKey(str)
paste = Required(unicode, max_len=16777215)
# set_sql_debug(True)
db.generate_mapping(create_tables=True)
@hook("after_request")
def enable_cors():
response.headers["Access-Control-Allow-Origin"] = "self"
@hook("before_request")
def setup_request():
request.session = request.environ["beaker.session"]
@get("/")
def index():
if not "id" in request.session:
request.session["id"] = "%030x" % random.randrange(16 ** 64)
else:
request.session["id"] = "%030x" % random.randrange(16 ** 64)
with document(title="pystebin") as doc:
with doc.head:
meta(name="viewport", content="width=device-width, initial-scale=1")
link(rel="stylesheet", href="/static/pystebin.css")
with doc.body:
with div(id="page"):
textarea(
id="paste",
name="paste",
placeholder="Paste..",
spellcheck=False,
)
with div(id="controls"):
span("Not saved", id="status")
raw(" : ")
a("new", href="/")
raw(" • ")
a("about", href="/about")
script(defer=True, src="/static/pystebin.js")
return template(doc.render(pretty=False))
@get("/<paste:re:[a-f0-9]+>")
@db_session
def view_paste(paste):
if Paste.exists(id=paste):
paste = Paste.get(id=paste)
else:
response.status = 404
with document(title="pystebin") as doc:
with doc.head:
meta(name="viewport", content="width=device-width, initial-scale=1")
link(rel="stylesheet", href="/static/pystebin.css")
with doc.body:
with div(id="page"):
textarea(paste.paste, readonly=True, spellcheck=False)
with div(id="controls"):
span("Viewing paste", id="status")
raw(" : ")
a("new", href="/")
raw(" • ")
a("about", href="/about")
return template(doc.render(pretty=False))
@post("/paste")
@db_session
def create_paste():
response.content_type = "application/json; charset=utf-8"
content = request.forms.get("paste")
if not Paste.exists(id=request.session["id"]):
Paste(id=request.session["id"], paste=content)
commit()
else:
Paste.get(id=request.session["id"]).set(paste=content)
return {"id": request.session["id"]}
@get("/about")
def about():
about = "<br>".join(open("./static/about.txt", "r").readlines())
with document(title="pystebin : about") as doc:
with doc.head:
meta(name="viewport", content="width=device-width, initial-scale=1")
with doc.body:
p(raw(about))
return template(doc.render(pretty=False))
@get("/static/<name>")
def static(name):
return static_file(name, root="./static")
@get("/favicon.ico")
def favicon():
return static_file("favicon.ico", root="./static")
@error(404)
def not_found(error):
return error
@error(403)
def forbidden(error):
return error
run(app=app, host="localhost", port=8118)