You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sitemaps.py 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # -*- coding: utf-8 -*-
  2. from django.conf import settings
  3. from django.contrib.sitemaps import Sitemap
  4. from django.urls import reverse
  5. from django.utils.translation import get_language, activate
  6. from oscar.core.loading import get_model
  7. Product = get_model('catalogue', 'Product')
  8. Category = get_model('catalogue', 'Category')
  9. """
  10. A basic example what a sitemap could look like for a multi-language Oscar
  11. instance.
  12. Creates entries for the homepage, for each product and each category.
  13. Repeats those for each enabled language.
  14. """
  15. class I18nSitemap(Sitemap):
  16. """
  17. A language-specific Sitemap class. Returns URLS for items for passed
  18. language.
  19. """
  20. def __init__(self, language):
  21. self.language = language
  22. self.original_language = get_language()
  23. def get_obj_location(self, obj):
  24. return obj.get_absolute_url()
  25. def location(self, obj):
  26. activate(self.language)
  27. location = self.get_obj_location(obj)
  28. activate(self.original_language)
  29. return location
  30. class StaticSitemap(I18nSitemap):
  31. def items(self):
  32. return ['home', ]
  33. def get_obj_location(self, obj):
  34. print("StaticSitemap!")
  35. return reverse(obj)
  36. class ProductSitemap(I18nSitemap):
  37. def items(self):
  38. print("ProductSitemap!")
  39. return Product.objects.browsable()
  40. class CategorySitemap(I18nSitemap):
  41. def items(self):
  42. print("CategorySitemap!")
  43. return Category.objects.all()
  44. language_neutral_sitemaps = {
  45. 'static': StaticSitemap,
  46. 'products': ProductSitemap,
  47. 'categories': CategorySitemap,
  48. }
  49. # Construct the sitemaps for every language
  50. base_sitemaps = {}
  51. for language, __ in settings.LANGUAGES:
  52. for name, sitemap_class in language_neutral_sitemaps.items():
  53. base_sitemaps['{0}-{1}'.format(name, language)] = sitemap_class(language)