Login

RSS feed authentication

Author:
rileycrane
Posted:
July 15, 2009
Language:
Python
Version:
1.0
Tags:
authentication feed rss simple
Score:
-1 (after 1 ratings)

This is a very simple way of getting authenticated RSS feeds in django, by slightly changing the django.contrib.syndication.views

1) copy django/contrib/syndication/views.py into mysite/feeds/views.py

2) replace the contents of that file with the snippet

3) any feeds which you require login just add them to the auth_required list. In this case I require login for /feeds/mystuff/ so I make auth_required = ['mystuff']

My directory structure is like this:

mysite/feeds/

views.py

feedmodels.py

feedmodels.py is just where you make your feed models (see http://docs.djangoproject.com/en/dev/ref/contrib/syndication/ where they give an example of "LatestEntries")

[urls.py] - this is what I add in urls.py

from mysite.feeds.feedmodels import Latest,MyPersonalStuff

feeds = {

'latest':Latest,

'mystuff':MyPersonalStuff,

}

(r'^feeds/(?P<url>.*)/$', 'mysite.feeds.views.feed', {'feed_dict': feeds}),

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# This is a slightly modified version of django.contrib.syndication.views
from django.contrib.syndication import feeds
from django.http import HttpResponse, HttpResponseRedirect, Http404

# Define which feeds require authentication
auth_required = ['mystuff']

def feed(request, url, feed_dict=None):
    if not feed_dict:
        raise Http404, "No feeds are registered."

    try:
        slug, param = url.split('/', 1)
    except ValueError:
        slug, param = url, ''

    # Check if feed requires authentication
    if slug in auth_required:
        # Yes: check authentication
        if request.user.is_authenticated():
            try:
                f = feed_dict[slug]
            except KeyError:
                raise Http404, "Slug %r isn't registered." % slug

            try:
                feedgen = f(slug, request).get_feed(param)
            except feeds.FeedDoesNotExist:
                raise Http404, "Invalid feed parameters. Slug %r is valid, but other parameters, or lack thereof, are not." % slug

            response = HttpResponse(mimetype=feedgen.mime_type)
            feedgen.write(response, 'utf-8')
            return response
        # No: redirect to login page
        else:
            # Redirect User to login page, and send them back to their feed after the provide their credentials
            return HttpResponseRedirect('/accounts/login/?next=%s' % request.path)
    # No authentication required
    else:
        try:
            f = feed_dict[slug]
        except KeyError:
            raise Http404, "Slug %r isn't registered." % slug

        try:
            feedgen = f(slug, request).get_feed(param)
        except feeds.FeedDoesNotExist:
            raise Http404, "Invalid feed parameters. Slug %r is valid, but other parameters, or lack thereof, are not." % slug

        response = HttpResponse(mimetype=feedgen.mime_type)
        feedgen.write(response, 'utf-8')
        return response

More like this

  1. Class Feeds DRY TemplateTag by gmandx 4 years, 9 months ago
  2. view by view basic authentication decorator by Scanner 7 years, 9 months ago
  3. HTTP (basic) auth enabled (new-style) syndication framework feed class by hupf 4 years, 2 months ago
  4. Media RSS generation for Photologue by ltvolks 5 years, 7 months ago
  5. iTunes Podcast RSS Feed by Kyle_Dickerson 4 years, 7 months ago

Comments

Please login first before commenting.