added custom image storage for profile photo.
authornishanth
Tue, 23 Feb 2010 20:24:26 +0530
changeset 61 708dd49d531b
parent 60 3eac96608091
child 62 db103856505e
added custom image storage for profile photo.
.hgignore
taskapp/models.py
taskapp/views/user.py
--- a/.hgignore	Tue Feb 23 19:42:12 2010 +0530
+++ b/.hgignore	Tue Feb 23 20:24:26 2010 +0530
@@ -40,4 +40,4 @@
 project/static/media
 project/kiwipycon/user/*.pyc
 apache/*
-
+images/*
--- a/taskapp/models.py	Tue Feb 23 19:42:12 2010 +0530
+++ b/taskapp/models.py	Tue Feb 23 20:24:26 2010 +0530
@@ -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 @@
 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 @@
     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)
--- a/taskapp/views/user.py	Tue Feb 23 19:42:12 2010 +0530
+++ b/taskapp/views/user.py	Tue Feb 23 20:24:26 2010 +0530
@@ -1,3 +1,4 @@
+import os
 from django.http import HttpResponse, Http404
 from django.shortcuts import redirect, render_to_response
 from pytask.taskapp.models import Task
@@ -74,7 +75,20 @@
         if request.user.is_authenticated() == True:
             profile = Profile.objects.get(user = request.user)
             data = request.POST#form.cleaned_data
-            properties = {'aboutme':data['aboutme'], 'foss_comm':data['foss_comm'], 'phonenum':data['phonenum'], 'homepage':data['homepage'], 'street':data['street'], 'city':data['city'], 'country':data['country'], 'nick':data['nick'],'photo':request.FILES['photo']}
+            properties = {'aboutme':data['aboutme'],
+                          'foss_comm':data['foss_comm'],
+                          'phonenum':data['phonenum'],
+                          'homepage':data['homepage'],
+                          'street':data['street'],
+                          'city':data['city'],
+                          'country':data['country'],
+                          'nick':data['nick']}
+            uploaded_photo = request.FILES.get('photo',None)
+            prev_photo = profile.photo
+            if uploaded_photo:
+                if prev_photo:
+                    os.remove(prev_photo.path)
+                properties['photo'] = uploaded_photo
             #fields = ['dob','gender','credits','aboutme','foss_comm','phonenum','homepage','street','city','country','nick']
             updateProfile(profile,properties)
             return redirect('/user/view/uid='+str(profile.user_id))