Secure Source of Randomness

This commit is contained in:
pixeebot[bot]
2024-12-26 03:11:52 +00:00
committed by GitHub
parent 3f7296d7a5
commit 304812d43f
3 changed files with 10 additions and 10 deletions

View File

@@ -2,22 +2,22 @@ from django.contrib.auth.models import AbstractUser
from django.db import models
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
import random
from PIL import Image, ImageDraw, ImageFont
from io import BytesIO
import base64
import os
import secrets
def generate_random_id(model_class, id_field):
"""Generate a random ID starting at 4 digits, expanding to 5 if needed"""
while True:
# Try to get a 4-digit number first
new_id = str(random.randint(1000, 9999))
new_id = str(secrets.SystemRandom().randint(1000, 9999))
if not model_class.objects.filter(**{id_field: new_id}).exists():
return new_id
# If all 4-digit numbers are taken, try 5 digits
new_id = str(random.randint(10000, 99999))
new_id = str(secrets.SystemRandom().randint(10000, 99999))
if not model_class.objects.filter(**{id_field: new_id}).exists():
return new_id