Andres Vargas - zodman

Cambiar el mimetype feed de django, para que en chrome se vea bonito.

Me dice un cliente …

Andrés el rss feed de noticias de chrome no se ve bonito como en firefox. Y yo pues wtf si es el navegador que lo interpetra asi.

Es que mira como se ve aqui http://cofradia.org/feed/ lo quiero y yo WTF!!!

Ok ok revisemos

Segun la documentación del RSS El mimetype que devuelve django es “application/rss+xml” en chrome encuentra esto y muestra en plano el xml y no parsea bonito como si fuera un xml.

Para cambiar esto, hay que sobre escribir un metodo en la definicion del feed y dectar el user agent y cambiar el mimetype

from django.utils.translation import ugettext_lazy as _ from django.contrib.syndication.views import Feed from django.core.urlresolvers import reverse from models import Article from settings import NEWS_FEED_MAX_ARTICLES

class LatestNewsFeed(Feed): title = _("XXXXX") description = _("Updates on changes and additions to xxxxxx.")

<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0060B0; font-weight: bold">link</span>(<span style="color: #007020">self</span>):
    <span style="color: #008000; font-weight: bold">return</span> reverse(<span style="background-color: #fff0f0">&#39;news_feed&#39;</span>)

<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0060B0; font-weight: bold">items</span>(<span style="color: #007020">self</span>):
    <span style="color: #008000; font-weight: bold">return</span> Article<span style="color: #303030">.</span>objects<span style="color: #303030">.</span>all()[:NEWS_FEED_MAX_ARTICLES]

<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0060B0; font-weight: bold">item_title</span>(<span style="color: #007020">self</span>, item):
    <span style="color: #008000; font-weight: bold">return</span> item<span style="color: #303030">.</span>title

<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0060B0; font-weight: bold">item_description</span>(<span style="color: #007020">self</span>, item):
    <span style="color: #008000; font-weight: bold">return</span> item<span style="color: #303030">.</span>extract

<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0060B0; font-weight: bold">get_feed</span>(<span style="color: #007020">self</span>, obj, request):
    f <span style="color: #303030">=</span> <span style="color: #007020">super</span>(LatestNewsFeed,<span style="color: #007020">self</span>)<span style="color: #303030">.</span>get_feed(obj,request)
    user_agent <span style="color: #303030">=</span> request<span style="color: #303030">.</span>META<span style="color: #303030">.</span>get(<span style="background-color: #fff0f0">&quot;HTTP_USER_AGENT&quot;</span>,<span style="background-color: #fff0f0">&quot;&quot;</span>)
    <span style="color: #008000; font-weight: bold">if</span> <span style="background-color: #fff0f0">&quot;webkit&quot;</span> <span style="color: #000000; font-weight: bold">in</span> user_agent<span style="color: #303030">.</span>lower():
        f<span style="color: #303030">.</span>mime_type<span style="color: #303030">=</span><span style="background-color: #fff0f0">&quot;text/xml&quot;</span>
    <span style="color: #008000; font-weight: bold">return</span> f

with django is easy!

#Django #Linuxmerida #Planetalinux #Linux #Python