diff options
| author | Ben Firshman | 2010-01-25 01:00:07 +0000 | 
|---|---|---|
| committer | Ben Firshman | 2014-09-21 17:08:55 -0700 | 
| commit | c22f6b46e9faecc4d83c6dc575f0179796fc0c1e (patch) | |
| tree | 95568dfcbd0f8aab24020f37b353aad4cbe484d0 | |
| parent | eb1e1c2feddb282c74dc597c4f51c5ccfe5c7fe1 (diff) | |
| download | django-shorturls-c22f6b46e9faecc4d83c6dc575f0179796fc0c1e.tar.bz2 | |
Added SHORTURLS_DEFAULT_CONVERTER setting so template tag converter can be specified
| -rw-r--r-- | README.md | 13 | ||||
| -rw-r--r-- | src/shorturls/__init__.py | 19 | ||||
| -rw-r--r-- | src/shorturls/templatetags/shorturl.py | 4 | ||||
| -rw-r--r-- | src/shorturls/views.py | 4 | 
4 files changed, 36 insertions, 4 deletions
| @@ -87,3 +87,16 @@ differ.  If not defined, the redirect view will try to guess the proper domain by  consulting the ``django.contrib.sites`` framework, if installed, or the  requested domain, if not. + +#### ``SHORTURLS_DEFAULT_CONVERTER`` + +The converter that is used to translate between short URLs and model IDs. +Defaults to the built in base 62 conversion. + +Available converters: + + - `shorturls.baseconv.base62` Base 62 encoding. + - `shorturls.baseconv.base32` [Douglas Crockford's base 32.](http://www.crockford.com/wrmg/base32.html) + - `shorturls.baseconv.hexconv` Hex encoding. + - `shorturls.baseconv.bin` Binary encoding, because why not. + diff --git a/src/shorturls/__init__.py b/src/shorturls/__init__.py index e69de29..528e838 100644 --- a/src/shorturls/__init__.py +++ b/src/shorturls/__init__.py @@ -0,0 +1,19 @@ +from django.conf import settings +from django.core.exceptions import ImproperlyConfigured +from django.utils.importlib import import_module +from shorturls import baseconv + +default_converter = baseconv.base62 + +if hasattr(settings, 'SHORTURLS_DEFAULT_CONVERTER'): +    mod_name, conv_name = settings.SHORTURLS_DEFAULT_CONVERTER.rsplit('.', 1) +    try: +        mod = import_module(mod_name) +    except ImportError, e: +        raise ImproperlyConfigured('Could not load converter specified by SHORTURLS_DEFAULT_CONVERTER. Error was: %s' % e) +    try: +        default_converter = getattr(mod, conv_name) +    except AttributeError: +        raise ImproperlyConfigured('Could not load converter specified by SHORTURLS_DEFAULT_CONVERTER. %s is not in %s.' % (conv_name, mod)) + +    
\ No newline at end of file diff --git a/src/shorturls/templatetags/shorturl.py b/src/shorturls/templatetags/shorturl.py index e1781cc..4c98d87 100644 --- a/src/shorturls/templatetags/shorturl.py +++ b/src/shorturls/templatetags/shorturl.py @@ -3,7 +3,7 @@ from django import template  from django.conf import settings  from django.core import urlresolvers  from django.utils.safestring import mark_safe -from shorturls.baseconv import base62 +from shorturls import default_converter as converter  class ShortURL(template.Node):      @classmethod @@ -27,7 +27,7 @@ class ShortURL(template.Node):          except (AttributeError, KeyError):              return '' -        tinyid = base62.from_decimal(obj.pk) +        tinyid = converter.from_decimal(obj.pk)          if hasattr(settings, 'SHORT_BASE_URL') and settings.SHORT_BASE_URL:              return urlparse.urljoin(settings.SHORT_BASE_URL, prefix+tinyid) diff --git a/src/shorturls/views.py b/src/shorturls/views.py index 2d0ef1a..2d0a47b 100644 --- a/src/shorturls/views.py +++ b/src/shorturls/views.py @@ -4,9 +4,9 @@ from django.contrib.sites.models import Site, RequestSite  from django.db import models  from django.http import HttpResponsePermanentRedirect, Http404  from django.shortcuts import get_object_or_404 -from shorturls.baseconv import base62 +from shorturls import default_converter -def redirect(request, prefix, tiny, converter=base62): +def redirect(request, prefix, tiny, converter=default_converter):      """      Redirect to a given object from a short URL.      """ | 
