Skip to content

Commit

Permalink
Changing the get_image_path shortcut because it screws things up if t…
Browse files Browse the repository at this point in the history
…he object being saved doesn't have a primary key yet. The Group model shows how it can be used.
  • Loading branch information
nathanborror committed Aug 13, 2010
1 parent 8792fb2 commit 5884a2b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
13 changes: 11 additions & 2 deletions basic/groups/models.py
@@ -1,8 +1,9 @@
from django.db import models
from django.db.models import permalink
from django.contrib.auth.models import User
from django.conf import settings

from basic.tools.shortcuts import get_image_path
from basic.tools.shortcuts import build_filename


GROUP_OWNER = 0
Expand All @@ -15,13 +16,21 @@
)


def get_icon_path(instance, filename):
if instance.pk:
group = Group.objects.get(pk=instance.pk)
if group.icon:
return group.icon.path.replace(settings.MEDIA_ROOT, '')
return build_filename(instance, filename)


class Group(models.Model):
""" Group model """
title = models.CharField(blank=False, max_length=255)
slug = models.SlugField(unique=True, help_text="Used for the Group URL: http://example.com/groups/the-club/")
tease = models.TextField(blank=True, help_text="Brief explaination of what this group is. Shows up when the group is listed amoung other groups.")
creator = models.ForeignKey(User, related_name='created_groups', help_text="Serves as a record as who the original creator was in case ownership is transfered.")
icon = models.FileField(upload_to=get_image_path, blank=True, help_text="Needs to be larger than 120x120 pixels.")
icon = models.FileField(upload_to=get_icon_path, blank=True, help_text="Needs to be larger than 120x120 pixels.")
invite_only = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True)
Expand Down
8 changes: 5 additions & 3 deletions basic/tools/shortcuts.py
@@ -1,18 +1,20 @@
import os.path
import hashlib
import datetime

from django.shortcuts import render_to_response
from django.template.context import RequestContext
from django.http import HttpResponseRedirect


def get_image_path(instance, filename):
def build_filename(instance, filename):
"""
Converts an image filename to a hash.
"""
name = hashlib.md5("%s" % instance.id).hexdigest()
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
name = hashlib.md5('%s' % now).hexdigest()
ext = os.path.splitext(filename)
return os.path.join("%s/%s" % (instance._meta.app_label, instance._meta.module_name), '%s%s' % (name, ext[1]))
return os.path.join('%s/%s' % (instance._meta.app_label, instance._meta.module_name), '%s%s' % (name, ext[1]))


def render(request, *args, **kwargs):
Expand Down

0 comments on commit 5884a2b

Please sign in to comment.