aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--MANIFEST.in2
-rw-r--r--README.rst142
-rw-r--r--brevisurl/templatetags/brevisurltags.py28
-rw-r--r--brevisurl/tests/templatetags/test_brevisurltags.py15
-rw-r--r--setup.py42
6 files changed, 226 insertions, 4 deletions
diff --git a/.gitignore b/.gitignore
index bab1113..972d7d1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,3 +6,4 @@
pip-log.txt*
*.swn
/dist/
+/django_brevisurl.egg-info/
diff --git a/MANIFEST.in b/MANIFEST.in
new file mode 100644
index 0000000..b1eaf85
--- /dev/null
+++ b/MANIFEST.in
@@ -0,0 +1,2 @@
+include README.rst
+include LICENSE \ No newline at end of file
diff --git a/README.rst b/README.rst
index e69de29..02a0720 100644
--- a/README.rst
+++ b/README.rst
@@ -0,0 +1,142 @@
+django-brevisurl
+================
+
+django-brevisurl is django app for shortening urls. Brevis is a latin word, which means
+short, so the name brevisurl == shorturl or url shortener. The actual creating of short
+url is handled by the shortening backend.
+
+
+Requirements
+------------
+
+- python 2.7+
+- django
+
+
+Installation
+------------
+
+Install via pypi or copy this module into your project or into your PYTHONPATH.
+
+
+**Put brevisurl into INSTALLED_APPS in your projects settings.py file**
+
+ INSTALLED_APPS = (
+ 'localeurl',
+ 'django.contrib.auth',
+ 'django.contrib.contenttypes',
+ 'django.contrib.sessions',
+ 'django.contrib.sites',
+ 'django.contrib.admin',
+ 'django.contrib.sitemaps',
+ 'web',
+ 'debug_toolbar',
+ 'rosetta',
+ 'south',
+ 'brevisurl'
+ )
+
+
+**Run syncdb command to create database tables from brevisurl models**
+
+::
+
+ python manage.py syncdb
+
+
+Configuration
+-------------
+
+**django settings.py constants**
+
+::
+
+ # Setting for default brevis backend
+ BREVISURL_BACKEND = 'brevisurl.backends.local.BrevisUrlBackend' # Default is 'brevisurl.backends.local.BrevisUrlBackend'
+
+
+Examples
+--------
+
+**Example 1**
+
+Using programmatic approach
+
+::
+
+ from brevisurl import get_connection
+
+ connection = get_connection()
+ short_url_obj = connection.shorten_url('http://www.codescale.net/')
+ print short_url_obj.shortened_url
+
+
+**Example 2**
+
+Using programmatic approach with shortcut
+
+::
+
+ from brevisurl import shorten_url
+
+ shor_url_obj = shorten_url('http://www.codescale.net/')
+ print shor_url_obj.shortened_url
+
+
+**Example 3**
+
+Using brevisurl in templates via filter approach
+
+::
+
+ {% load brevisurl %}
+ {% url homepage as homepage_url %}
+ {{ homepage_url|shorten_url }}
+
+
+**Example 4**
+
+Using brevisurl in templates with filtered tag approach.
+brevisurl comes with special tag called `absurl` that works
+exactly the same as `url` django tag but prepends protocol + domain
+in front of resovled url path.
+
+::
+
+ {% load brevisurltags %}
+ {% absurl homepage as homepage_url %}
+ {{ homepage_url|shorten_url }}
+
+
+Tests
+-----
+
+**Tested on evnironment**
+
+- Xubuntu Linux 12.04 LTS precise 64-bit
+- python 2.7.3+
+- python unittest
+- django 1.4
+
+**Running tests**
+
+To run the test run command: ::
+
+ $ python manage.py test brevisurl
+
+
+
+Author
+------
+
+| char0n (Vladimír Gorej, CodeScale s.r.o.)
+| email: gorej@codescale.net
+| web: http://www.codescale.net
+
+
+References
+----------
+
+ - http://github.com/char0n/django-brevisurl
+ - http://pypi.python.org/pypi/django-brevisurl/
+ - http://www.codescale.net/en/community#django-brevisurl \ No newline at end of file
diff --git a/brevisurl/templatetags/brevisurltags.py b/brevisurl/templatetags/brevisurltags.py
index 2d883f0..e9cb847 100644
--- a/brevisurl/templatetags/brevisurltags.py
+++ b/brevisurl/templatetags/brevisurltags.py
@@ -1,11 +1,37 @@
+import urlparse
+
from django import template
+from django.template.defaulttags import URLNode, url
+from django.contrib.sites.models import Site
from brevisurl import shorten_url as shorten_url_util, get_connection
+
register = template.Library()
@register.filter
def shorten_url(original_url):
short_url = shorten_url_util(original_url, connection=get_connection(fail_silently=True))
- return short_url.shortened_url if short_url is not None else original_url \ No newline at end of file
+ return short_url.shortened_url if short_url is not None else original_url
+
+
+# Modified copy of django snippet #1518 (http://djangosnippets.org/snippets/1518/)
+class AbsoluteURLNode(URLNode):
+ def render(self, context):
+ path = super(AbsoluteURLNode, self).render(context)
+ domain = "http://{0}".format(Site.objects.get_current().domain)
+ if self.asvar:
+ context[self.asvar]= urlparse.urljoin(domain, context[self.asvar])
+ return ''
+ else:
+ return urlparse.urljoin(domain, path)
+
+def absurl(parser, token, node_cls=AbsoluteURLNode):
+ """Just like {% url %} but ads the domain of the current site."""
+ node_instance = url(parser, token)
+ return node_cls(view_name=node_instance.view_name,
+ args=node_instance.args,
+ kwargs=node_instance.kwargs,
+ asvar=node_instance.asvar)
+absurl = register.tag(absurl) \ No newline at end of file
diff --git a/brevisurl/tests/templatetags/test_brevisurltags.py b/brevisurl/tests/templatetags/test_brevisurltags.py
index de1ed00..38ccfcc 100644
--- a/brevisurl/tests/templatetags/test_brevisurltags.py
+++ b/brevisurl/tests/templatetags/test_brevisurltags.py
@@ -7,7 +7,7 @@ from brevisurl.models import ShortUrl
class TestShortenUrlTag(TestCase):
- def test_shorten_url_tag(self):
+ def test_shorten_url_filter(self):
original_url = 'http://www.codescale.net/'
url = Template("""
{% load brevisurltags %}
@@ -17,11 +17,20 @@ class TestShortenUrlTag(TestCase):
self.assertEqual(ShortUrl.objects.all()[0].original_url, original_url)
self.assertRegexpMatches(url, URLValidator.regex)
- def test_shorten_url_tag_invalid_url(self):
+ def test_shorten_url_filter_invalid_url(self):
original_url = 'www.codescale.'
url = Template("""
{% load brevisurltags %}
{{ url|shorten_url }}
""").render(Context({'url': original_url})).strip()
self.assertEqual(ShortUrl.objects.all().count(), 0)
- self.assertEqual(url, original_url) \ No newline at end of file
+ self.assertEqual(url, original_url)
+
+ def test_absurl_tag(self):
+ url = Template("""
+ {% load brevisurltags %}
+ {% absurl brevisurl_redirect token='12345' as brevis_url %}
+ {{ brevis_url|shorten_url }}
+ """).render(Context()).strip()
+ self.assertEqual(ShortUrl.objects.all().count(), 1)
+ self.assertRegexpMatches(url, URLValidator.regex) \ No newline at end of file
diff --git a/setup.py b/setup.py
new file mode 100644
index 0000000..96aa1aa
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,42 @@
+# -*- coding: utf-8 -*-
+import os
+from setuptools import setup, find_packages
+
+
+def read(fname):
+ """Utility function to read the README file.
+
+ Used for the long_description. It's nice, because now 1) we have a top level
+ README file and 2) it's easier to type in the README file than to put a raw
+ string in below ...
+
+ """
+ return open(os.path.join(os.path.dirname(__file__), fname)).read()
+
+
+setup(
+ name='django-brevisurl',
+ version='0.9',
+ description='django-brevisurl is django app for shortening urls',
+ long_description=read('README.rst'),
+ author=u'Vladimír Gorej',
+ author_email='gorej@codescale.net',
+ url='http://www.codescale.net/en/community#django-brevisurl',
+ download_url='http://github.com/char0n/django-brevisurl/tarball/master',
+ license='BSD',
+ keywords = 'url short shortener',
+ packages=find_packages('.'),
+ install_requires=['django'],
+ platforms='any',
+ classifiers=[
+ 'Development Status :: 4 - Beta',
+ 'Environment :: Web Environment',
+ 'Intended Audience :: Developers',
+ 'Intended Audience :: Information Technology',
+ 'License :: OSI Approved :: BSD License',
+ 'Operating System :: OS Independent',
+ 'Programming Language :: Python',
+ 'Topic :: Utilities',
+ 'Topic :: Internet :: WWW/HTTP'
+ ]
+) \ No newline at end of file