Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ Depends on
* django-model-utils
* python-pytz


Huboard
-------

Expand Down
7 changes: 1 addition & 6 deletions django_mesh/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,14 @@ def get_oembed_markup(self, matchobj):
return '%(spacing)s<a href="%(url)s">%(url)s</a>' % 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):
if self.rendered_text == '':
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
Expand Down
3 changes: 1 addition & 2 deletions django_mesh/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
124 changes: 124 additions & 0 deletions django_mesh/tests/test_sitemaps.py
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>')
Copy link
Copy Markdown
Member

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

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)
7 changes: 5 additions & 2 deletions django_mesh_test_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down Expand Up @@ -84,7 +88,6 @@

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

Expand Down
34 changes: 34 additions & 0 deletions django_mesh_test_project/sitemaps.py
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"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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)
41 changes: 41 additions & 0 deletions django_mesh_test_project/templates/sitemaps.html
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>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{% 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>
14 changes: 14 additions & 0 deletions django_mesh_test_project/urls.py
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)),
)