Code

Ticket #10907: news_sitemaps.diff

File news_sitemaps.diff, 9.8 KB (added by lakinwecker, 5 years ago)

Fixed the bugs.

Line 
1Index: django/contrib/sitemaps/views.py
2===================================================================
3--- django/contrib/sitemaps/views.py    (revision 10628)
4+++ django/contrib/sitemaps/views.py    (working copy)
5@@ -22,7 +22,7 @@
6     xml = loader.render_to_string('sitemap_index.xml', {'sitemaps': sites})
7     return HttpResponse(xml, mimetype='application/xml')
8 
9-def sitemap(request, sitemaps, section=None):
10+def sitemap(request, sitemaps, section=None, template='sitemap.xml'):
11     maps, urls = [], []
12     if section is not None:
13         if section not in sitemaps:
14@@ -41,5 +41,5 @@
15             raise Http404("Page %s empty" % page)
16         except PageNotAnInteger:
17             raise Http404("No page '%s'" % page)
18-    xml = smart_str(loader.render_to_string('sitemap.xml', {'urlset': urls}))
19+    xml = smart_str(loader.render_to_string(template, {'urlset': urls}))
20     return HttpResponse(xml, mimetype='application/xml')
21Index: django/contrib/sitemaps/__init__.py
22===================================================================
23--- django/contrib/sitemaps/__init__.py (revision 10628)
24+++ django/contrib/sitemaps/__init__.py (working copy)
25@@ -38,7 +38,7 @@
26     # http://sitemaps.org/protocol.php#index.
27     limit = 50000
28 
29-    def __get(self, name, obj, default=None):
30+    def _get(self, name, obj, default=None):
31         try:
32             attr = getattr(self, name)
33         except AttributeError:
34@@ -59,19 +59,22 @@
35         return self._paginator
36     paginator = property(_get_paginator)
37 
38+    def get_url_info(self, item, current_site):
39+        loc = "http://%s%s" % (current_site.domain, self._get('location', item))
40+        url_info = {
41+            'location':   loc,
42+            'lastmod':    self._get('lastmod', item, None),
43+            'changefreq': self._get('changefreq', item, None),
44+            'priority':   self._get('priority', item, None)
45+        }
46+        return url_info
47+
48     def get_urls(self, page=1):
49         from django.contrib.sites.models import Site
50         current_site = Site.objects.get_current()
51         urls = []
52         for item in self.paginator.page(page).object_list:
53-            loc = "http://%s%s" % (current_site.domain, self.__get('location', item))
54-            url_info = {
55-                'location':   loc,
56-                'lastmod':    self.__get('lastmod', item, None),
57-                'changefreq': self.__get('changefreq', item, None),
58-                'priority':   self.__get('priority', item, None)
59-            }
60-            urls.append(url_info)
61+            urls.append(self.get_url_info(item, current_site))
62         return urls
63 
64 class FlatPageSitemap(Sitemap):
65@@ -98,3 +101,17 @@
66         if self.date_field is not None:
67             return getattr(item, self.date_field)
68         return None
69+
70+class NewsSitemap(Sitemap):
71+    # This limit is defined by Google. See the index documentation at
72+    # http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=74288
73+    limit = 1000
74+
75+
76+    def get_url_info(self, item, current_site):
77+        url_info = super(NewsSitemap, self).get_url_info(item, current_site)
78+        url_info.update({
79+            'publication_date': self._get('publication_date', item, None),
80+            'keywords': self._get('keywords', item, None),
81+        })
82+        return url_info
83Index: django/contrib/sitemaps/templates/news_sitemap.xml
84===================================================================
85--- django/contrib/sitemaps/templates/news_sitemap.xml  (revision 0)
86+++ django/contrib/sitemaps/templates/news_sitemap.xml  (revision 0)
87@@ -0,0 +1,19 @@
88+<?xml version="1.0" encoding="UTF-8"?>
89+<urlset
90+  xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
91+  xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
92+{% spaceless %}
93+{% for url in urlset %}
94+  <url>
95+    <loc>{{ url.location }}</loc>
96+    {% if url.lastmod %}<lastmod>{{ url.lastmod|date:"Y-m-d" }}</lastmod>{% endif %}
97+    {% if url.changefreq %}<changefreq>{{ url.changefreq }}</changefreq>{% endif %}
98+    {% if url.priority %}<priority>{{ url.priority }}</priority>{% endif %}
99+    <news:news>
100+      {% if url.publication_date %}<news:publication_date>{{ url.publication_date|date:"Y-m-d" }}</news:publication_date>{% endif %}
101+      {% if url.keywords %}<news:keywords>Business, Mergers, Acquisitions</news:keywords>{% endif %}
102+    </news:news>
103+   </url>
104+{% endfor %}
105+{% endspaceless %}
106+</urlset>
107
108Property changes on: django/contrib/sitemaps/templates/news_sitemap.xml
109___________________________________________________________________
110Added: svn:eol-style
111   + native
112
113Index: docs/ref/contrib/sitemaps.txt
114===================================================================
115--- docs/ref/contrib/sitemaps.txt       (revision 10628)
116+++ docs/ref/contrib/sitemaps.txt       (working copy)
117@@ -8,9 +8,10 @@
118    :synopsis: A framework for generating Google sitemap XML files.
119 
120 Django comes with a high-level sitemap-generating framework that makes
121-creating sitemap_ XML files easy.
122+creating sitemap_ and newssitemap_ XML files easy.
123 
124 .. _sitemap: http://www.sitemaps.org/
125+.. _newssitemap: http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=74288
126 
127 Overview
128 ========
129@@ -20,12 +21,20 @@
130 to other pages on your site. This information helps search engines index your
131 site.
132 
133-The Django sitemap framework automates the creation of this XML file by letting
134+A Google News sitemap is an XML file that allows you to have more control over
135+the content you submit to Google News. Creating a News sitemap allows you to
136+tell Google about all your news articles, their publication date and keywords.
137+A news sitemaps differs only slightly from a normal sitemap.  The most important
138+difference is the requirement for your site to register (and be accepted) into
139+the list of news sites that google news indexes.
140+
141+The Django sitemap framework automates the creation of these XML files by letting
142 you express this information in Python code.
143 
144 It works much like Django's :ref:`syndication framework
145 <ref-contrib-syndication>`. To create a sitemap, just write a
146-:class:`~django.contrib.sitemaps.Sitemap` class and point to it in your
147+:class:`~django.contrib.sitemaps.Sitemap` and/or
148+:class:`~django.contrib.sitemaps.Sitemap` class and point to them in your
149 :ref:`URLconf <topics-http-urls>`.
150 
151 Installation
152@@ -51,13 +60,20 @@
153 Initialization
154 ==============
155 
156-To activate sitemap generation on your Django site, add this line to your
157+To activate sitemap generation on your Django site, add the applicable lines to your
158 :ref:`URLconf <topics-http-urls>`::
159 
160    (r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps})
161 
162 This tells Django to build a sitemap when a client accesses :file:`/sitemap.xml`.
163 
164+For a google news sitemap, the setup is nearly identical
165+:ref:`URLconf <topics-http-urls>`::
166+
167+   (r'^news_sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': news_sitemaps, 'template': 'news_sitemap.xml'})
168+
169+This tells Django to build a news sitemap when a client accesses :file:`/news_sitemap.xml`.
170+
171 The name of the sitemap file is not important, but the location is. Search
172 engines will only index links in your sitemap for the current URL level and
173 below. For instance, if :file:`sitemap.xml` lives in your root directory, it may
174@@ -67,11 +83,20 @@
175 
176 The sitemap view takes an extra, required argument: ``{'sitemaps': sitemaps}``.
177 ``sitemaps`` should be a dictionary that maps a short section label (e.g.,
178-``blog`` or ``news``) to its :class:`~django.contrib.sitemaps.Sitemap` class
179-(e.g., ``BlogSitemap`` or ``NewsSitemap``). It may also map to an *instance* of
180+``blog`` or ``events``) to its :class:`~django.contrib.sitemaps.Sitemap` class
181+(e.g., ``BlogSitemap`` or ``EventSitemap``). It may also map to an *instance* of
182 a :class:`~django.contrib.sitemaps.Sitemap` class (e.g.,
183 ``BlogSitemap(some_var)``).
184 
185+The sitemap framework takes an extra, optional argument: ``{'template': 'news_sitemap.xml'}``.
186+The :file:`news_sitemap.xml` generates a Google News Sitemap. When using this
187+template, the 'sitemaps' argument should only contain
188+:class:`~django.contrib.sitemaps.NewsSitemap` classes (or instances).
189+Because :class:`~django.contrib.sitemaps.NewsSitemap` extends
190+:class:`~django.contrib.sitemaps.Sitemap` the rest of this documentaiton will
191+only refer to those attributes that are unique to the
192+:class:`~django.contrib.sitemaps.NewsSitemap` classes (or instances).
193+
194 Sitemap classes
195 ===============
196 
197@@ -209,6 +234,39 @@
198 
199         .. _sitemaps.org documentation: http://www.sitemaps.org/protocol.html#prioritydef
200 
201+NewsSitemap class reference
202+=======================
203+
204+.. class:: NewsSitemap
205+
206+    A ``NewsSitemap`` class can define all of the following methods/attributes in addition to those above:
207+
208+    .. attribute:: Sitemap.items
209+
210+        **Required.** A method that returns a list of objects. (See :class:`~django.contrib.sitemaps.Sitemap`)
211+        In the case of a news sitemap, this method must only return news items from the past 3 days.
212+
213+    .. attribute:: Sitemap.publication_date
214+
215+        **Required.** A method that takes on argument -- an object as returned by
216+        :attr:`~Sitemap.items()` -- and return that object's published_date, as a Python
217+        ``datetime.datetime`` object.
218+
219+    .. attribute:: Sitemap.keywords
220+
221+        **Optional.** Either a method or attribute.
222+
223+        If it's a method, it should return a string containing the comma-separated list of
224+        keywords describing the content of the article returned by :attr:`~Sitemap.items()`.
225+
226+        If it's an attribute, its value should be a string containing the comma-separated list
227+        of keywords describing the content of the article returned by :attr:`~Sitemap.items()`.
228+
229+        In both cases, the keywords may drawn from, but not limited to _googlenewscategories.
230+
231+
232+.. _googlenewscategories: http://www.google.com/support/webmasters/bin/answer.py?answer=42993
233+
234 Shortcuts
235 =========
236