modified make_key and used it in the form
authorNishanth Amuluru <nishanth@fossee.in>
Fri, 07 Jan 2011 09:01:27 +0530
changeset 253 beb830b0e744
parent 252 5a92fcacdd5a
child 254 5c000bf6f241
modified make_key and used it in the form
profile/forms.py
utils.py
--- a/profile/forms.py	Fri Jan 07 08:56:24 2011 +0530
+++ b/profile/forms.py	Fri Jan 07 09:01:27 2011 +0530
@@ -76,6 +76,7 @@
                               gender=self.cleaned_data['gender'],
                               address=self.cleaned_data['address']
                               phonenum=self.cleaned_data['phonenum'],
+                              uniq_key=make_key(Profile),
                              )
         new_profile.save()
         
--- a/utils.py	Fri Jan 07 08:56:24 2011 +0530
+++ b/utils.py	Fri Jan 07 09:01:27 2011 +0530
@@ -1,8 +1,13 @@
 import string,random
 
-def make_key():
+def make_key(model_name):
     """ return a 10 character random key.
     """
-    
-    return ''.join([ random.choice(string.uppercase+string.digits) for i in range(10)])
 
+    while True:
+        key = ''.join([ random.choice(string.uppercase+string.digits) for i in range(10)])
+        try:
+            model_name.objects.get(uniq_key=key)
+        except model_name.DoesNotExist:
+            return key
+