Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 7a41e33a9463a54eb9a62be2a38671fec5a63d96 @jjdelc committed May 10, 2009
@@ -0,0 +1,6 @@
+Django external links was created by Jesús Del Carpio (Jj)
+ Contact: jjdelc@gmail.com
+ http://isgeek.net/
+
+django-external-links:
+ http://github.com/jjdelc/django-external-links
8 README
@@ -0,0 +1,8 @@
+=====================
+Django External links
+=====================
+
+A generic application to keep track of the clicks on the external links
+in your sites
+
+
@@ -0,0 +1,80 @@
+=====================
+Django External links
+=====================
+
+A generic application to keep track of the clicks on the external links
+in your sites
+
+Installation
+============
+
+1. Get the code
+ - Get a tarball
+ - Pull from the Github repo:
+ http://github.com/jjdelc/django-external-links
+
+2. Add 'external_links' in your PYTHONPATH
+ - Add a symlink to your proj dir
+ - Put it you your python site-packages
+ - Anything else that does it
+
+3. Add 'external_links' to your INSTALLED_APPS
+
+4. Run ./manage.py syncdb
+ [proj] ./manage.py syncdb
+ Creating table external_links_linkclick
+ Installing index for external_links.LinkClick model
+
+5. Add the UrlConf somewhere in your URLs
+ urlpatterns += patterns(''
+ url(r'^external/', include('external_links.urls')),
+ )
+
+
+Keeping track of links
+======================
+Every external link on the site should actually be a redirection to
+the external link endpoint, which will keep track of the click and
+perform the redirection to the final site
+
+Reference
+=========
+
+TemplateTag
+~~~~~~~~~~~
+ This is what you will be using the most, add
+ {% load external_link_tags %}
+ anywhere in your templates, now for each external link use
+ the {% external %} tag:
+ <a href="{% external "http://example.com" %}">Other site</a>
+
+ Which should translate to:
+ <a href="/external/?link=http%3A%2F%2example.com">Other site</a>
+
+
+Models
+~~~~~~
+ The LinkClick model represents a clicked link, it stores:
+ The logged user, or None if it's an annonymous session
+ The Date and Time
+ The referer page from where the click was made
+ The destination site
+ User's IP address
+
+ To use, call the 'store' method with the view's Request object
+ as parameter
+ >>> destination = request.GET['link']
+ >>> clicked_link = LinkClick(link=destination)
+ >>> clicked_link.store(request)
+
+ But you shouldn't need to do that, it's all done in the view
+
+
+Views
+~~~~~
+ There's an external_link view that registers all the links
+ that go through the external_links redirection.
+
+ If the view is sent with no "link" GET param, it will default
+ to '/'
+
@@ -0,0 +1 @@
+VERSION = (0, 1, 'pre')
@@ -0,0 +1,32 @@
+# -*- coding: utf-8 -*-
+
+from django.db import models
+
+from django.contrib.auth.models import User
+
+class LinkClick(models.Model):
+ """
+ Represents a click on an external link.
+ Usage:
+ clicked_link = LinkClick(link_url)
+ clicked_link.store(request)
+ """
+ user = models.ForeignKey(User, null=True)
+ link = models.CharField(max_length=512)
+ referer = models.CharField(max_length=512)
+ ip_addr = models.IPAddressField()
+ date = models.DateField(auto_now=True)
+ time = models.TimeField(auto_now=True)
+
+ def store(self, request):
+ """
+ Update params based on Request object
+ """
+ user = None
+ if request.user.is_authenticated():
+ user = request.user
+
+ self.user = user
+ self.referer = request.META.get('HTTP_REFERER','')
+ self.ip_addr = request.META['REMOTE_ADDR']
+ self.save()
No changes.
@@ -0,0 +1,21 @@
+# -*- coding: utf-8 -*-
+
+"""
+ External links template tags
+"""
+
+from urllib import quote
+
+from django.template import Library
+from django.core.urlresolvers import reverse
+
+register = Library()
+
+@register.simple_tag
+def external(link):
+ """
+ Replaces an external link with a redirect to
+ keep track of the clicked link
+ """
+ redirect_endpoint = reverse('external_link')
+ return redirect_endpoint + '?link=' + quote(link, safe='')
No changes.
@@ -0,0 +1,9 @@
+# -*- coding: utf-8 -*-
+
+from django.conf.urls.defaults import url, patterns
+
+urlpatterns = patterns('external_links.views',
+ url(r'^$',
+ 'external_link',
+ name='external_link')
+)
@@ -0,0 +1,21 @@
+# -*- coding: utf-8 -*-
+
+from django.http import HttpResponseRedirect
+
+from external_links.models import LinkClick
+
+def external_link(request):
+ """
+ Redirects links and keeps track of them
+ """
+ try:
+ link = request.GET['link']
+ link_click = LinkClick(link=link)
+ link_click.store(request)
+ except KeyError:
+ # Someone got here without the link param
+ # Redirect to Home as default
+ link = '/'
+
+ return HttpResponseRedirect(link)
+

0 comments on commit 7a41e33

Please sign in to comment.