diff options
| author | Ben Firshman | 2010-01-25 00:03:58 +0000 |
|---|---|---|
| committer | Ben Firshman | 2014-09-21 17:07:11 -0700 |
| commit | dd385d25d42bc16251c9e755b72500fec519b262 (patch) | |
| tree | 3a1133fa0749ec25ea3cf6ca7cf282e37046cf5c | |
| parent | 967f4ddab7bb68bc1cbd10a30658c65fd67c9acb (diff) | |
| download | django-shorturls-dd385d25d42bc16251c9e755b72500fec519b262.tar.bz2 | |
The converter can now be chosen for the view, and more helpful 404s are thrown
| -rw-r--r-- | src/shorturls/tests/test_views.py | 1 | ||||
| -rw-r--r-- | src/shorturls/views.py | 23 |
2 files changed, 13 insertions, 11 deletions
diff --git a/src/shorturls/tests/test_views.py b/src/shorturls/tests/test_views.py index a2813f2..e2c7b63 100644 --- a/src/shorturls/tests/test_views.py +++ b/src/shorturls/tests/test_views.py @@ -1,5 +1,4 @@ from django.conf import settings -from django.http import Http404 from django.test import TestCase from shorturls.baseconv import base62 diff --git a/src/shorturls/views.py b/src/shorturls/views.py index d811fde..2d0ef1a 100644 --- a/src/shorturls/views.py +++ b/src/shorturls/views.py @@ -6,7 +6,7 @@ from django.http import HttpResponsePermanentRedirect, Http404 from django.shortcuts import get_object_or_404 from shorturls.baseconv import base62 -def redirect(request, prefix, tiny): +def redirect(request, prefix, tiny, converter=base62): """ Redirect to a given object from a short URL. """ @@ -16,15 +16,18 @@ def redirect(request, prefix, tiny): # any of that stuff goes wrong. try: app_label, model_name = settings.SHORTEN_MODELS[prefix].split('.') - try: - model = models.get_model(app_label, model_name) - except LookupError: - raise Http404('Bad model.') - if not model: - raise ValueError - id = base62.to_decimal(tiny) - except (AttributeError, ValueError, KeyError): - raise Http404('Bad prefix, model, SHORTEN_MODELS, or encoded ID.') + except KeyError: + raise Http404('Bad prefix.') + try: + model = models.get_model(app_label, model_name) + except LookupError: + model = False + if not model: + raise Http404('Bad model specified in SHORTEN_MODELS.') + try: + id = converter.to_decimal(tiny) + except ValueError: + raise Http404('Bad encoded ID.') # Try to look up the object. If it's not a valid object, or if it doesn't # have an absolute url, bail again. |
