-
Notifications
You must be signed in to change notification settings - Fork 5
Sitemap #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Sitemap #39
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,6 @@ Depends on | |
| * django-model-utils | ||
| * python-pytz | ||
|
|
||
|
|
||
| Huboard | ||
| ------- | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| # -*- coding: utf-8 -*- | ||
| #Copyright (C) 2011 Seán Hayes | ||
| # | ||
| #This program is free software: you can redistribute it and/or modify | ||
| #it under the terms of the GNU General Public License as published by | ||
| #the Free Software Foundation, either version 3 of the License, or | ||
| #(at your option) any later version. | ||
| # | ||
| #This program is distributed in the hope that it will be useful, | ||
| #but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| #GNU General Public License for more details. | ||
| # | ||
| #You should have received a copy of the GNU General Public License | ||
| #along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| # Django imports | ||
| from django.core.urlresolvers import reverse | ||
| from datetime import date, timedelta | ||
| # Test imports | ||
| from .util import BaseTestCase | ||
|
|
||
| # App imports | ||
| from ..models import Post | ||
|
|
||
| class SitemapIndexTestCase(BaseTestCase): | ||
| def test_items_returned(self): | ||
| self.c1.save() | ||
| self.c3.save() | ||
| self.p1.channel = self.c1 | ||
| self.p1.save() | ||
|
|
||
| self.p2.channel = self.c3 | ||
| self.p2.save() | ||
|
|
||
| self.p3.channel = self.c1 | ||
| self.p3.save() | ||
|
|
||
| response = self.client.get(reverse('sitemap')) | ||
|
|
||
| self.assertEqual(response.status_code, 200) | ||
| self.assertContains(response, "sitemap-channel") | ||
| self.assertContains(response, "sitemap-tag") | ||
| self.assertContains(response, "sitemap-post") | ||
|
|
||
| class PostSitemapsTestCase(BaseTestCase): | ||
|
|
||
| def test_items_returned(self): | ||
| domain_name = 'http://example.com/' | ||
| sub_name = 'blog/' | ||
| channel = 'posts/' | ||
|
|
||
| self.c1.save() | ||
| self.c3.save() | ||
| self.t1.save() | ||
| self.t2.save() | ||
|
|
||
| self.p1.channel = self.c1 | ||
| self.p1.save() | ||
| self.p1.tags.add(self.t1) | ||
| self.p1.published = date.today() | ||
|
|
||
| self.p2.channel = self.c3 | ||
| self.p2.save() | ||
| self.p2.tags.add(self.t2) | ||
|
|
||
| self.p3.channel = self.c1 | ||
| self.p3.save() | ||
| self.p3.tags.add(self.t2) | ||
|
|
||
| response = self.client.get(reverse('sitemaps', kwargs={'section': 'post'})) | ||
|
|
||
| self.assertContains(response, '<loc>%(dn)s%(sn)s%(c)s%(slug)s/</loc>' %{"dn":domain_name, "sn":sub_name, "c":channel, "slug": self.p1.slug}) | ||
| self.assertNotContains(response, '<loc>%(dn)s%(sn)s%(c)s%(slug)s/</loc>' %{"dn":domain_name, "sn":sub_name, "c":channel, "slug": self.p2.slug}) | ||
| # possible tags in sitemap page | ||
| self.assertContains(response, '<changefreq>') | ||
| self.assertContains(response, '<priority>') | ||
| self.assertContains(response, '<lastmod>') | ||
|
|
||
| self.assertContains(response, '<news:keywords>') | ||
| self.assertContains(response, '<news:publication_date>') | ||
| self.assertContains(response, '<news:title>') | ||
| self.assertContains(response, '<news:publication>') | ||
|
|
||
| # p1 tags should be in there | ||
| self.assertContains(response, self.p1.title) #<news:title> | ||
| self.assertContains(response, self.t1.title ) #<news:keywords> | ||
| self.assertContains(response, self.p1.published) #<news:publication_date> | ||
|
|
||
| # p2 tags should not, since its put in a private channel | ||
| self.assertNotContains(response, self.p2.title) | ||
| self.assertNotContains(response, self.t2.title ) #<news:keywords> | ||
|
|
||
| self.assertNotContains(response, self.p3.title) | ||
|
|
||
| class ChannelSitemapsTestCase(BaseTestCase): | ||
| def test_items_returned(self): | ||
| self.c1.save() | ||
| self.c3.save() | ||
|
|
||
| response = self.client.get(reverse('sitemaps', kwargs={'section': 'channel'})) | ||
|
|
||
| self.assertContains(response, self.c1.title) | ||
| self.assertNotContains(response, self.c3.title) | ||
|
|
||
| class TagSitemapsTestCase(BaseTestCase): | ||
| def test_items_returned(self): | ||
|
|
||
| self.c1.save() | ||
| self.c2.save() | ||
| self.p1.channel = self.c1 | ||
| self.p2.channel = self.c2 | ||
| self.p1.save() | ||
| self.p2.save() | ||
|
|
||
| self.t1.save() | ||
| self.t2.save() | ||
|
|
||
| self.p1.tags.add(self.t1) | ||
| self.p2.tags.add(self.t2) | ||
|
|
||
| response = self.client.get(reverse('sitemaps', kwargs={'section': 'tag'})) | ||
|
|
||
| self.assertContains(response, self.t1.title) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| from django.contrib.sitemaps import GenericSitemap | ||
|
|
||
| from django.contrib.sitemaps import Sitemap | ||
| from django_mesh.models import Post, Channel, Tag | ||
| from django.contrib.sites.models import Site | ||
|
|
||
| class PostSitemap(Sitemap): | ||
| changefreq = "always" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. daily |
||
| priority = 0.9 | ||
|
|
||
| def items(self): | ||
| return Post.objects.active().filter(channel__public=True) | ||
|
|
||
| def lastmod(self, obj): | ||
| return obj.modified | ||
|
|
||
| @property | ||
| def name(self): | ||
| site = Site.objects.get_current() | ||
| return site | ||
|
|
||
| class ChannelSitemap(Sitemap): | ||
| changefreq="always" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. weekly |
||
| priority = 0.5 | ||
|
|
||
| def items(self): | ||
| return Channel.objects.filter(public=True) | ||
|
|
||
| class TagSitemap(Sitemap): | ||
| changefreq="always" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. daily |
||
| priority = 0.5 | ||
|
|
||
| def items(self): | ||
| return Tag.objects.filter(post__channel__public=True) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <urlset | ||
| xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" | ||
| xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"> | ||
| {% spaceless %} | ||
| {% for url in urlset %} | ||
| <url> | ||
| <loc>{{ url.location }}</loc> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be a good guide to use: https://github.com/callowayproject/django-news-sitemaps/blob/master/news_sitemaps/templates/sitemaps/news_sitemap.xml |
||
| {% if url.lastmod %}<lastmod>{{ url.lastmod|date:"Y-m-d" }}</lastmod>{% endif %} | ||
| {% if url.changefreq %}<changefreq>{{ url.changefreq }}</changefreq>{% endif %} | ||
| {% if url.priority %}<priority>{{ url.priority }}</priority>{% endif %} | ||
| <news:news> | ||
|
|
||
| {% if url.item.tags %} | ||
| <news:keywords> | ||
| {% for tag in url.item.tags.all %} | ||
| {{tag}} | ||
| {% endfor %} | ||
| </news:keywords> | ||
| {% endif %} | ||
|
|
||
| {% if url.item.published %} | ||
| <news:publication_date>{{ url.item.published|date:"Y-m-d" }}</news:publication_date> | ||
| {% endif %} | ||
|
|
||
| {% if url.item.title %}<news:title>{{ url.item.title }}</news:title>{% endif %} | ||
|
|
||
| {% if url.item.author %} | ||
| {% for site in sites %} | ||
| <news:publication> | ||
| <news:name> {{ site.name }} </news:name> | ||
| <news:language>en</news:language> | ||
| </news:publication> | ||
| {% endfor %} | ||
| {% endif %} | ||
|
|
||
| </news:news> | ||
| </url> | ||
| {% endfor %} | ||
| {% endspaceless %} | ||
| </urlset> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,23 @@ | ||
| from django.conf.urls import patterns, include, url | ||
|
|
||
| from .sitemaps import PostSitemap, ChannelSitemap, TagSitemap | ||
| from django.contrib.sitemaps.views import sitemap | ||
| from django.contrib import admin | ||
| admin.autodiscover() | ||
|
|
||
| sitemaps = { | ||
| 'post':PostSitemap, | ||
| 'channel':ChannelSitemap, | ||
| 'tag':TagSitemap | ||
| } | ||
|
|
||
| urlpatterns = patterns('', | ||
| url(r'^blog/', include('django_mesh.urls')), | ||
|
|
||
| url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.index', {'sitemaps': sitemaps}, name='sitemap'), | ||
|
|
||
| url(r'^sitemap-(?P<section>.+)\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps, | ||
| 'template_name': "sitemaps.html" | ||
| }, name='sitemaps'), | ||
| url(r'^admin/', include(admin.site.urls)), | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to add assertions for the values these tags contain.
It might be helpful to use an XML parser. This is a good one:
https://pypi.python.org/pypi/xmltodict