Skip to content

Commit

Permalink
Implement /sitemap.xml for NightlyBuilds and Pastebin apps
Browse files Browse the repository at this point in the history
Introduce StaticSitemap() which reads a Site's URLConf and generates a Sitemap
for all URLs which can be reversed without any arguments.
  • Loading branch information
eht16 committed Jun 9, 2013
1 parent ea338ea commit 96d4000
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
61 changes: 59 additions & 2 deletions geany/sitemaps.py
Expand Up @@ -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

Expand All @@ -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)
12 changes: 10 additions & 2 deletions nightlybuilds/urls.py
Expand Up @@ -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('',
Expand All @@ -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)
)
10 changes: 9 additions & 1 deletion pastebin/urls.py
Expand Up @@ -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('',
Expand All @@ -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)
)

0 comments on commit 96d4000

Please sign in to comment.