Created
October 17, 2013 17:32
-
-
Save rafaelcanovas/7028987 to your computer and use it in GitHub Desktop.
bit.ly templatetag for Django
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import bitly_api | |
from django import template | |
from django.conf import settings | |
from django.core.exceptions import ImproperlyConfigured | |
register = template.Library() | |
try: | |
BITLY_LOGIN = settings.BITLY_LOGIN | |
BITLY_API_KEY = settings.BITLY_API_KEY | |
except AttributeError: | |
raise ImproperlyConfigured('Missing bitly login or api key') | |
@register.filter | |
def bitly_shorten(url): | |
bitly = bitly_api.Connection(BITLY_LOGIN, BITLY_API_KEY) | |
try: | |
return bitly.shorten(url)['url'] | |
except: | |
return '' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment