taskapp/models.py
changeset 118 5bbb29a07b87
parent 116 cb7f54ed5866
child 119 39ab7c460143
--- a/taskapp/models.py	Fri Feb 26 15:00:01 2010 +0530
+++ b/taskapp/models.py	Fri Feb 26 15:15:16 2010 +0530
@@ -26,6 +26,13 @@
 IMAGES_DIR = "./images"
 UPLOADS_DIR = "./uploads"
 
+def get_key():
+    """ generate a 10 character name with random alphabets and digits.
+    """
+
+    name = ''.join([ random.choice(string.uppercase+string.digits) for i in range(10)])
+    return name
+
 class CustomImageStorage(FileSystemStorage):
 
     def path(self, name):
@@ -39,10 +46,10 @@
         """
     
         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
+        file_name = get_key() + ext
+        while self.exists(file_name):
+            file_name = get_key() + ext
+        return file_name
 
 class Profile(models.Model):