zinnia.sitemaps
Covered: 91 lines
Missed: 0 lines
Skipped 25 lines
Percent: 100 %
  1
"""Sitemaps for Zinnia"""
  2
from django.contrib.sitemaps import Sitemap
  3
from django.core.urlresolvers import reverse
  5
from tagging.models import TaggedItem
  7
from zinnia.models import Entry
  8
from zinnia.models import Author
  9
from zinnia.models import Category
 10
from zinnia.managers import tags_published
 11
from zinnia.managers import entries_published
 14
class EntrySitemap(Sitemap):
 15
    """Sitemap for entries"""
 16
    priority = 0.5
 17
    changefreq = 'never'
 19
    def items(self):
 20
        """Return published entries"""
 21
        return Entry.published.all()
 23
    def lastmod(self, obj):
 24
        """Return last modification of an entry"""
 25
        return obj.last_update
 28
class CategorySitemap(Sitemap):
 29
    """Sitemap for categories"""
 30
    changefreq = 'monthly'
 32
    def cache(self, categories):
 33
        """Cache categorie's entries percent on total entries"""
 34
        len_entries = float(Entry.published.count())
 35
        self.cache_categories = {}
 36
        for cat in categories:
 37
            self.cache_categories[cat.pk] = cat.entries_published_set(
 38
                ).count() / len_entries
 40
    def items(self):
 41
        """Return all categories with coeff"""
 42
        categories = Category.objects.all()
 43
        self.cache(categories)
 44
        return categories
 46
    def lastmod(self, obj):
 47
        """Return last modification of a category"""
 48
        entries = entries_published(obj.entry_set)
 49
        if not entries:
 50
            return None
 51
        return entries[0].creation_date
 53
    def priority(self, obj):
 54
        """Compute priority with cached coeffs"""
 55
        priority = 0.5 + self.cache_categories[obj.pk]
 56
        if priority > 1.0:
 57
            priority = 1.0
 58
        return '%.1f' % priority
 61
class AuthorSitemap(Sitemap):
 62
    """Sitemap for authors"""
 63
    priority = 0.5
 64
    changefreq = 'monthly'
 66
    def items(self):
 67
        """Return published authors"""
 68
        return Author.published.all()
 70
    def lastmod(self, obj):
 71
        """Return last modification of an author"""
 72
        entries = entries_published(obj.entry_set)
 73
        if not entries:
 74
            return None
 75
        return entries[0].creation_date
 77
    def location(self, obj):
 78
        """Return url of an author"""
 79
        return reverse('zinnia_author_detail', args=[obj.username])
 82
class TagSitemap(Sitemap):
 83
    """Sitemap for tags"""
 84
    changefreq = 'monthly'
 86
    def cache(self, tags):
 87
        """Cache tag's entries percent on total entries"""
 88
        len_entries = float(Entry.published.count())
 89
        self.cache_tags = {}
 90
        for tag in tags:
 91
            entries = TaggedItem.objects.get_by_model(
 92
                Entry.published.all(), tag)
 93
            self.cache_tags[tag.pk] = (entries, entries.count() / len_entries)
 95
    def items(self):
 96
        """Return all tags with coeff"""
 97
        tags = tags_published()
 98
        self.cache(tags)
 99
        return tags
101
    def lastmod(self, obj):
102
        """Return last modification of a tag"""
103
        entries = self.cache_tags[obj.pk][0]
104
        return entries[0].creation_date
106
    def priority(self, obj):
107
        """Compute priority with cached coeffs"""
108
        priority = 0.5 + self.cache_tags[obj.pk][1]
109
        if priority > 1.0:
110
            priority = 1.0
111
        return '%.1f' % priority
113
    def location(self, obj):
114
        """Return url of a tag"""
115
        return reverse('zinnia_tag_detail', args=[obj.name])