Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactoring the Entry.short_url property

  • Loading branch information...
commit 46bcd165d03583aef172e05b20ba94bc92c9db57 1 parent b515dd6
Julien Fache authored
2  zinnia/admin/entry.py
View
@@ -193,7 +193,7 @@ def get_actions(self, request):
del actions['make_mine']
if not settings.PING_DIRECTORIES:
del actions['ping_directories']
- if not settings.USE_TWITTER or not settings.USE_BITLY:
+ if not settings.USE_TWITTER:
del actions['make_tweet']
return actions
11 zinnia/models.py
View
@@ -22,7 +22,6 @@
import mptt
from tagging.fields import TagField
-from zinnia.settings import USE_BITLY
from zinnia.settings import UPLOAD_TO
from zinnia.settings import MARKUP_LANGUAGE
from zinnia.settings import ENTRY_TEMPLATES
@@ -34,6 +33,7 @@
from zinnia.managers import AuthorPublishedManager
from zinnia.managers import DRAFT, HIDDEN, PUBLISHED
from zinnia.moderator import EntryCommentModerator
+from zinnia.url_shortener import get_url_shortener
from zinnia.signals import ping_directories_handler
from zinnia.signals import ping_external_urls_handler
@@ -235,14 +235,7 @@ def comments_are_open(self):
@property
def short_url(self):
"""Return the entry's short url"""
- if not USE_BITLY:
- return False
-
- from django_bitly.models import Bittle
-
- bittle = Bittle.objects.bitlify(self)
- url = bittle and bittle.shortUrl or self.get_absolute_url()
- return url
+ return get_url_shortener()(self)
def __unicode__(self):
return '%s: %s' % (self.title, self.get_status_display())
4 zinnia/settings.py
View
@@ -59,8 +59,8 @@
F_MIN = getattr(settings, 'ZINNIA_F_MIN', 0.1)
F_MAX = getattr(settings, 'ZINNIA_F_MAX', 1.0)
-USE_BITLY = getattr(settings, 'ZINNIA_USE_BITLY',
- 'django_bitly' in settings.INSTALLED_APPS)
+URL_SHORTENER_BACKEND = getattr(settings, 'ZINNIA_URL_SHORTENER_BACKEND',
+ 'zinnia.url_shortener.backends.default')
STOP_WORDS = getattr(settings, 'ZINNIA_STOP_WORDS',
('able', 'about', 'across', 'after', 'all', 'almost',
24 zinnia/url_shortener/__init__.py
View
@@ -0,0 +1,24 @@
+"""Url shortener for Zinnia"""
+import warnings
+
+from django.utils.importlib import import_module
+from django.core.exceptions import ImproperlyConfigured
+
+from zinnia.settings import URL_SHORTENER_BACKEND
+from zinnia.url_shortener.backends.default import backend as default_backend
+
+
+def get_url_shortener():
+ """Return the selected url shortener backend"""
+ try:
+ backend_module = import_module(URL_SHORTENER_BACKEND)
+ backend = getattr(backend_module, 'backend')
+ except (ImportError, AttributeError):
+ warnings.warn('%s backend cannot be imported' % URL_SHORTENER_BACKEND,
+ RuntimeWarning)
+ backend = default_backend
+ except ImproperlyConfigured, e:
+ warnings.warn(str(e), RuntimeWarning)
+ backend = default_backend
+
+ return backend
1  zinnia/url_shortener/backends/__init__.py
View
@@ -0,0 +1 @@
+"""Shortlink backends for Zinnia"""
20 zinnia/url_shortener/backends/bitly.py
View
@@ -0,0 +1,20 @@
+"""Bit.ly url shortener backend for Zinnia"""
+from django.conf import settings
+from django.core.exceptions import ImproperlyConfigured
+
+try:
+ from django_bitly.models import Bittle
+except ImportError:
+ raise ImproperlyConfigured('django_bitly is not available')
+
+if not getattr(settings, 'BITLY_LOGIN', ''):
+ raise ImproperlyConfigured('You have to set a BITLY_LOGIN setting')
+
+if not getattr(settings, 'BITLY_API_KEY', ''):
+ raise ImproperlyConfigured('You have to set a BITLY_API_KEY setting')
+
+
+def backend(entry):
+ """Bit.ly url shortener backend for Zinnia"""
+ bittle = Bittle.objects.bitlify(entry)
+ return bittle.shortUrl
11 zinnia/url_shortener/backends/default.py
View
@@ -0,0 +1,11 @@
+"""Default url shortener backend for Zinnia"""
+from django.contrib.sites.models import Site
+from django.core.urlresolvers import reverse
+
+from zinnia.settings import PROTOCOL
+
+
+def backend(entry):
+ """Default url shortener backend for Zinnia"""
+ return '%s://%s%s' % (PROTOCOL, Site.objects.get_current().domain,
+ reverse('zinnia_entry_shortlink', args=[entry.pk]))
Please sign in to comment.
Something went wrong with that request. Please try again.