Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Changing the get_image_path shortcut because it screws things up if t…

…he object being saved doesn't have a primary key yet. The Group model shows how it can be used.
  • Loading branch information...
commit 5884a2baa70ed2d17eb0eb6379d7eeae2f0c9fc3 1 parent 8792fb2
@nathanborror authored
Showing with 16 additions and 5 deletions.
  1. +11 −2 basic/groups/models.py
  2. +5 −3 basic/tools/shortcuts.py
View
13 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
@@ -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)
View
8 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):
Please sign in to comment.
Something went wrong with that request. Please try again.