Ticket #10907: news_sitemaps.diff

File news_sitemaps.diff, 9.8 KB (added by Lakin Wecker, 15 years ago)

Fixed the bugs.

  • django/contrib/sitemaps/views.py

     
    2222    xml = loader.render_to_string('sitemap_index.xml', {'sitemaps': sites})
    2323    return HttpResponse(xml, mimetype='application/xml')
    2424
    25 def sitemap(request, sitemaps, section=None):
     25def sitemap(request, sitemaps, section=None, template='sitemap.xml'):
    2626    maps, urls = [], []
    2727    if section is not None:
    2828        if section not in sitemaps:
     
    4141            raise Http404("Page %s empty" % page)
    4242        except PageNotAnInteger:
    4343            raise Http404("No page '%s'" % page)
    44     xml = smart_str(loader.render_to_string('sitemap.xml', {'urlset': urls}))
     44    xml = smart_str(loader.render_to_string(template, {'urlset': urls}))
    4545    return HttpResponse(xml, mimetype='application/xml')
  • django/contrib/sitemaps/__init__.py

     
    3838    # http://sitemaps.org/protocol.php#index.
    3939    limit = 50000
    4040
    41     def __get(self, name, obj, default=None):
     41    def _get(self, name, obj, default=None):
    4242        try:
    4343            attr = getattr(self, name)
    4444        except AttributeError:
     
    5959        return self._paginator
    6060    paginator = property(_get_paginator)
    6161
     62    def get_url_info(self, item, current_site):
     63        loc = "http://%s%s" % (current_site.domain, self._get('location', item))
     64        url_info = {
     65            'location':   loc,
     66            'lastmod':    self._get('lastmod', item, None),
     67            'changefreq': self._get('changefreq', item, None),
     68            'priority':   self._get('priority', item, None)
     69        }
     70        return url_info
     71
    6272    def get_urls(self, page=1):
    6373        from django.contrib.sites.models import Site
    6474        current_site = Site.objects.get_current()
    6575        urls = []
    6676        for item in self.paginator.page(page).object_list:
    67             loc = "http://%s%s" % (current_site.domain, self.__get('location', item))
    68             url_info = {
    69                 'location':   loc,
    70                 'lastmod':    self.__get('lastmod', item, None),
    71                 'changefreq': self.__get('changefreq', item, None),
    72                 'priority':   self.__get('priority', item, None)
    73             }
    74             urls.append(url_info)
     77            urls.append(self.get_url_info(item, current_site))
    7578        return urls
    7679
    7780class FlatPageSitemap(Sitemap):
     
    98101        if self.date_field is not None:
    99102            return getattr(item, self.date_field)
    100103        return None
     104
     105class NewsSitemap(Sitemap):
     106    # This limit is defined by Google. See the index documentation at
     107    # http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=74288
     108    limit = 1000
     109
     110
     111    def get_url_info(self, item, current_site):
     112        url_info = super(NewsSitemap, self).get_url_info(item, current_site)
     113        url_info.update({
     114            'publication_date': self._get('publication_date', item, None),
     115            'keywords': self._get('keywords', item, None),
     116        })
     117        return url_info
  • django/contrib/sitemaps/templates/news_sitemap.xml

     
     1<?xml version="1.0" encoding="UTF-8"?>
     2<urlset
     3  xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
     4  xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
     5{% spaceless %}
     6{% for url in urlset %}
     7  <url>
     8    <loc>{{ url.location }}</loc>
     9    {% if url.lastmod %}<lastmod>{{ url.lastmod|date:"Y-m-d" }}</lastmod>{% endif %}
     10    {% if url.changefreq %}<changefreq>{{ url.changefreq }}</changefreq>{% endif %}
     11    {% if url.priority %}<priority>{{ url.priority }}</priority>{% endif %}
     12    <news:news>
     13      {% if url.publication_date %}<news:publication_date>{{ url.publication_date|date:"Y-m-d" }}</news:publication_date>{% endif %}
     14      {% if url.keywords %}<news:keywords>Business, Mergers, Acquisitions</news:keywords>{% endif %}
     15    </news:news>
     16   </url>
     17{% endfor %}
     18{% endspaceless %}
     19</urlset>
  • docs/ref/contrib/sitemaps.txt

    Property changes on: django/contrib/sitemaps/templates/news_sitemap.xml
    ___________________________________________________________________
    Added: svn:eol-style
       + native
    
     
    88   :synopsis: A framework for generating Google sitemap XML files.
    99
    1010Django comes with a high-level sitemap-generating framework that makes
    11 creating sitemap_ XML files easy.
     11creating sitemap_ and newssitemap_ XML files easy.
    1212
    1313.. _sitemap: http://www.sitemaps.org/
     14.. _newssitemap: http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=74288
    1415
    1516Overview
    1617========
     
    2021to other pages on your site. This information helps search engines index your
    2122site.
    2223
    23 The Django sitemap framework automates the creation of this XML file by letting
     24A Google News sitemap is an XML file that allows you to have more control over
     25the content you submit to Google News. Creating a News sitemap allows you to
     26tell Google about all your news articles, their publication date and keywords.
     27A news sitemaps differs only slightly from a normal sitemap.  The most important
     28difference is the requirement for your site to register (and be accepted) into
     29the list of news sites that google news indexes.
     30
     31The Django sitemap framework automates the creation of these XML files by letting
    2432you express this information in Python code.
    2533
    2634It works much like Django's :ref:`syndication framework
    2735<ref-contrib-syndication>`. To create a sitemap, just write a
    28 :class:`~django.contrib.sitemaps.Sitemap` class and point to it in your
     36:class:`~django.contrib.sitemaps.Sitemap` and/or
     37:class:`~django.contrib.sitemaps.Sitemap` class and point to them in your
    2938:ref:`URLconf <topics-http-urls>`.
    3039
    3140Installation
     
    5160Initialization
    5261==============
    5362
    54 To activate sitemap generation on your Django site, add this line to your
     63To activate sitemap generation on your Django site, add the applicable lines to your
    5564:ref:`URLconf <topics-http-urls>`::
    5665
    5766   (r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps})
    5867
    5968This tells Django to build a sitemap when a client accesses :file:`/sitemap.xml`.
    6069
     70For a google news sitemap, the setup is nearly identical
     71:ref:`URLconf <topics-http-urls>`::
     72
     73   (r'^news_sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': news_sitemaps, 'template': 'news_sitemap.xml'})
     74
     75This tells Django to build a news sitemap when a client accesses :file:`/news_sitemap.xml`.
     76
    6177The name of the sitemap file is not important, but the location is. Search
    6278engines will only index links in your sitemap for the current URL level and
    6379below. For instance, if :file:`sitemap.xml` lives in your root directory, it may
     
    6783
    6884The sitemap view takes an extra, required argument: ``{'sitemaps': sitemaps}``.
    6985``sitemaps`` should be a dictionary that maps a short section label (e.g.,
    70 ``blog`` or ``news``) to its :class:`~django.contrib.sitemaps.Sitemap` class
    71 (e.g., ``BlogSitemap`` or ``NewsSitemap``). It may also map to an *instance* of
     86``blog`` or ``events``) to its :class:`~django.contrib.sitemaps.Sitemap` class
     87(e.g., ``BlogSitemap`` or ``EventSitemap``). It may also map to an *instance* of
    7288a :class:`~django.contrib.sitemaps.Sitemap` class (e.g.,
    7389``BlogSitemap(some_var)``).
    7490
     91The sitemap framework takes an extra, optional argument: ``{'template': 'news_sitemap.xml'}``.
     92The :file:`news_sitemap.xml` generates a Google News Sitemap. When using this
     93template, the 'sitemaps' argument should only contain
     94:class:`~django.contrib.sitemaps.NewsSitemap` classes (or instances).
     95Because :class:`~django.contrib.sitemaps.NewsSitemap` extends
     96:class:`~django.contrib.sitemaps.Sitemap` the rest of this documentaiton will
     97only refer to those attributes that are unique to the
     98:class:`~django.contrib.sitemaps.NewsSitemap` classes (or instances).
     99
    75100Sitemap classes
    76101===============
    77102
     
    209234
    210235        .. _sitemaps.org documentation: http://www.sitemaps.org/protocol.html#prioritydef
    211236
     237NewsSitemap class reference
     238=======================
     239
     240.. class:: NewsSitemap
     241
     242    A ``NewsSitemap`` class can define all of the following methods/attributes in addition to those above:
     243
     244    .. attribute:: Sitemap.items
     245
     246        **Required.** A method that returns a list of objects. (See :class:`~django.contrib.sitemaps.Sitemap`)
     247        In the case of a news sitemap, this method must only return news items from the past 3 days.
     248
     249    .. attribute:: Sitemap.publication_date
     250
     251        **Required.** A method that takes on argument -- an object as returned by
     252        :attr:`~Sitemap.items()` -- and return that object's published_date, as a Python
     253        ``datetime.datetime`` object.
     254
     255    .. attribute:: Sitemap.keywords
     256
     257        **Optional.** Either a method or attribute.
     258
     259        If it's a method, it should return a string containing the comma-separated list of
     260        keywords describing the content of the article returned by :attr:`~Sitemap.items()`.
     261
     262        If it's an attribute, its value should be a string containing the comma-separated list
     263        of keywords describing the content of the article returned by :attr:`~Sitemap.items()`.
     264
     265        In both cases, the keywords may drawn from, but not limited to _googlenewscategories.
     266
     267
     268.. _googlenewscategories: http://www.google.com/support/webmasters/bin/answer.py?answer=42993
     269
    212270Shortcuts
    213271=========
    214272
Back to Top