Commit db89a0f8 authored by Nicholas Anderson's avatar Nicholas Anderson
Browse files

Moved hashids_counter to the cache

The global caused errors on unmigrated databases, and it could
potentially cause threading errors in larger scale production
parent 0e2ef2e5
from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
from django.core.cache import cache
# from django.conf import settings
import string # random
from hashids import Hashids
hashids = Hashids(salt="", alphabet=(string.ascii_lowercase + string.digits))
hashids_counter = None
class URL(models.Model):
......@@ -37,24 +37,20 @@ class URL(models.Model):
def generate_valid_short():
global hashids_counter
hashids_counter += 1
short = hashids.encrypt(hashids_counter)
if cache.get("hashids_counter") == None:
cache.set("hashids_counter", URL.objects.count())
short = hashids.encrypt(cache.get("hashids_counter"))
tries = 1
while tries < 100:
urls = URL.objects.get(short__iexact=short)
tries += 1
hashids_counter += 1
except URL.DoesNotExist:
return short
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):
This is simply a wrapper model which, if an object exists, indicates
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