--
Daniel,
I've done something similar for a simple image manager. I have a
WebImage object that holds references to an image and its thumbnail.
The thumbnail is created using imagemagick's convert function when
the image is updated. Extracts from models.py and admin.py shown
below should give you all the clues you need.
John
#################
# models.py
from django.conf import settings
from django.db import models
from django.db.models import F, Q
import os
import uuid
import subprocess
_CONVERT_CMD = '/usr/bin/convert'
_CONVERT_THUMBNAIL_OPTS = ['-thumbnail','150x50']
# ...
class WebImage(models.Model):
"Images that are used on the web pages. Limited to certain sizes.
Can be produced by an ImageGenerator."
def get_image_file_path(inst, filename):
fn, ext = os.path.splitext(filename)
filename = "%s%s" % (uuid.uuid4(), ext)
return os.path.join(settings.MEDIA_ROOT+'wi/', filename)
name = models.CharField(max_length=80,help_text="Full name of the
web image")
slug = models.SlugField(max_length=50,help_text="Slug for the web
image name")
description = models.TextField(help_text="Image description")
img =
models.ImageField(null=True,upload_to=get_image_file_path,height_field="height",width_field="width")
img_gen = models.ForeignKey('ImageGenerator', blank=True,
null=True, on_delete=models.SET_NULL)
img_size = models.ForeignKey('ImageSize', blank=True, null=True,
default=None, on_delete=models.SET_NULL)
width =
models.IntegerField(editable=False,blank=True,help_text="Width of
the picture. Not editable")
height =
models.IntegerField(editable=False,blank=True,help_text="Height of
the picture. Not editable")
licensing = models.TextField(blank=True,help_text="Details of
image ownership, copyright and licensing. Blank for 'same as
original image'")
created_by =
models.CharField(editable=False,blank=True,max_length=80,
help_text="Uploader. Not editable")
creation_date =
models.DateTimeField(editable=False,blank=True,auto_now_add=True,
help_text="Date/time uploaded. Not editable")
uploaded_by =
models.CharField(editable=False,blank=True,max_length=80,
help_text="Uploader. Not editable")
upload_date =
models.DateTimeField(editable=False,blank=True,auto_now_add=True,
help_text="Date/time uploaded. Not editable")
tags = TaggableManager()
def thumb_path(self):
return
os.path.join(settings.MEDIA_ROOT+'thumb/',os.path.basename(
self.img.name))
def thumb_url(self):
return "%sthumb/%s" %
(settings.MEDIA_URL,os.path.basename(
self.img.name))
def create_thumb(self):
args = [_CONVERT_CMD,
self.img.name] + _CONVERT_THUMBNAIL_OPTS +
[self.thumb_path()]
try:
output = subprocess.call(args)
except subprocess.CalledProcessError, e:
self.description = "%s\n%s\n%d" % (self.description, e.output,
e.returncode)
pass # This needs to be a logger.
self.save()
def thumb_field(self):
return ('<img src="%s" />' % self.thumb_url())
thumb_field.short_description = "Thumbnail"
thumb_field.allow_tags = True
def get_absolute_url(self):
return "%swi/%s" %
(settings.MEDIA_URL,os.path.split(
self.img.name)[1])
def __unicode__(self):
return "%s (%dx%d)" % (
self.name,self.width,self.height)
########################
# admin.py
class WebImageAdmin(ModelAdmin):
def save_model(self, req, obj, form, change):
# Add the uploader info to the object
if not change or obj.uploaded_by == '':
obj.uploaded_by = "%s (%s %s)" % (req.user.username,
req.user.first_name, req.user.last_name)
# And the creator info in the same manner
if not change or obj.created_by == '':
obj.created_by = "%s (%s %s)" % (req.user.username,
req.user.first_name, req.user.last_name)
# And save
obj.save()
# Create a thumbnail
obj.create_thumb()
list_display = ("__unicode__","img_size","thumb_field",)
list_filter = ('img_size','width','height')
ordering = ('name','width','height')
prepopulated_fields = {"slug": ("name",)}
save_on_top = True
form = WebImageForm