utils.py
author Nishanth Amuluru <nishanth@fossee.in>
Fri, 07 Jan 2011 09:01:27 +0530
changeset 253 beb830b0e744
parent 251 1773b48559e1
permissions -rw-r--r--
modified make_key and used it in the form
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
251
1773b48559e1 Added make_key to utils
Nishanth Amuluru <nishanth@fossee.in>
parents: 250
diff changeset
     1
import string,random
1773b48559e1 Added make_key to utils
Nishanth Amuluru <nishanth@fossee.in>
parents: 250
diff changeset
     2
253
beb830b0e744 modified make_key and used it in the form
Nishanth Amuluru <nishanth@fossee.in>
parents: 251
diff changeset
     3
def make_key(model_name):
251
1773b48559e1 Added make_key to utils
Nishanth Amuluru <nishanth@fossee.in>
parents: 250
diff changeset
     4
    """ return a 10 character random key.
1773b48559e1 Added make_key to utils
Nishanth Amuluru <nishanth@fossee.in>
parents: 250
diff changeset
     5
    """
1773b48559e1 Added make_key to utils
Nishanth Amuluru <nishanth@fossee.in>
parents: 250
diff changeset
     6
253
beb830b0e744 modified make_key and used it in the form
Nishanth Amuluru <nishanth@fossee.in>
parents: 251
diff changeset
     7
    while True:
beb830b0e744 modified make_key and used it in the form
Nishanth Amuluru <nishanth@fossee.in>
parents: 251
diff changeset
     8
        key = ''.join([ random.choice(string.uppercase+string.digits) for i in range(10)])
beb830b0e744 modified make_key and used it in the form
Nishanth Amuluru <nishanth@fossee.in>
parents: 251
diff changeset
     9
        try:
beb830b0e744 modified make_key and used it in the form
Nishanth Amuluru <nishanth@fossee.in>
parents: 251
diff changeset
    10
            model_name.objects.get(uniq_key=key)
beb830b0e744 modified make_key and used it in the form
Nishanth Amuluru <nishanth@fossee.in>
parents: 251
diff changeset
    11
        except model_name.DoesNotExist:
beb830b0e744 modified make_key and used it in the form
Nishanth Amuluru <nishanth@fossee.in>
parents: 251
diff changeset
    12
            return key
beb830b0e744 modified make_key and used it in the form
Nishanth Amuluru <nishanth@fossee.in>
parents: 251
diff changeset
    13