Creare un custom feed in Django con il Syndication Feed Framework

Ciao a tutti.

In questo articolo vorrei mostrare come creare un custom feed in Django, utilizzando il Django Syndication Feed Framework 1.2 o superiore.

Mi è capitato di dover creare un XML con alcuni tag in più per ogni item del feed. In particolare, oltre ai soliti tag presenti in un feed di tipo RSS, ho aggiunto i tag

<short_description/>

ed

<image/>

Leggendo la documentazione ufficiale del framework, viene consigliato di creare un custom feed generator, ma il processo non è molto chiaro.

Vediamo dunque passo passo come risolvere il tutto.

Creiamo innanzitutto un custom feed generator.

Questo generatore non dovrà fare altro che produrre lo stesso XML che produrrebbe il generatore di default del framework e per ogni item della lista aggiungere i nuovi tag.

1
2
3
4
5
class CustomFeedGenerator(Rss201rev2Feed):
    def add_item_elements(self, handler, item):
        super(CustomFeedGenerator, self).add_item_elements(handler, item)
        handler.addQuickElement(u"image", item['image'])
        handler.addQuickElement(u"short_description", item['short_description'])

Vediamo che il generator non fa altro che richiamare il metodo add_item_elements(handler, item) della sua superclasse e poi aggiungere i nuovi tag per ogni item dell’XML.
Notiamo che i valori che andrà ad inserire nei tag image e short_description dovranno essere presenti nel dizionario item, passato come argomento.

Creiamo dunque la nostra classe CustomFeed che eredita dalla classe Feed e vediamo come istruirla ad usare il nostro CustomFeedGenerator e come inserirà i nuovi valori nel dizionario item.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class CustomFeed(Feed):

    feed_type = CustomFeedGenerator

    def title(self, obj):
        return u"My amazing feed"

    def description(self, obj):
        return u"My feed description"

    def link(self, obj):
        return "http://%s" % obj.get_absolute_url()

    def item_extra_kwargs(self, obj):
        """
        Returns an extra keyword arguments dictionary that is used with
        the `add_item` call of the feed generator.
        Add the 'content' field of the 'Entry' item, to be used by the custom feed generator.
        """

        return { 'image': obj.get_picture().get_medium_url() if obj.get_picture() else "",
                 'short_description' : obj.get_preview(),}

CustomFeed è molto simile ai vari feed a cui siamo abituati. Le uniche novità sono rappresentate dalla dichiarazione del tipo di feed

feed_type = CustomFeedGenerator

e dal metodo

item_extra_kwargs

, in grado di appendere eventuali nuovi valori nel dizionario item, ottenendo il risultato desiderato.

Tags: , ,


About Stefano

Stefano Mancini is a co-founder of DevInterface.

After graduating in Computer Science, he first specialized in Java/J2EE development by participating in several international projects in the pharmaceutical and banking ambits.

Enthusiast of agile development, like SCRUM for project management and eXtreme Programming for code writing, he then moved to dynamic languages like Ruby and Python.

About DevInterface

We are an information and communication technology agency. Our mission is to provide web application development, design services and communication strategies. We specialize in building web applications with modern and efficient frameworks.

Related Post

2 Responses to “Creare un custom feed in Django con il Syndication Feed Framework”

  1. Jason Kinnear scrive:

    If I found this thismorning I would be even happier, thanks for the great post!

Leave a Reply

Insert code beetween <code lang="ruby"> and </code>

Copyright 2012 DevInterface s.n.c.

DevInterface Blog is proudly powered by WordPress