Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding support for file uploads

  • Loading branch information...
commit cf350d1600c217078d230d88a1c5ce91e04bf396 1 parent e6c14f1
@progrium progrium authored
Showing with 46 additions and 15 deletions.
  1. +20 −7 bin.py
  2. +12 −2 models.py
  3. +5 −4 static/style.css
  4. +9 −2 templates/bin.html
View
27 bin.py
@@ -6,6 +6,8 @@
from models import Bin, Post
import urllib
import re
+import hashlib
+from cgi import FieldStorage
class BinHandler(webapp.RequestHandler):
def get(self):
@@ -19,12 +21,13 @@ def get(self):
self.redirect('/%s' % bin.name)
else:
posts = bin.post_set.order('-created').fetch(50)
- type = 'atom' if feed else 'html'
- self.response.out.write(template.render('templates/bin.' + type, {'bin':bin, 'posts':posts, 'request':self.request}))
+ self.response.out.write(template.render('templates/bin.%s' % ('atom' if feed else 'html'),
+ {'bin':bin, 'posts':posts, 'request':self.request}))
def post(self):
bin = self._get_bin(self.request.path)
self._record_post(bin)
+ # TODO: This should maybe be a header thing
if 'http://' in self.request.query_string:
urlfetch.fetch(url=self.request.query_string.replace('http://', 'http://hookah.webhooks.org/'),
payload=urllib.urlencode(self.request.POST.items()), method='POST')
@@ -40,12 +43,22 @@ def head(self):
def _record_post(self, bin, use_get=False):
post = Post(bin=bin, remote_addr=self.request.remote_addr)
post.headers = dict(self.request.headers)
- post.body = self.request.body
+ try:
+ post.body = self.request.body
+ except UnicodeDecodeError:
+ #post.body_binary = self.request.body
+ pass
post.query_string = self.request.query_string
- if use_get:
- data_source = self.request.GET
- else:
- data_source = self.request.POST
+ data_source = self.request.GET if use_get else self.request.POST
+ for k in data_source.keys():
+ if isinstance(data_source[k], FieldStorage):
+ file_body = data_source[k].file.read()
+ data_source[k] = {
+ 'file_name': data_source[k].filename,
+ 'file_extension': data_source[k].filename.split('.')[-1],
+ 'file_digest': hashlib.md5(file_body).hexdigest(),
+ 'file_size': round(len(file_body) / 1024.0, 1),
+ }
post.form_data = [[k,v] for k,v in data_source.items()]
post.put()
View
14 models.py
@@ -3,6 +3,7 @@
from google.appengine.api import datastore_types
from django.utils import simplejson
+
def baseN(num,b,numerals="0123456789abcdefghijklmnopqrstuvwxyz"):
return ((num == 0) and "0" ) or (baseN(num // b, b).lstrip("0") + numerals[num % b])
@@ -42,6 +43,7 @@ class Post(db.Model):
query_string = db.StringProperty()
form_data = ObjectProperty()
body = db.TextProperty()
+ #body_binary = db.BlobProperty()
def id(self):
return baseN(abs(hash(self.created)), 36)[0:6]
@@ -56,13 +58,21 @@ def __iter__(self):
for k,v in items:
try:
outval = simplejson.dumps(simplejson.loads(v), sort_keys=True, indent=2)
- except ValueError:
+ except (ValueError, TypeError):
outval = v
out.append((k, outval))
else:
out = (('body', self.body),)
- return iter(sorted(out))
+ # Sort by field/file then by field name
+ files = list()
+ fields = list()
+ for (k,v) in out:
+ if type(v) is dict:
+ files.append((k,v))
+ else:
+ fields.append((k,v))
+ return iter(sorted(fields) + sorted(files))
def __str__(self):
return '\n'.join("%s = %s" % (k,v) for k,v in self)
View
9 static/style.css
@@ -3,14 +3,15 @@ body { font-family: Helvetica,Arial,sans-serif; font-size: smaller;}
#header { border-bottom: 2px solid darkgreen; padding: 5px; padding-bottom: 1px; background-color: lightgreen;}
#wrapper { margin-left: auto; margin-right: auto; width: 800px;}
#content { margin: 10px; }
-pre { font-size: larger;}
+pre { font-size: larger; display: inline;}
ul { padding-left: 20px;}
-li { margin-bottom: 10px;}
+li { margin-bottom: 20px;}
#make-form { margin: 20px; text-align: center; margin-right: 80px;}
#make-form input { font-size: larger;}
a.headers { font-size: smaller;}
-td { font-size: smaller; }
-td.key { width: 50px; font-weight: bold;}
+td { font-size: smaller; padding-top: 2px; }
+td.key { width: 75px; font-weight: bold; padding-right: 4px;}
+
dl {
padding: 0.5em;
View
11 templates/bin.html
@@ -19,8 +19,15 @@
<table>
{% for part in post %}
<tr>
- <td title="{{ part.0 }}" valign="top" class="key">{{ part.0 }}</td>
- <td>{% ifequal part.0 "body" %}<pre>{% endifequal %}{{ part.1|escape }}{% ifequal part.0 "body" %}</pre>{% endifequal %}</td></tr>
+ <td title="{{ part.0 }}" valign="top" align="right" class="key">{{ part.0 }}</td>
+ <td>
+ {% if part.1.file_digest %}
+ <img src="http://openicon.appspot.com/{{ part.1.file_extension|default:"text/plain" }}?size=16&default=http%3A%2F%2Fopenicon.appspot.com%2Ftext%2Fplain" style="vertical-align: middle; margin-right: 2px; margin-top: -4px;" /><pre>{{ part.1.file_name }}</pre> ({{ part.1.file_size }}KB)<br />
+ <code>{{ part.1.file_digest}}</code>
+ {% else %}
+ <pre>{{ part.1|escape }}</pre>
+ {% endif %}
+ </td></tr>
{% endfor %}
</table>
</li>
Please sign in to comment.
Something went wrong with that request. Please try again.