您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

sitemaps.py 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. print("I18nSitemap! __init__")
  22. self.language = language
  23. self.original_language = get_language()
  24. def get_obj_location(self, obj):
  25. print("I18nSitemap! get_obj_location")
  26. return obj.get_absolute_url()
  27. def location(self, obj):
  28. print("I18nSitemap! location")
  29. activate(self.language)
  30. location = self.get_obj_location(obj)
  31. activate(self.original_language)
  32. return location
  33. class StaticSitemap(I18nSitemap):
  34. def items(self):
  35. print("StaticSitemap! items")
  36. return ['home', ]
  37. def get_obj_location(self, obj):
  38. print("StaticSitemap!")
  39. return reverse(obj)
  40. class ProductSitemap(I18nSitemap):
  41. def items(self):
  42. print("ProductSitemap!")
  43. return Product.objects.browsable()
  44. class CategorySitemap(I18nSitemap):
  45. def items(self):
  46. print("CategorySitemap!")
  47. return Category.objects.all()
  48. language_neutral_sitemaps = {
  49. 'static': StaticSitemap,
  50. 'products': ProductSitemap,
  51. 'categories': CategorySitemap,
  52. }
  53. # Construct the sitemaps for every language
  54. base_sitemaps = {}
  55. for language, __ in settings.LANGUAGES:
  56. for name, sitemap_class in language_neutral_sitemaps.items():
  57. base_sitemaps['{0}-{1}'.format(name, language)] = sitemap_class(language)