From 55b7830fd3426d7731b979a67c13756108b87fca Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Tue, 26 Jan 2010 18:18:24 +0100 Subject: Optionally store nick using HTTP cookies. --- pastepy.py | 13 +++++++++++-- templates/paste.tmpl | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/pastepy.py b/pastepy.py index 20b386f..f63d275 100644 --- a/pastepy.py +++ b/pastepy.py @@ -1,4 +1,4 @@ -import templates, pygments, cgi, db, datetime, hashlib, settings, os, mimetypes +import templates, pygments, cgi, db, datetime, hashlib, settings, os, mimetypes, Cookie from pygments import highlight from pygments.lexers import get_all_lexers, get_lexer_by_name from pygments.formatters import HtmlFormatter @@ -22,6 +22,7 @@ class Paste(object): self.formatter = HtmlFormatter(linenos = 'table', lineanchors = 'line', anchorlinenos = True) def paste(self): + c = Cookie.SimpleCookie(self.environ['HTTP_COOKIE'] if 'HTTP_COOKIE' in self.environ else None) if self.environ['REQUEST_METHOD'] == 'POST': mp = cgi.FieldStorage(fp = self.environ['wsgi.input'], environ = self.environ, keep_blank_values = True) if mp['type'].value == 'Preview': @@ -36,6 +37,8 @@ class Paste(object): 'title': settings.pastebin_name, 'header': settings.pastebin_name, 'lexers': self.lexers, + 'nick': c['nick'].value if 'nick' in c else 'Anonymous', + 'remembered': 'nick' in c, }))] def preview(self, mp): @@ -75,7 +78,13 @@ class Paste(object): finally: session.close() - self.start_response('302 Found', [('Location', '/view/%s' % hash)]) + headers = [('Location', '/view/%s' % hash)] + c = Cookie.SimpleCookie() + c['nick'] = nick + dt = (datetime.datetime.utcnow() + datetime.timedelta(days = 30)) if 'remember_me' in mp else datetime.datetime.utcfromtimestamp(0) + c['nick']['expires'] = dt.strftime('%a, %d-%b-%y %H:%M:%S GMT') + headers.append(('Set-Cookie', c['nick'].OutputString())) + self.start_response('302 Found', headers) return [] def view(self): diff --git a/templates/paste.tmpl b/templates/paste.tmpl index 0a201fe..195d463 100644 --- a/templates/paste.tmpl +++ b/templates/paste.tmpl @@ -16,7 +16,7 @@ #end for -
  • +
  • -- cgit v1.2.3