From: Michael Tremer Date: Mon, 22 Oct 2018 13:11:45 +0000 (+0100) Subject: nopaste: Refactor everything X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a41085fb1ef27d03105a162f3255ae7f384262a0;p=ipfire.org.git nopaste: Refactor everything Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 5a8ebe2f..4c6faf20 100644 --- a/Makefile.am +++ b/Makefile.am @@ -83,9 +83,9 @@ web_PYTHON = \ src/web/handlers_fireinfo.py \ src/web/handlers_iuse.py \ src/web/handlers_mirrors.py \ - src/web/handlers_nopaste.py \ src/web/location.py \ src/web/newsletter.py \ + src/web/nopaste.py \ src/web/people.py \ src/web/ui_modules.py @@ -150,6 +150,12 @@ templates_newsletter_DATA = \ templates_newsletterdir = $(templatesdir)/newsletter +templates_nopaste_DATA = \ + src/templates/nopaste/create.html \ + src/templates/nopaste/view.html + +templates_nopastedir = $(templatesdir)/nopaste + templates_people_DATA = \ src/templates/people/base.html \ src/templates/people/conferences.html \ diff --git a/src/backend/nopaste.py b/src/backend/nopaste.py index da7d54cc..71d0d77e 100644 --- a/src/backend/nopaste.py +++ b/src/backend/nopaste.py @@ -12,14 +12,10 @@ class Nopaste(Object): if account: uid = account.uid - # Escape any backslashes. PostgreSQL tends to think that they lead octal - # values or something that confuses the convertion from text to bytea. if type == "text": - content = content.replace("\\", "\\\\") mimetype = "text/plain" elif type == "file": - content = buffer(content) mimetype = self._guess_mimetype(content) # http://blog.00null.net/easily-generating-random-strings-in-postgresql/ @@ -39,8 +35,6 @@ class Nopaste(Object): # Return the mime type ms.setflags(magic.MAGIC_MIME_TYPE) - buf = "%s" % buf - try: return ms.buffer(buf) finally: @@ -67,7 +61,7 @@ class Nopaste(Object): WHERE uuid = %s", uuid) if res: - return res.content + return bytes(res.content) def _update_lastseen(self, uuid): self.db.execute("UPDATE nopaste SET time_lastseen = NOW(), views = views + 1 \ diff --git a/src/templates/base.html b/src/templates/base.html index aec348e4..1e6362b5 100644 --- a/src/templates/base.html +++ b/src/templates/base.html @@ -231,7 +231,7 @@
© {{ year }} - {{ _("IPFire is free software written by the IPFire Project") }} - {% if not current_user and hostname in ("blog.ipfire.org") %} + {% if not current_user and hostname in ("blog.ipfire.org", "nopaste.ipfire.org") %} {{ _("Login") }} {% elif current_user %} diff --git a/src/templates/nopaste/create.html b/src/templates/nopaste/create.html index 0ad8051d..ea3bf19b 100644 --- a/src/templates/nopaste/create.html +++ b/src/templates/nopaste/create.html @@ -8,76 +8,59 @@ {% end %} {% end block %} -{% block container %} +{% block content %} +
+
+ {% if mode == "paste" %} +

{{ _("New Paste") }}

+ {% elif mode == "upload" %} +

{{ _("Upload File") }}

+ {% end %} -
-
- {% if mode == "paste" %} -

{{ _("New Paste") }}

- {% elif mode == "upload" %} -

{{ _("Upload File") }}

- {% end %} - -
-
-
+ + {% raw xsrf_form_html() %} + + + + {% if mode == "paste" %}
- - - {% if mode == "paste" %} -
- -
- {% elif mode == "upload" %} - -
- - - {% if max_size %} -

- {{ _("You may upload up to %s.") % format_size(max_size) }} -

- {% end %} -
- {% end %} + +
- + {% end %} + +
{% if mode == "paste" %} -
- -
- -
-
+ + {% elif mode == "upload" %} + + + + {% if max_size %} + + {{ _("You may upload up to %s") % format_size(max_size) }} + + {% end %} {% end %} - -
- -
- -
-
- -
-
- -
-
- - {% raw xsrf_form_html() %} - -
-
-
-
+
+
+ + + +
+ + + +
+
{% end block %} diff --git a/src/templates/nopaste/view.html b/src/templates/nopaste/view.html index 04e00f0a..6d6f5e20 100644 --- a/src/templates/nopaste/view.html +++ b/src/templates/nopaste/view.html @@ -2,86 +2,69 @@ {% block title %}{{ entry.subject or _("Paste %s") % entry.uuid }}{% end block %} -{% block container %} -
- - - {% if content %} -
{{ content }}
+{% block content %} +
+
+
+
+

{{ entry.subject or _("Paste %s") % entry.uuid }}

+
+ {{ _("Uploaded %s") % locale.format_date(entry.time_created) }} - - - - {% else %} -
-

+ {% if entry.account %} + {{ _("by") }} - - - {{ _("Download File") }} - + {{ entry.account }} -

- {{ _("Filesize: %s") % format_size(entry.size) }} -

+ + {{ entry.account }} + , + {% else %} + {{ _("from %s") % entry.address }}, + {% end %} -

-
+ {{ _("One View", "%(num)s Views", entry.views) % { "num" : entry.views } }} +
-
- {% end %} +
+
+ {% if content %} + + + {{ _("Download") }} + + {% end %} -
-
-
- {% if entry.account %} -
{{ _("Uploaded by") }}
-
- {{ entry.account.name }} - {{ entry.account.name }} -
- {% elif current_user and entry.address %} -
{{ _("Uploaded from") }}
-
{{ entry.address }}
- {% end %} + + + {{ _("Report abuse") }} + +
+
-
{{ _("Created") }}
-
{{ locale.format_date(entry.time_created) }}
+
-
{{ _("Views") }}
-
{{ entry.views }}
+ {% if content %} +
{{ content }}
-
{{ _("Expires") }}
-
- {% if entry.time_expires %} - {{ locale.format_date(entry.time_expires) }} - {% else %} - {{ _("never") }} - {% end %} -
- -
+ + + -
- + {% elif entry.mimetype.startswith("image/") %} + + {% else %} + + {% end %} +
-
+ {% end block %} diff --git a/src/web/__init__.py b/src/web/__init__.py index 20c3f2ce..09a3826f 100644 --- a/src/web/__init__.py +++ b/src/web/__init__.py @@ -18,6 +18,7 @@ from . import blog from . import download from . import location from . import newsletter +from . import nopaste from . import people from . import ui_modules @@ -231,9 +232,9 @@ class Application(tornado.web.Application): # nopaste.ipfire.org self.add_handlers(r"nopaste(\.dev)?\.ipfire\.org", [ - (r"/", NopasteCreateHandler), - (r"/raw/(.*)", NopasteRawHandler), - (r"/view/(.*)", NopasteViewHandler), + (r"/", nopaste.CreateHandler), + (r"/raw/(.*)", nopaste.RawHandler), + (r"/view/(.*)", nopaste.ViewHandler), ] + authentication_handlers) # location.ipfire.org diff --git a/src/web/handlers_nopaste.py b/src/web/nopaste.py similarity index 80% rename from src/web/handlers_nopaste.py rename to src/web/nopaste.py index ae4ceb1f..125c4630 100644 --- a/src/web/handlers_nopaste.py +++ b/src/web/nopaste.py @@ -2,9 +2,9 @@ import tornado.web -from .handlers_base import * +from . import handlers_base as base -class NopasteCreateHandler(BaseHandler): +class CreateHandler(base.BaseHandler): MODES = ("paste", "upload") def get(self): @@ -55,7 +55,7 @@ class NopasteCreateHandler(BaseHandler): @property def _max_size(self): - # Authenticated users are allowed to upload up to 10MB + # Authenticated users are allowed to upload up to 25MB if self.current_user: return 25 * (1024 ** 2) @@ -63,7 +63,7 @@ class NopasteCreateHandler(BaseHandler): return 2 * (1024 ** 2) -class NopasteRawHandler(BaseHandler): +class RawHandler(base.BaseHandler): def get(self, uid): entry = self.backend.nopaste.get(uid) if not entry: @@ -76,22 +76,14 @@ class NopasteRawHandler(BaseHandler): self.set_header("Content-Type", entry.mimetype) # Set expiry headers - expires = entry.time_expires or \ - (datetime.datetime.utcnow() + datetime.timedelta(days=30)) - - # For HTTP/1.0 - self.set_header("Expires", expires) - - # For HTTP/1.1 - max_age = expires - datetime.datetime.utcnow() - self.set_header("Cache-Control", "public,max-age=%d" % max_age.total_seconds()) + self.set_expires(3600) # Send content content = self.backend.nopaste.get_content(entry.uuid) self.finish(content) -class NopasteViewHandler(BaseHandler): +class ViewHandler(base.BaseHandler): def get(self, uid): entry = self.backend.nopaste.get(uid) if not entry: