Created
January 4, 2017 16:35
-
-
Save ddahan/e69f4406ef9a45c8ab6065331046031a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@classmethod | |
def gen_id(cls): | |
''' Retourne un ID (unique donc) généré aléatoirement entre first_id et | |
last_id. Est appelé à chaque fois qu'une carte est générée. WARN : les | |
performances de cette méthode se dégraderont au fil du nombre d'objets.Les bornes pourront être changées si cela devient un problème.''' | |
first_id = 10000000000 # min = 10000000000 (11 digits) | |
last_id = 100000000000 # max = 99999999999 (11 digits) | |
while True: # WARN : attention aux risques de boucles infinies | |
# TODO : ajouter un timeout | |
the_id = random.randrange(first_id, last_id) # inclus / exclu | |
queryset = cls.objects.filter(pk=the_id) | |
if not queryset.exists(): # ID non utilisé, on peut le prendre | |
return the_id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment