Commit fbd55072 authored by David Haynes's avatar David Haynes 🙆
Browse files

Merge branch 'fixhashids' into 'master'

Moved hashids_counter to the cache

The global caused errors on unmigrated databases, and it could
potentially cause threading errors in larger scale production

@dbond2 please tell me if this is a bad idea

See merge request !18
parents 0e2ef2e5 8299025a
......@@ -119,9 +119,6 @@ Exit the mysql shell by typing:
`> exit`
### Additional Setup
For a currently undetermined reason you will need to comment out line 55 of go/go/go/ to avoid the app from literally exploding:
`# hashids_counter = URL.objects.count()`
Now, to configure your newly created database with the project settings, and set up your project's cryptographic key, copy the in settings/ to Follow the comment instructions provided in each file to set your secret key and database info.
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