Permalink
Browse files

Save images to disk for thumbnailing

  • Loading branch information...
stephenmcd committed Jul 31, 2011
1 parent 7acb585 commit 88e35a4208c903b6d0d31d3708c53b4976b30bc9
Showing with 23 additions and 2 deletions.
  1. +1 −0 .hgignore
  2. +8 −0 core/templatetags/core_tags.py
  3. +10 −0 core/utils.py
  4. +4 −2 templates/list.html
View
@@ -8,3 +8,4 @@ syntax: glob
local_settings.py
static/cache/*
static/photos/*
+static/drawings/*
@@ -16,6 +16,14 @@ def photo_for_user(user):
"""
return "photos/%s" % user.id
+@register.filter
+def image_for_drawing(drawing):
+ """
+ Just returns the path to the drawing's image since we can't
+ combined values in a template to pass to the thumbnail tag.
+ """
+ return "drawings/%s" % drawing.id
+
@register.simple_tag(takes_context=True)
def load_in_progress(context):
"""
View
@@ -1,4 +1,8 @@
+from os import mkdir
+from os.path import join, exists
+
+from django.conf import settings
from django.contrib.auth.models import User
from redis import Redis, ConnectionPool
@@ -69,6 +73,12 @@ def save(self, message):
data=message[3].replace(" ", "+"))
for user_id in redis.smembers(self.drawers_data_key):
drawing.users.add(User.objects.get(id=user_id))
+ # Save image file for thumbnailing.
+ path = join(settings.MEDIA_ROOT, "drawings")
+ if not exists(path):
+ mkdir(path)
+ with open(join(path, str(drawing.id)), "wb") as f:
+ f.write(drawing.data.split(",", 1)[1].decode("base64"))
return False
def mousedown(self, message):
View
@@ -1,5 +1,5 @@
{% extends "base.html" %}
-{% load pagination_tags %}
+{% load thumbnail core_tags pagination_tags %}
{% block body_class %}gallery{% endblock %}
@@ -8,7 +8,9 @@
<div class="clearfix gallery-items">
{% for drawing in drawings %}
<div>
- <a href="{% url view drawing.slug %}"><img src="{{ drawing.data }}">{{ drawing }}</a><br>
+ {% with drawing|image_for_drawing as drawing_image %}
+ <a href="{% url view drawing.slug %}"><img src="{% thumbnail drawing_image 152x71 %}">{{ drawing }}</a><br>
+ {% endwith %}
{% include "rating.html" %}
</div>
{% endfor %}

0 comments on commit 88e35a4

Please sign in to comment.