Use get_random_string everywhere (#210)

Django's get_random_string tries really hard to either use sysrandom or
be otherwise as unpredictable as possible. Thanks to David Gullasch for
pointing out both the problem and the solution.
This commit is contained in:
Tobias Kunze
2016-08-29 19:10:01 +02:00
committed by Raphael Michel
parent e440782545
commit 4a02ed566f
4 changed files with 10 additions and 13 deletions

View File

@@ -1,8 +1,7 @@
import random
from django.conf import settings
from django.core.exceptions import ValidationError
from django.db import models
from django.utils.crypto import get_random_string
from django.utils.translation import ugettext_lazy as _
from .base import LoggedModel
@@ -14,7 +13,7 @@ from .orders import CartPosition, OrderPosition
def generate_code():
charset = list('ABCDEFGHKLMNPQRSTUVWXYZ23456789')
while True:
code = "".join([random.choice(charset) for i in range(settings.ENTROPY['voucher_code'])])
code = get_random_string(length=settings.ENTROPY['voucher_code'], allowed_chars=charset)
if not Voucher.objects.filter(code=code).exists():
return code