sitemaps.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from __future__ import unicode_literals
  2. from django.contrib.sitemaps import Sitemap
  3. from django.contrib.sites.models import Site
  4. from mezzanine.conf import settings
  5. from mezzanine.core.models import Displayable
  6. from mezzanine.utils.sites import current_site_id
  7. blog_installed = "mezzanine.blog" in settings.INSTALLED_APPS
  8. if blog_installed:
  9. from mezzanine.blog.models import BlogPost
  10. class DisplayableSitemap(Sitemap):
  11. """
  12. Sitemap class for Django's sitemaps framework that returns
  13. all published items for models that subclass ``Displayable``.
  14. """
  15. def items(self):
  16. """
  17. Return all published items for models that subclass
  18. ``Displayable``, excluding those that point to external sites.
  19. """
  20. return list(Displayable.objects.url_map(in_sitemap=True).values())
  21. def lastmod(self, obj):
  22. if blog_installed and isinstance(obj, BlogPost):
  23. return obj.updated or obj.publish_date
  24. def get_urls(self, **kwargs):
  25. """
  26. Ensure the correct host by injecting the current site.
  27. """
  28. kwargs["site"] = Site.objects.get(id=current_site_id())
  29. return super(DisplayableSitemap, self).get_urls(**kwargs)