from django.http import HttpResponseRedirect
import settings


class FeedburnerMiddleware(object):
    '''
    Redirect the user to a feedburner feed for basic feeds
    '''
    def process_request(self, request):
        r = request.path.split('/')
        if not settings.FEEDBURNER or\
            not r[1] == 'feeds' or \
            not r[-2] in settings.FEEDBURNER[1]:
            return None
        
        if request.META['HTTP_USER_AGENT'].startswith('FeedBurner'):
            return None
        else:
            return HttpResponseRedirect('/'.join((
                'http://feedproxy.google.com', 
                settings.FEEDBURNER[0],
                '/'.join(r[3:-1]))
                ))