Ticket #329: SimplerFeedConfiguration.txt

File SimplerFeedConfiguration.txt, 4.4 KB (added by jacob, 10 years ago)
Line 
1from django.core.rss import FeedConfiguration, register_feed
2from django.views.rss.rss import feed
3
4from django.models.auth import users
5from django.models.tags import tags, destinations, posts
6import queries
7
8class SimplerFeedConfiguration(FeedConfiguration):
9    """Normal `FeedConfiguration` objects are easy to deal with unless
10    your feed needs parameters, in which case things can get a little
11    complicated. `SimplerFeedConfiguration` doesn't add or subtract
12    power, but does rearrange and rename things to make them easier
13    to understand.
14   
15    Limitation: doesn't handle enclosures yet."""
16   
17    default_generator_parameters = {}
18    slug = None # that'll break nicely
19   
20    def __init__(self):
21        FeedConfiguration.__init__(
22                self,
23                slug = self.slug,
24                title_cb = self.title,
25                link_cb = self.link,
26                description_cb = self.description,
27                get_list_func_cb = self.generator_chooser,
28                get_list_kwargs = self.default_generator_parameters,
29                get_list_kwargs_cb = self.generator_parameters,
30                param_func = self.feed_victim,
31                param_kwargs_cb = self.feed_parameters)
32               
33    def item_generator(self, **kwargs):
34        """This is the default item generator function, which you should
35        either
36       
37        a) over-ride to return a list of items to insert into your
38           feed, or
39           
40        b) bypass by over-riding `generator_chooser` below.
41       
42        `kwargs` -- `default_generator_parameters` updated with the result
43                    result of `generator_parameters`."""
44        return []
45
46    def title(self, feed_victim):
47        """Return the feed title."""
48        return "Title"
49
50    def description(self, feed_victim):
51        """Return the feed descrition."""
52        return "Description"
53
54    def link(self, feed_victim):
55        """Return the feed link."""
56        return "http://djangoproject.com/"
57
58    def feed_parameters(self, param):
59        """Convert the feed parameter string into a dictionary of
60        parameters."""
61        return {}
62
63    def feed_victim(self, **feed_parameters):
64        """Find the feed victim according to the feed parameters."""
65        return None
66
67    def generator_chooser(self, feed_victim):
68        """Choose which item generator function to use based on the
69        result of `feed_victim` below. If you don't over-ride this method, we'll just
70        use `item_generator` above."""
71        return self.item_generator
72
73    def generator_parameters(self, feed_victim):
74        """Return a dictionary of keyword arguments for the item generator
75        based on the feed_victim."""
76        return {}
77
78    def register(feedclass):
79        """Class method to register this class as a feed configuration.
80        Returns a viewer method you can aim a urlconf at."""
81        feed = feedclass()
82        register_feed(feed)
83        return feed.view
84    register = classmethod(register)
85
86    def view(self, request, param=None):
87        """Method to render this feed."""
88        return feed(request, self.slug, param)
89
90class UserFeed(SimplerFeedConfiguration):
91    """A feed for users' postings unqualified by tag."""
92
93    slug = 'tags_user'
94    default_generator_parameters = {
95        'limit': 10
96        }
97   
98    def feed_parameters(self, param):
99        try:
100            user = users.get_object(username__exact=param)
101        except users.UserDoesNotExist:
102            raise Http404
103        return { 'username': param }
104   
105    def feed_victim(self, username=None):
106        try:
107            user = users.get_object(username__exact=username)
108        except users.UserDoesNotExist:
109            raise Http404
110        return user
111
112    def generator_parameters(self, victim):
113        return { 'id': victim.id }
114
115    def item_generator(self, limit=None, id=None):
116        all = posts.get_list(user_id__exact=id, order_by=['-posted'])
117        return all[:limit]
118
119    def description(self, victim):
120        return "The latest postings by %s" % victim.username
121
122    def title(self, victim):
123        return "tags/%s" % victim.username
124
125    def link(self, victim):
126        return "/tags/%s" % victim.username
127
128user_feed = UserFeed.register()
129
130# So if the urlconf points to user_feed, it'll all work nicely.
Back to Top