]>
git.ipfire.org Git - ipfire.org.git/blob - src/web/nopaste.py
c25fe60639f5a74782d13545daab92a3cc29c379
6 from . import ui_modules
8 class CreateHandler(base
.AnalyticsMixin
, base
.BaseHandler
):
9 @tornado.web
.authenticated
11 self
.render("nopaste/create.html")
13 @tornado.web
.authenticated
14 @base.ratelimit(minutes
=15, requests
=5)
16 subject
= self
.get_argument("subject", None)
17 content
= self
.get_argument("content")
20 expires
= self
.get_argument_int("expires", "0")
22 with self
.db
.transaction():
23 paste
= self
.backend
.nopaste
.create(content
, subject
=subject
, expires
=expires
,
24 account
=self
.current_user
, address
=self
.get_remote_ip())
26 # Redirect to the paste
27 return self
.redirect("/view/%s" % paste
.uuid
)
31 def get_current_user(self
):
32 if self
.request
.method
== "PUT":
33 return self
.perform_basic_authentication()
35 # Perform the usual authentication
36 return super().get_current_user()
38 def check_xsrf_cookie(self
):
39 # Skip the check on PUT
40 if self
.request
.method
== "PUT":
43 # Perform the check as usual
44 super().check_xsrf_cookie()
46 def write_error(self
, *args
, **kwargs
):
47 if self
.request
.method
== "PUT":
50 # Write errors as usual
51 return super().write_error(*args
, **kwargs
)
53 @tornado.web
.authenticated
54 @base.ratelimit(minutes
=15, requests
=5)
56 with self
.db
.transaction():
57 paste
= self
.backend
.nopaste
.create(
58 self
.request
.body
, account
=self
.current_user
, address
=self
.get_remote_ip())
60 # Send a message to the client
61 self
.write("https://%s/view/%s\n" % (self
.request
.host
, paste
.uuid
))
63 # Tell the user when this expires
65 self
.write(" This paste will expire at %s\n" % self
.locale
.format_date(paste
.expires_at
))
71 class UploadHandler(base
.AnalyticsMixin
, base
.BaseHandler
):
72 @tornado.web
.authenticated
74 self
.render("nopaste/upload.html")
76 @tornado.web
.authenticated
78 subject
= self
.get_argument("subject", None)
81 expires
= self
.get_argument_int("expires", "0")
83 with self
.db
.transaction():
84 for f
in self
.request
.files
.get("file", []):
85 paste
= self
.backend
.nopaste
.create(f
.body
, subject
=subject
, expires
=expires
,
86 account
=self
.current_user
, address
=self
.get_remote_ip())
88 # Only accept one file
91 # Complain if no file was selected
93 raise tornado
.web
.HTTPError(400, "No file uploaded")
95 # Redirect to the paste
96 return self
.redirect("/view/%s" % paste
.uuid
)
99 class RawHandler(base
.AnalyticsMixin
, base
.BaseHandler
):
101 with self
.db
.transaction():
102 paste
= self
.backend
.nopaste
.get(uid
)
104 raise tornado
.web
.HTTPError(404)
106 # This has received a view
110 self
.set_header("Content-Disposition", "inline; filename=\"%s\"" % paste
.subject
)
113 self
.set_header("Content-Type", paste
.mimetype
)
116 self
.finish(paste
.blob
)
119 class ViewHandler(base
.AnalyticsMixin
, base
.BaseHandler
):
121 with self
.db
.transaction():
122 paste
= self
.backend
.nopaste
.get(uid
)
124 raise tornado
.web
.HTTPError(404)
126 # This has received a view
129 self
.render("nopaste/view.html", paste
=paste
)
132 class CodeModule(ui_modules
.UIModule
):
133 def render(self
, content
):
134 return self
.render_string("nopaste/modules/code.html", content
=content
)
136 def javascript_files(self
):
137 return "js/prettify.js"
140 return "css/prettify.css"
142 def embedded_javascript(self
):