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"""
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
41
"""Return all categories with coeff"""
42
categories = Category.objects.all()
43
self.cache(categories)
46
def lastmod(self, obj):
47
"""Return last modification of a category"""
48
entries = entries_published(obj.entry_set)
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]
58
return '%.1f' % priority
61
class AuthorSitemap(Sitemap):
62
"""Sitemap for authors"""
64
changefreq = 'monthly'
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)
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())
91
entries = TaggedItem.objects.get_by_model(
92
Entry.published.all(), tag)
93
self.cache_tags[tag.pk] = (entries, entries.count() / len_entries)
96
"""Return all tags with coeff"""
97
tags = tags_published()
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]
111
return '%.1f' % priority
113
def location(self, obj):
114
"""Return url of a tag"""
115
return reverse('zinnia_tag_detail', args=[obj.name])