diff options
author | nishanth | 2010-02-23 20:24:26 +0530 |
---|---|---|
committer | nishanth | 2010-02-23 20:24:26 +0530 |
commit | 5a9d62c40dc13a41a12678216ec34d726ddf237e (patch) | |
tree | b5fad3d6a0d4cc0d8f89aa46f3ac23dc27a26436 /taskapp/models.py | |
parent | ddc16cd915c9314b7adf46fca8ea670718ffb867 (diff) | |
download | pytask-5a9d62c40dc13a41a12678216ec34d726ddf237e.tar.gz pytask-5a9d62c40dc13a41a12678216ec34d726ddf237e.tar.bz2 pytask-5a9d62c40dc13a41a12678216ec34d726ddf237e.zip |
added custom image storage for profile photo.
Diffstat (limited to 'taskapp/models.py')
-rw-r--r-- | taskapp/models.py | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/taskapp/models.py b/taskapp/models.py index c95211f..eb5130a 100644 --- a/taskapp/models.py +++ b/taskapp/models.py @@ -1,3 +1,7 @@ +import random +import string +import os +from django.core.files.storage import FileSystemStorage from django.db import models from django.contrib.auth.models import User import tagging @@ -24,6 +28,24 @@ STATUS_CHOICES = ( IMAGES_DIR = "./images" UPLOADS_DIR = "./uploads" +class CustomImageStorage(FileSystemStorage): + + def path(self, name): + """ we return images directory path. + """ + + return os.path.join(IMAGES_DIR, name) + + def get_available_name(self, name): + """ here we are going with username as the name of image. + """ + + root, ext = os.path.splitext(name) + name = ''.join([ random.choice(string.uppercase+string.digits) for i in range(10)])+ext + while self.exists(name): + name = ''.join([ random.choice(string.uppercase+string.digits) for i in range(10)])+ext + return name + class Profile(models.Model): user = models.ForeignKey(User, unique = True) @@ -41,7 +63,7 @@ class Profile(models.Model): city = models.CharField(max_length = 25, blank = True) country = models.CharField(max_length = 25, blank = True) nick = models.CharField(max_length = 20, blank = True) - photo = models.ImageField(upload_to = IMAGES_DIR, blank = True) + photo = models.ImageField(storage = CustomImageStorage(),upload_to = IMAGES_DIR, blank = True) def __unicode__(self): return unicode(self.user.username) |