Skip to content

Commit

Permalink
Save images to disk for thumbnailing
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenmcd committed Jul 31, 2011
1 parent 7acb585 commit 88e35a4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions .hgignore
Expand Up @@ -8,3 +8,4 @@ syntax: glob
local_settings.py
static/cache/*
static/photos/*
static/drawings/*
8 changes: 8 additions & 0 deletions core/templatetags/core_tags.py
Expand Up @@ -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):
"""
Expand Down
10 changes: 10 additions & 0 deletions core/utils.py
@@ -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

Expand Down Expand Up @@ -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):
Expand Down
6 changes: 4 additions & 2 deletions templates/list.html
@@ -1,5 +1,5 @@
{% extends "base.html" %}
{% load pagination_tags %}
{% load thumbnail core_tags pagination_tags %}

{% block body_class %}gallery{% endblock %}

Expand All @@ -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 %}
Expand Down

0 comments on commit 88e35a4

Please sign in to comment.