Use SystemRandom to generate unpredictable random slugs. Fixed duplicate characters in the choice string, removed iI from it to prevent confusion. Fixes issue #40.

This commit is contained in:
Martin Mahner 2014-01-07 12:46:57 +01:00
parent 5a3b726e31
commit c687ab125a

View file

@ -1,19 +1,22 @@
import datetime from datetime import datetime
import random from os import urandom
import mptt from random import SystemRandom
from django.db import models from django.db import models
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.conf import settings from django.conf import settings
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
import mptt
from dpaste.highlight import LEXER_DEFAULT from dpaste.highlight import LEXER_DEFAULT
R = SystemRandom()
L = getattr(settings, 'DPASTE_SLUG_LENGTH', 4) L = getattr(settings, 'DPASTE_SLUG_LENGTH', 4)
T = getattr(settings, 'DPASTE_SLUG_CHOICES', T = getattr(settings, 'DPASTE_SLUG_CHOICES',
'abcdefghijkmnopqrstuvwwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ1234567890') 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ1234567890')
def generate_secret_id(length=L): def generate_secret_id(length=L):
return ''.join([random.choice(T) for i in range(length)]) return ''.join([R.choice(T) for i in range(L)])
class Snippet(models.Model): class Snippet(models.Model):
secret_id = models.CharField(_(u'Secret ID'), max_length=255, blank=True) secret_id = models.CharField(_(u'Secret ID'), max_length=255, blank=True)
@ -36,7 +39,7 @@ class Snippet(models.Model):
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
if not self.pk: if not self.pk:
self.published = datetime.datetime.now() self.published = datetime.now()
self.secret_id = generate_secret_id() self.secret_id = generate_secret_id()
super(Snippet, self).save(*args, **kwargs) super(Snippet, self).save(*args, **kwargs)