Source

djangobook-cn / docs / sitemaps.py

# coding=utf-8
#!/usr/bin/env python
#
# The Django Book: Japanese translation of free online Django books.
#
# @package	docs
# @identifier	docs/sitemap.py
# @version	1.0
# @rights	GPLv3 - http://www.fsf.org/licensing/licenses/gpl.html
# @creator	Kosei Kitahara - http://surgo.jp/
# @issued	2009-03-01
# @modified	2009-03-01
# @references	http://djangobook.appspot.com/
#
# Copyright (C) 2009 Kosei Kitahara. - http://surgo.jp/
# 
#  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/.

import os
import datetime
import cPickle as pickle

from django.contrib.sitemaps import Sitemap
from django.core import urlresolvers

import configs

class DocsSitemap(Sitemap):
    changefreq = "never"
    
    def items(self):
        paths = []
        for dirpath, dirnames, filenames in os.walk(configs.DOCS_SOURCE_PATH):
            head, tail = os.path.split(dirpath)
            for filename in filenames:
                name, ext = os.path.splitext(filename)
                if ext == configs.DOCS_SOURCE_EXTENSION:
                    if tail == '_sources':
                        paths = paths+[name]
                    else:
                        paths = paths+[tail+'/'+name]
        return paths

    def priority(self, obj):
        priority = 0.5
        if obj == 'index':
            priority = 1.0
        else:
            bits = str(obj).split('/')
            if bits[-1] == 'index':
                priority = priority + 0.3
        return priority
    
    def lastmod(self, obj):
        return datetime.datetime.fromtimestamp(os.stat(os.path.join(configs.DOCS_PICKLE_PATH, 'last_build')).st_mtime)

    def location(self, obj):
        if str(obj) == 'index':
            return urlresolvers.reverse(
                'document_index',
            )
        else:
            return urlresolvers.reverse(
                'document_detail',
                kwargs={'url':str(obj).replace('/index', '')}
            )

sitemaps = {'docs':DocsSitemap}