From 96d4000f66db7dd702c862f8b3a221963448eb5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Tr=C3=B6ger?= Date: Sun, 9 Jun 2013 12:58:37 +0200 Subject: [PATCH] Implement /sitemap.xml for NightlyBuilds and Pastebin apps Introduce StaticSitemap() which reads a Site's URLConf and generates a Sitemap for all URLs which can be reversed without any arguments. --- geany/sitemaps.py | 61 +++++++++++++++++++++++++++++++++++++++++-- nightlybuilds/urls.py | 12 +++++++-- pastebin/urls.py | 10 ++++++- 3 files changed, 78 insertions(+), 5 deletions(-) diff --git a/geany/sitemaps.py b/geany/sitemaps.py index 595f1e8..2db0e26 100644 --- a/geany/sitemaps.py +++ b/geany/sitemaps.py @@ -3,15 +3,19 @@ # it under the terms of the GNU Affero 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 Affero General Public License for more details. -# +# # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +from django.contrib import sitemaps +from django.contrib.sites.models import Site +from django.core import urlresolvers +from django_hosts.reverse import get_host from mezzanine.blog.models import BlogPost from mezzanine.core.sitemaps import DisplayableSitemap @@ -34,3 +38,56 @@ def priority(self, obj): return 1 else: return 0.5 + + +######################################################################## +class StaticSitemap(sitemaps.Sitemap): + """Return the static sitemap items""" + priority = 0.5 + + #---------------------------------------------------------------------- + def __init__(self, domain, patterns): + self._domain = domain + self._patterns = patterns + self._site = None + self._host = None + self._items = {} + self._get_site_and_host() + self._initialize() + + #---------------------------------------------------------------------- + def _get_site_and_host(self): + self._site = Site.objects.get(domain=self._domain) + self._host = get_host(self._domain) + + #---------------------------------------------------------------------- + def _initialize(self): + for pattern in self._patterns: + if getattr(pattern, 'name', None) is not None: + url_resolved = self._resolve_url(pattern.name) + if url_resolved: + self._items[pattern.name] = url_resolved + + #---------------------------------------------------------------------- + def _resolve_url(self, url): + try: + return urlresolvers.reverse(url, urlconf=self._host.urlconf) + except urlresolvers.NoReverseMatch: + return None + + #---------------------------------------------------------------------- + def items(self): + return self._items.keys() + + #---------------------------------------------------------------------- + def changefreq(self, obj): + return 'monthly' + + #---------------------------------------------------------------------- + def location(self, obj): + return self._items[obj] + + #---------------------------------------------------------------------- + def get_urls(self, page=1, site=None, protocol=None): + # pass our site to the parent as we know better which site we are on + return super(StaticSitemap, self).get_urls(page, self._site, protocol) diff --git a/nightlybuilds/urls.py b/nightlybuilds/urls.py index 9a94ab3..8aff3e1 100644 --- a/nightlybuilds/urls.py +++ b/nightlybuilds/urls.py @@ -3,17 +3,18 @@ # it under the terms of the GNU Affero 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 Affero General Public License for more details. -# +# # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . from django.conf.urls import url, patterns from nightlybuilds.views import NightlyBuildsView +from geany.sitemaps import StaticSitemap urlpatterns = patterns('', @@ -22,3 +23,10 @@ url(r'^$', NightlyBuildsView.as_view(), name='home'), ) + +# Sitemap framework +sitemaps = {"sitemaps": {"all": StaticSitemap('nightly.geany.org', urlpatterns)}} +urlpatterns += patterns('', + # use our custom sitemap implementation + url(r"^sitemap\.xml$", 'django.contrib.sitemaps.views.sitemap', sitemaps) +) diff --git a/pastebin/urls.py b/pastebin/urls.py index 8c3ba6b..a002896 100644 --- a/pastebin/urls.py +++ b/pastebin/urls.py @@ -13,8 +13,9 @@ # along with this program. If not, see . -from django.conf.urls import url, patterns, include +from django.conf.urls import url, patterns from django.views.generic.base import TemplateView +from geany.sitemaps import StaticSitemap urlpatterns = patterns('', @@ -32,3 +33,10 @@ url(r'^(?P[a-zA-Z0-9]+)/delete/$', 'pastebin.views.snippet_delete', name='snippet_delete'), url(r'^(?P[a-zA-Z0-9]+)/raw/$', 'pastebin.views.snippet_details', {'template_name': 'pastebin/snippet_details_raw.html', 'is_raw': True}, name='snippet_details_raw'), ) + +# Sitemap framework +sitemaps = {"sitemaps": {"all": StaticSitemap('pastebin.geany.org', urlpatterns)}} +urlpatterns += patterns('', + # use our custom sitemap implementation + url(r"^sitemap\.xml$", 'django.contrib.sitemaps.views.sitemap', sitemaps) +)