Skip to content

Instantly share code, notes, and snippets.

@crodjer
Created April 10, 2011 16:00
Show Gist options
  • Save crodjer/912464 to your computer and use it in GitHub Desktop.
Save crodjer/912464 to your computer and use it in GitHub Desktop.
Add sitemaps to universal subtitles. To apply do `touch sitemaps.py` in project root directory first
diff --git a/settings.py b/settings.py
index ef8903e..9324082 100644
--- a/settings.py
+++ b/settings.py
@@ -347,6 +347,7 @@ INSTALLED_APPS = (
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
+ 'django.contrib.sitemaps',
'django.contrib.admin',
'django.contrib.markup',
'django_extensions',
diff --git a/sitemaps.py b/sitemaps.py
index e69de29..18804da 100644
--- a/sitemaps.py
+++ b/sitemaps.py
@@ -0,0 +1,67 @@
+from django.contrib.sitemaps import Sitemap
+from videos.models import Video
+import datetime
+
+DEFAULT_CHANGEFREQ = "monthly"
+DEFAULT_PRIORITY = 0.6
+DEFAULT_LASTMOD = datetime.datetime(2011, 3, 1)
+
+class AbstractSitemap(object):
+ '''
+ An abstract sitemap class to be used for static pages.
+ '''
+ def __init__(self, page, changefreq=DEFAULT_CHANGEFREQ,
+ priority=DEFAULT_PRIORITY, lastmod=DEFAULT_LASTMOD):
+ self.url = page
+ self.changefreq=changefreq
+ self.priority=priority
+ self.lastmod=lastmod
+
+ def get_absolute_url(self):
+ return self.url
+
+AS = AbstractSitemap
+
+class StaticSitemap(Sitemap):
+ '''
+ Definition of static pages, which more or less remain the same
+ and are not based on the database data.
+ '''
+ pages = [
+ AS('/', "weekly", 1), #Home
+ AS('/services/'), #Services
+ AS('/faq/'), #FAQ
+ # Add more static pages
+ ]
+
+ def items(self):
+ return self.pages
+
+ def changefreq(self, obj):
+ return obj.changefreq
+
+ def priority(self, obj):
+ return obj.priority
+
+ def lastmod(self, obj):
+ return obj.lastmod
+
+class VideoSitemap(Sitemap):
+ '''
+ Definition of video pages, based on the videos available on site.
+ TODO: Set video last modification time according to latest subtitle edition
+ '''
+ changefreq="weekly"
+ priority = 0.8
+
+ def items(self):
+ return Video.objects.all()
+
+ def lastmod(self, obj):
+ edited = obj.edited
+ return obj.edited
+
+sitemaps = {
+ 'video':VideoSitemap,
+ 'static':StaticSitemap,
+ }
diff --git a/urls.py b/urls.py
index edafb23..c4eda7a 100644
--- a/urls.py
+++ b/urls.py
@@ -19,6 +19,7 @@
from django.conf.urls.defaults import *
from django.conf import settings
from socialauth.models import AuthMeta, OpenidProfile, TwitterUserProfile, FacebookUserProfile
+from sitemaps import sitemaps
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
@@ -99,7 +100,8 @@ urlpatterns = patterns(
(r'^test-ogg$', 'django.views.generic.simple.direct_to_template',
{'template': 'alpha-test01-ogg.htm'}, 'test-ogg-page'),
(r'^test-mp4$', 'django.views.generic.simple.direct_to_template',
- {'template': 'alpha-test01-mp4.htm'}, 'test-mp4-page'),
+ {'template': 'alpha-test01-mp4.htm'}, 'test-mp4-page'),
+ url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}, name='sitemap'),
)
if settings.DEBUG:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment