diff options
| author | Teddy Wing | 2015-09-28 15:23:29 -0400 | 
|---|---|---|
| committer | Teddy Wing | 2015-09-28 15:23:29 -0400 | 
| commit | 02abc1c92a12b97cb21a20e82ff20d59c6fca562 (patch) | |
| tree | e380ecc28e38b6058391b72ca5ee6bd86cf29b6d /src | |
| parent | 71caf877ebdfc51aafae48ff25b6d4da2d072c3d (diff) | |
| download | django-shorturls-02abc1c92a12b97cb21a20e82ff20d59c6fca562.tar.bz2 | |
Create `ShortURLConverter` classextract-shortener-to-class
Extract URL shortening functionality into a new class. This moves the
work outside of the `ShortURL` template tag class and into a new
`ShortURLConverter` class.
This allows us to ask for short URLs outside the context of the template
tag.
It now becomes possible to find the short URL of an object in other
places, like an API.
Diffstat (limited to 'src')
| -rw-r--r-- | src/shorturls/short_url_converter.py | 37 | ||||
| -rw-r--r-- | src/shorturls/templatetags/shorturl.py | 35 | 
2 files changed, 43 insertions, 29 deletions
| diff --git a/src/shorturls/short_url_converter.py b/src/shorturls/short_url_converter.py new file mode 100644 index 0000000..2fabfbc --- /dev/null +++ b/src/shorturls/short_url_converter.py @@ -0,0 +1,37 @@ +import urlparse +from django.conf import settings +from django.core import urlresolvers +from shorturls import default_converter as converter + + +class ShortURLConverter(object): +    def __init__(self, obj): +        self.obj = obj + +    def shorten(self): +        try: +            prefix = self.get_prefix(self.obj) +        except (AttributeError, KeyError): +            return '' + +        tinyid = converter.from_decimal(self.obj.pk) + +        if hasattr(settings, 'SHORT_BASE_URL') and settings.SHORT_BASE_URL: +            return urlparse.urljoin(settings.SHORT_BASE_URL, prefix+tinyid) + +        try: +            return urlresolvers.reverse('shorturls.views.redirect', kwargs={ +                'prefix': prefix, +                'tiny': tinyid +            }) +        except urlresolvers.NoReverseMatch: +            return '' + +    def get_prefix(self, model): +        if not hasattr(self.__class__, '_prefixmap'): +            self.__class__._prefixmap = dict( +                (m, p) for p, m in settings.SHORTEN_MODELS.items()) +        key = '%s.%s' % ( +            model._meta.app_label, +            model.__class__.__name__.lower()) +        return self.__class__._prefixmap[key] diff --git a/src/shorturls/templatetags/shorturl.py b/src/shorturls/templatetags/shorturl.py index 4c98d87..1865791 100644 --- a/src/shorturls/templatetags/shorturl.py +++ b/src/shorturls/templatetags/shorturl.py @@ -1,9 +1,6 @@ -import urlparse  from django import template -from django.conf import settings -from django.core import urlresolvers  from django.utils.safestring import mark_safe -from shorturls import default_converter as converter +from shorturls.short_url_converter import ShortURLConverter  class ShortURL(template.Node):      @classmethod @@ -21,31 +18,11 @@ class ShortURL(template.Node):              obj = self.obj.resolve(context)          except template.VariableDoesNotExist:              return '' -             -        try: -            prefix = self.get_prefix(obj) -        except (AttributeError, KeyError): -            return '' -         -        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) -         -        try: -            return urlresolvers.reverse('shorturls.views.redirect', kwargs = { -                'prefix': prefix, -                'tiny': tinyid -            }) -        except urlresolvers.NoReverseMatch: -            return '' -             -    def get_prefix(self, model): -        if not hasattr(self.__class__, '_prefixmap'): -            self.__class__._prefixmap = dict((m,p) for p,m in settings.SHORTEN_MODELS.items()) -        key = '%s.%s' % (model._meta.app_label, model.__class__.__name__.lower()) -        return self.__class__._prefixmap[key] -         + +        shortener = ShortURLConverter(obj) +        return shortener.shorten() + +  class RevCanonical(ShortURL):      def render(self, context):          url = super(RevCanonical, self).render(context) | 
