[geany/www.geany.org] 96d400: Implement /sitemap.xml for NightlyBuilds and Pastebin apps

Enrico Tröger git-noreply at xxxxx
Tue Oct 29 21:49:03 UTC 2013


Branch:      refs/heads/master
Author:      Enrico Tröger <enrico.troeger at uvena.de>
Committer:   Enrico Tröger <enrico.troeger at uvena.de>
Date:        Sun, 09 Jun 2013 10:58:37 UTC
Commit:      96d4000f66db7dd702c862f8b3a221963448eb5a
             https://github.com/geany/www.geany.org/commit/96d4000f66db7dd702c862f8b3a221963448eb5a

Log Message:
-----------
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.


Modified Paths:
--------------
    geany/sitemaps.py
    nightlybuilds/urls.py
    pastebin/urls.py

Modified: geany/sitemaps.py
61 files changed, 59 insertions(+), 2 deletions(-)
===================================================================
@@ -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 <http://www.gnu.org/licenses/>.
 
+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)


Modified: nightlybuilds/urls.py
12 files changed, 10 insertions(+), 2 deletions(-)
===================================================================
@@ -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 <http://www.gnu.org/licenses/>.
 
 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)
+)


Modified: pastebin/urls.py
10 files changed, 9 insertions(+), 1 deletions(-)
===================================================================
@@ -13,8 +13,9 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
-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<snippet_id>[a-zA-Z0-9]+)/delete/$', 'pastebin.views.snippet_delete', name='snippet_delete'),
     url(r'^(?P<snippet_id>[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)
+)



--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).


More information about the Commits mailing list