Commit 4a8076f8 authored by Jean Michel Rouly's avatar Jean Michel Rouly
Browse files

Merge branch 'hashid_generation' into 'dev'

Hashid generation

Use Hashids to generate shortUrls.

Right now we will hit collisions at a 50% rate at 10k urls assuming perfect randomness. Using hashids reduces the chance of collisions as the number of urls increase.

Hashids are guarenteed unique. The downside being that as the number of urls get larger, the hashids will get longer as well. The rate is exponential though with roughly number of characters as log base alphabet length of (id + seed).

See merge request !10
parents 7ca4267f 2e0107c8
......@@ -4,6 +4,9 @@ from django.utils import timezone
from django.conf import settings
import random, string
from hashids import Hashids
hashids = Hashids(salt="srct.gmu.edu", alphabet=(string.ascii_lowercase + string.digits))
hashids_counter = None
class URL( models.Model ):
"""
......@@ -20,6 +23,8 @@ class URL( models.Model ):
clicks = models.IntegerField( default = 0 )
expires = models.DateTimeField( blank = True, null = True )
def __unicode__(self):
return '<%s : %s>' % (self.owner.username, self.target)
......@@ -28,17 +33,21 @@ class URL( models.Model ):
@staticmethod
def generate_valid_short():
selection = string.ascii_lowercase + string.digits
tries = 0
while True:
short = ''.join(random.choice(selection) for i in range(5))
global hashids_counter
hashids_counter += 1
short = hashids.encrypt(hashids_counter)
tries = 1
while tries < 100:
try:
urls = URL.objects.get( short__iexact = short )
tries += 1
hashids_counter += 1
except URL.DoesNotExist:
return short
if tries > 100:
return None
return None
# this needs to be here instead of at the top because the model's manager must be available before this line
hashids_counter = URL.objects.count()
class RegisteredUser( models.Model ):
......
......@@ -5,6 +5,7 @@ django-auth-ldap==1.2.1
django-piwik==0.1
django-qrcode==0.3
django-simple-captcha==0.4.2
hashids==1.0.1
gunicorn==18.0
piwikapi==0.3
python-ldap==2.4.15
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment