From c687ab125af67d769afc781731b1a2b663a5bb2c Mon Sep 17 00:00:00 2001 From: Martin Mahner Date: Tue, 7 Jan 2014 12:46:57 +0100 Subject: [PATCH] Use SystemRandom to generate unpredictable random slugs. Fixed duplicate characters in the choice string, removed iI from it to prevent confusion. Fixes issue #40. --- dpaste/models.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/dpaste/models.py b/dpaste/models.py index e55be4f..3abef84 100644 --- a/dpaste/models.py +++ b/dpaste/models.py @@ -1,19 +1,22 @@ -import datetime -import random -import mptt +from datetime import datetime +from os import urandom +from random import SystemRandom from django.db import models from django.core.urlresolvers import reverse from django.conf import settings from django.utils.translation import ugettext_lazy as _ +import mptt + from dpaste.highlight import LEXER_DEFAULT +R = SystemRandom() L = getattr(settings, 'DPASTE_SLUG_LENGTH', 4) T = getattr(settings, 'DPASTE_SLUG_CHOICES', - 'abcdefghijkmnopqrstuvwwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ1234567890') + 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ1234567890') 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): 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): if not self.pk: - self.published = datetime.datetime.now() + self.published = datetime.now() self.secret_id = generate_secret_id() super(Snippet, self).save(*args, **kwargs)