Commit 9c45114c authored by David Haynes's avatar David Haynes

foreign key points to registereduser now

- I remember :dbond: saying a foreignkey to User could be a bad idea
parent 80ebfb8e
......@@ -12,47 +12,6 @@ from hashids import Hashids
hashids = Hashids(salt="srct.gmu.edu", alphabet=(string.ascii_lowercase + string.digits))
class URL(models.Model):
"""
This model represents a stored URL redirection rule. Each URL has an
owner, target url, short identifier, click counter, and expiration
date.
"""
owner = models.ForeignKey(User)
date_created = models.DateTimeField(default=timezone.now)
target = models.URLField(max_length=1000)
short = models.SlugField(primary_key=True, max_length=20)
clicks = models.IntegerField(default=0)
qrclicks = models.IntegerField(default=0)
socialclicks = models.IntegerField(default=0)
expires = models.DateTimeField(blank=True, null=True)
def __unicode__(self):
return '<%s : %s>' % (self.owner.username, self.target)
class Meta:
ordering = ['short']
@staticmethod
def generate_valid_short():
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
cache.incr("hashids_counter")
except URL.DoesNotExist:
return short
return None
class RegisteredUser(models.Model):
"""
This is simply a wrapper model which, if an object exists, indicates
......@@ -93,3 +52,44 @@ class RegisteredUser(models.Model):
def handle_regUser_creation(sender, instance, created, **kwargs):
if created:
RegisteredUser.objects.create(user=instance)
class URL(models.Model):
"""
This model represents a stored URL redirection rule. Each URL has an
owner, target url, short identifier, click counter, and expiration
date.
"""
owner = models.ForeignKey(RegisteredUser)
date_created = models.DateTimeField(default=timezone.now)
target = models.URLField(max_length=1000)
short = models.SlugField(primary_key=True, max_length=20)
clicks = models.IntegerField(default=0)
qrclicks = models.IntegerField(default=0)
socialclicks = models.IntegerField(default=0)
expires = models.DateTimeField(blank=True, null=True)
def __unicode__(self):
return '<%s : %s>' % (self.owner.username, self.target)
class Meta:
ordering = ['short']
@staticmethod
def generate_valid_short():
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
cache.incr("hashids_counter")
except URL.DoesNotExist:
return short
return None
......@@ -31,7 +31,7 @@
<a href="{{url.target}}">{{url.target}}</a>
<br />
{% if url.owner == request.user.registereduser.user and request.user.registereduser.approved == True %}
{% if url.owner == request.user.registereduser and request.user.registereduser.approved == True %}
<strong>Clicks:</strong>
{{url.clicks}}
<br />
......
......@@ -41,7 +41,7 @@ def index(request):
# We don't commit the url object yet because we need to add its
# owner, and parse its date field.
url = url_form.save(commit=False)
url.owner = request.user.registereduser.user
url.owner = request.user.registereduser
# If the user entered a short url, it's already been validated,
# so accept it. If they did not, however, then generate a
......@@ -116,7 +116,7 @@ def my_links(request):
if not request.user.registereduser.approved:
return render(request, 'not_registered.html')
urls = URL.objects.filter(owner=request.user.registereduser.user)
urls = URL.objects.filter(owner=request.user.registereduser)
domain = "%s://%s" % (request.scheme, request.META.get('HTTP_HOST')) + "/"
......@@ -138,7 +138,7 @@ def delete(request, short):
return render(request, 'not_registered.html')
url = get_object_or_404(URL, short__iexact=short)
if url.owner == request.user.registereduser.user:
if url.owner == request.user.registereduser:
url.delete()
return redirect('my_links')
else:
......
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