Permalink
Browse files

Got rid of is_nonunique (what was I thinking?) in Shurl in favor of t…

…he get_or_create() function already in Domain
  • Loading branch information...
1 parent 78e1d65 commit 35cae1fda33c7a42a950cd96c5b36db5965824b1 @aldeka committed Apr 29, 2012
Showing with 22 additions and 22 deletions.
  1. +11 −13 shortcake/models.py
  2. +1 −1 shortcake/templates/thanks.html
  3. +4 −3 shortcake/tests.py
  4. +3 −4 shortcake/views.py
  5. +3 −1 urls.py
View
@@ -1,5 +1,5 @@
from django.db import models
-from django.forms import ModelForm
+from django import forms
import datetime
import string
@@ -52,7 +52,7 @@ def get_or_create(url):
class Shurl(models.Model):
'''Model for a shortened URL.
"No I'm not joking, and don't call me Shirley!"'''
- url = models.URLField(unique=True,verbose_name="URL")
+ url = models.URLField(verbose_name="URL")
short_suffix = models.CharField(max_length=20,unique=True,**optional)
access_count = models.IntegerField(default=0)
creation_time = models.DateTimeField(auto_now_add=True)
@@ -83,17 +83,17 @@ def shortening_algo(n):
return convert_to_base_64(n)
@staticmethod
- def is_nonunique(url):
- '''Tests to see if there's already a short url for this url. If so, returns the other object. If not, returns False'''
+ def get_or_create(url):
+ '''Returns a Shurl object, either old or freshly created, based on a url'''
# TODO: make this cleverer about duplicate-detection -- #s, ? arguments, www v. no www, etcetera
# Even better: include function to identify common other url shortener services' urls, follow where they lead, and return the "real" url that they lead to. t.co, I'm looking at you...
try:
- if url[-1] == '/':
- url = url[:len(url)-1]
- s = Shurl.objects.get(url='http://' + url + '/')
- return s
+ s = Shurl.objects.get(url=url)
except Shurl.DoesNotExist:
- return False
+ s = Shurl(url=url)
+ s.save()
+ s.assign_short_suffix()
+ return s
def get_url(self):
'''Ups the access_count of the shurl, the shurl's domain, and the relevant month's log before returning the url.
@@ -110,11 +110,9 @@ def get_url(self):
log.save()
return self.url
-class ShurlForm(ModelForm):
+class ShurlForm(forms.Form):
'''Form for making a new shortened URL'''
- class Meta:
- model=Shurl
- fields = ('url',)
+ url = forms.URLField()
class MonthLog(models.Model):
'''Model for a log of accesses for a given domain in a given month.'''
@@ -14,7 +14,7 @@
``"""=========="""``
</pre>
-<p>Here's your shortened URL: <a href="{{ shurl.short_url }}">{{ shurl.short_url }}</a></p>
+<p>Here's your shortened URL: <a href="{{ shurl.short_suffix }}">{{ shurl.short_url }}</a></p>
<p><a href="/">&laquo; Back</a></p>
{% endblock %}
View
@@ -14,7 +14,6 @@ def test_basic_addition(self):
class ShurlCreationTest(TestCase):
def setUp(self):
- self.client = Client()
s = Shurl(url='http://www.test.com')
s.save()
s.assign_short_suffix()
@@ -39,6 +38,10 @@ def test(self):
yet_another_s = Shurl.objects.get(pk=63)
self.assertEqual(yet_another_s.short_url(),'cak.es/_')
+ # test duplication handling code
+ t = Shurl.get_or_create('http://www.test.com')
+ self.assertEqual(t.pk,1)
+
class Base64Test(TestCase):
def test_shortening_algorithm(self):
self.assertEqual('9',Shurl.shortening_algo(9))
@@ -49,7 +52,6 @@ def test_shortening_algorithm(self):
class LoggingTest(TestCase):
def setUp(self):
- self.client = Client()
s = Shurl(url='http://www.test.com')
s.save()
s.assign_short_suffix()
@@ -87,7 +89,6 @@ def test(self):
class DomainTest(TestCase):
def setUp(self):
- self.client = Client()
s = Shurl(url='http://www.test.com/a/b/c/')
s.save()
s.assign_short_suffix()
View
@@ -17,10 +17,9 @@ def home(request):
else:
# handle post from form
form = ShurlForm(request.POST)
- shurl = Shurl.is_nonunique(request.POST['url'])
- if not shurl:
- shurl = form.save()
- shurl.assign_short_suffix()
+ form.is_valid()
+ # get_or_create does dup detection for us
+ shurl = Shurl.get_or_create(form.cleaned_data['url'])
return render_to_response('thanks.html',
{'shurl':shurl,},
context_instance=RequestContext(request))
View
@@ -13,8 +13,10 @@
url(r'^api/popular/$', 'shortcake.views.top_ten', name='top_ten'),
url(r'^api/(?P<short_suffix>.+)/accesses/$', 'shortcake.views.shurl_accesses', name='shurl_accesses'),
url(r'^api/(?P<short_suffix>.+)/stats/$', 'shortcake.views.shurl_stats', name='shurl_stats'),
- # dangerous: this matches everything!
+ # dangerous: this matches everything after api!
url(r'^api/(?P<short_suffix>.+)/$', 'shortcake.views.forward', name='forward'),
+ # even more dangerous: this matches everything!
+ url(r'^(?P<short_suffix>.+)/$', 'shortcake.views.forward', name='forward'),
# url(r'^mysite/', include('mysite.foo.urls')),

0 comments on commit 35cae1f

Please sign in to comment.