# -*- coding: utf-8 -*- from django.core.urlresolvers import reverse from django.contrib.sitemaps import Sitemap from project.guestbook.models import Category, Entry class GuestbookIndexViewSitemap(Sitemap): changefreq = 'monthly' priority = None def items(self): self.latest_entry = ( Entry.objects.filter(published=True) .extra(select={'ordering_date': 'CASE WHEN pub_date IS NOT NULL THEN pub_date ELSE created_at END'}) .order_by('-ordering_date')[0] ) return ['guestbook-index'] def location(self, item): return reverse(item) def lastmod(self, item): return self.latest_entry.ordering_date class GuestbookCategorySitemap(Sitemap): changefreq = 'monthly' priority = None def items(self): # если у записи не указана дата публикации, то учитывается дата создания self.all_entries = ( Entry.objects.filter(published=True) .extra(select={'ordering_date': 'CASE WHEN pub_date IS NOT NULL THEN pub_date ELSE created_at END'}) .order_by('-ordering_date') ) return Category.objects.all() def lastmod(self, obj): entry_dates = [e.ordering_date for e in self.all_entries if e.category_id == obj.id] return max(entry_dates)