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
environments.

@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/models.py 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 secret.py.template in settings/ to secret.py. 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="srct.gmu.edu", alphabet=(string.ascii_lowercase + string.digits))
hashids_counter = None
class URL(models.Model):
......@@ -37,24 +37,20 @@ class URL(models.Model):
@staticmethod
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())
cache.incr("hashids_counter")
short = hashids.encrypt(cache.get("hashids_counter"))
tries = 1
while tries < 100:
try:
urls = URL.objects.get(short__iexact=short)
tries += 1
hashids_counter += 1
cache.incr("hashids_counter")
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