summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornishanth2010-02-23 20:24:26 +0530
committernishanth2010-02-23 20:24:26 +0530
commit5a9d62c40dc13a41a12678216ec34d726ddf237e (patch)
treeb5fad3d6a0d4cc0d8f89aa46f3ac23dc27a26436
parentddc16cd915c9314b7adf46fca8ea670718ffb867 (diff)
downloadpytask-5a9d62c40dc13a41a12678216ec34d726ddf237e.tar.gz
pytask-5a9d62c40dc13a41a12678216ec34d726ddf237e.tar.bz2
pytask-5a9d62c40dc13a41a12678216ec34d726ddf237e.zip
added custom image storage for profile photo.
-rw-r--r--.hgignore2
-rw-r--r--taskapp/models.py24
-rw-r--r--taskapp/views/user.py16
3 files changed, 39 insertions, 3 deletions
diff --git a/.hgignore b/.hgignore
index 899d580..4630cb4 100644
--- a/.hgignore
+++ b/.hgignore
@@ -40,4 +40,4 @@ project/media/user/*
project/static/media
project/kiwipycon/user/*.pyc
apache/*
-
+images/*
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)
diff --git a/taskapp/views/user.py b/taskapp/views/user.py
index 7581f35..8746214 100644
--- a/taskapp/views/user.py
+++ b/taskapp/views/user.py
@@ -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 @@ def edit_my_profile(request):
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))