From 2a4fcd0c4d0649e5f2a7b336fa64a088b57cf8cf Mon Sep 17 00:00:00 2001 From: Hung Nguyen Date: Mon, 13 Oct 2014 17:09:32 -0400 Subject: [PATCH 1/2] sitemaps created for Post sitemap indexing google news sitemap support based on requirements via support.google.com creating a Google News Sitemap sitemaps file added added tests for sitemaps cleaned up code --- README.rst | 1 - django_mesh/models.py | 7 +- django_mesh/tests/test_models.py | 3 +- django_mesh/tests/test_sitemaps.py | 124 ++++++++++++++++++ django_mesh_test_project/settings.py | 7 +- django_mesh_test_project/sitemaps.py | 28 ++++ .../templates/sitemaps.html | 39 ++++++ django_mesh_test_project/urls.py | 14 ++ 8 files changed, 212 insertions(+), 11 deletions(-) create mode 100644 django_mesh/tests/test_sitemaps.py create mode 100644 django_mesh_test_project/sitemaps.py create mode 100644 django_mesh_test_project/templates/sitemaps.html diff --git a/README.rst b/README.rst index 6ba666f..8dbcb4d 100644 --- a/README.rst +++ b/README.rst @@ -17,7 +17,6 @@ Depends on * django-model-utils * python-pytz - Huboard ------- diff --git a/django_mesh/models.py b/django_mesh/models.py index 392cb53..789a43b 100644 --- a/django_mesh/models.py +++ b/django_mesh/models.py @@ -46,7 +46,7 @@ def get_oembed_markup(self, matchobj): return '%(spacing)s%(url)s' % gd def render(self): - #TODO: strip out dangerous HTML attributes, only allow basic formatting tags + self.rendered_text = oembed_regex.sub(self.get_oembed_markup, self.text) def save(self, *args, **kwargs): @@ -54,11 +54,6 @@ def save(self, *args, **kwargs): self.render() super(_Abstract, self).save(*args, **kwargs) -# try: -# ping_google() -# except Exception: -# # Bare 'except' because we could get a variety of HTTP-related exceptions. -# pass def __str__(self): return self.title diff --git a/django_mesh/tests/test_models.py b/django_mesh/tests/test_models.py index 00ad92c..3305ebf 100644 --- a/django_mesh/tests/test_models.py +++ b/django_mesh/tests/test_models.py @@ -74,7 +74,6 @@ def test_str_unicode(self): self.p1.channel = self.c1 self.p1.save() - returned_title = str(self.p1) self.assertEqual(self.p1.title, returned_title) @@ -101,4 +100,4 @@ def test_get_absolute_url(self): def test_str_unicode(self): self.t1.save() returned_title = str(self.t1) - self.assertEqual(self.t1.title, returned_title) + self.assertEqual(self.t1.title, returned_title) \ No newline at end of file diff --git a/django_mesh/tests/test_sitemaps.py b/django_mesh/tests/test_sitemaps.py new file mode 100644 index 0000000..4591474 --- /dev/null +++ b/django_mesh/tests/test_sitemaps.py @@ -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 . + +# 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, '%(dn)s%(sn)s%(c)s%(slug)s/' %{"dn":domain_name, "sn":sub_name, "c":channel, "slug": self.p1.slug}) + self.assertNotContains(response, '%(dn)s%(sn)s%(c)s%(slug)s/' %{"dn":domain_name, "sn":sub_name, "c":channel, "slug": self.p2.slug}) +# possible tags in sitemap page + self.assertContains(response, '') + self.assertContains(response, '') + self.assertContains(response, '') + + self.assertContains(response, '') + self.assertContains(response, '') + self.assertContains(response, '') + self.assertContains(response, '') + +# p1 tags should be in there + self.assertContains(response, self.p1.title) # + self.assertContains(response, self.t1.title ) # + self.assertContains(response, self.p1.published) # + +# p2 tags should not, since its put in a private channel + self.assertNotContains(response, self.p2.title) + self.assertNotContains(response, self.t2.title ) # + + 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) \ No newline at end of file diff --git a/django_mesh_test_project/settings.py b/django_mesh_test_project/settings.py index 32112a1..eff8af2 100644 --- a/django_mesh_test_project/settings.py +++ b/django_mesh_test_project/settings.py @@ -25,11 +25,15 @@ # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True +TEMPLATE_LOADERS = ( + 'django.template.loaders.filesystem.Loader', + 'django.template.loaders.app_directories.Loader', + ) + TEMPLATE_DEBUG = True ALLOWED_HOSTS = [] - # Application definition INSTALLED_APPS = ( @@ -84,7 +88,6 @@ USE_TZ = True - # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.6/howto/static-files/ diff --git a/django_mesh_test_project/sitemaps.py b/django_mesh_test_project/sitemaps.py new file mode 100644 index 0000000..8c0d53b --- /dev/null +++ b/django_mesh_test_project/sitemaps.py @@ -0,0 +1,28 @@ +from django.contrib.sitemaps import GenericSitemap + +from django.contrib.sitemaps import Sitemap +from django_mesh.models import Post, Channel, Tag + +class PostSitemaps(Sitemap): + changefreq = "always" + priority = 0.9 + + def items(self): + return Post.objects.active().filter(channel__public=True) + + def lastmod(self, obj): + return obj.modified + +class ChannelSitemaps(Sitemap): + changefreq="always" + priority = 0.5 + + def items(self): + return Channel.objects.filter(public=True) + +class TagSitemaps(Sitemap): + changefreq="always" + priority = 0.5 + + def items(self): + return Tag.objects.filter(post__channel__public=True) \ No newline at end of file diff --git a/django_mesh_test_project/templates/sitemaps.html b/django_mesh_test_project/templates/sitemaps.html new file mode 100644 index 0000000..68ca7e7 --- /dev/null +++ b/django_mesh_test_project/templates/sitemaps.html @@ -0,0 +1,39 @@ + + +{% spaceless %} +{% for url in urlset %} + + {{ url.location }} + {% if url.lastmod %}{{ url.lastmod|date:"Y-m-d" }}{% endif %} + {% if url.changefreq %}{{ url.changefreq }}{% endif %} + {% if url.priority %}{{ url.priority }}{% endif %} + + + {% if url.item.tags %} + + {% for tag in url.item.tags.all %} + {{tag}} + {% endfor %} + + {% endif %} + + {% if url.item.published %} + {{ url.item.published|date:"Y-m-d" }} + {% endif %} + + {% if url.item.title %}{{ url.item.title }}{% endif %} + + {% if url.item.author %} + + Django Mesh Publication + en + + {% endif %} + + + +{% endfor %} +{% endspaceless %} + \ No newline at end of file diff --git a/django_mesh_test_project/urls.py b/django_mesh_test_project/urls.py index 22e1fd4..064fc46 100644 --- a/django_mesh_test_project/urls.py +++ b/django_mesh_test_project/urls.py @@ -1,9 +1,23 @@ from django.conf.urls import patterns, include, url +from .sitemaps import PostSitemaps, ChannelSitemaps, TagSitemaps +from django.contrib.sitemaps.views import sitemap from django.contrib import admin admin.autodiscover() +sitemaps = { + 'post':PostSitemaps, + 'channel':ChannelSitemaps, + 'tag':TagSitemaps +} + 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
.+)\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps, + 'template_name': "sitemaps.html" + }, name='sitemaps'), url(r'^admin/', include(admin.site.urls)), ) \ No newline at end of file From 8a39aba65352e23300f42128cd07d4639c47c358 Mon Sep 17 00:00:00 2001 From: Hung Nguyen Date: Tue, 6 Jan 2015 18:37:26 -0500 Subject: [PATCH 2/2] not working sitemap.name --- django_mesh_test_project/sitemaps.py | 12 +++++++++--- django_mesh_test_project/templates/sitemaps.html | 4 +++- django_mesh_test_project/urls.py | 8 ++++---- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/django_mesh_test_project/sitemaps.py b/django_mesh_test_project/sitemaps.py index 8c0d53b..db09185 100644 --- a/django_mesh_test_project/sitemaps.py +++ b/django_mesh_test_project/sitemaps.py @@ -2,8 +2,9 @@ from django.contrib.sitemaps import Sitemap from django_mesh.models import Post, Channel, Tag +from django.contrib.sites.models import Site -class PostSitemaps(Sitemap): +class PostSitemap(Sitemap): changefreq = "always" priority = 0.9 @@ -13,14 +14,19 @@ def items(self): def lastmod(self, obj): return obj.modified -class ChannelSitemaps(Sitemap): + @property + def name(self): + site = Site.objects.get_current() + return site + +class ChannelSitemap(Sitemap): changefreq="always" priority = 0.5 def items(self): return Channel.objects.filter(public=True) -class TagSitemaps(Sitemap): +class TagSitemap(Sitemap): changefreq="always" priority = 0.5 diff --git a/django_mesh_test_project/templates/sitemaps.html b/django_mesh_test_project/templates/sitemaps.html index 68ca7e7..fab1101 100644 --- a/django_mesh_test_project/templates/sitemaps.html +++ b/django_mesh_test_project/templates/sitemaps.html @@ -26,10 +26,12 @@ {% if url.item.title %}{{ url.item.title }}{% endif %} {% if url.item.author %} + {% for site in sites %} - Django Mesh Publication + {{ site.name }} en + {% endfor %} {% endif %} diff --git a/django_mesh_test_project/urls.py b/django_mesh_test_project/urls.py index 064fc46..406be97 100644 --- a/django_mesh_test_project/urls.py +++ b/django_mesh_test_project/urls.py @@ -1,14 +1,14 @@ from django.conf.urls import patterns, include, url -from .sitemaps import PostSitemaps, ChannelSitemaps, TagSitemaps +from .sitemaps import PostSitemap, ChannelSitemap, TagSitemap from django.contrib.sitemaps.views import sitemap from django.contrib import admin admin.autodiscover() sitemaps = { - 'post':PostSitemaps, - 'channel':ChannelSitemaps, - 'tag':TagSitemaps + 'post':PostSitemap, + 'channel':ChannelSitemap, + 'tag':TagSitemap } urlpatterns = patterns('',