Login

RSS feed authentication

Author:
rileycrane
Posted:
July 15, 2009
Language:
Python
Version:
1.0
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. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.