1
0

sitemaps.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from django.contrib.sitemaps import Sitemap
  2. from blog.models import Blog, Cat_Blog
  3. from core.models import Page
  4. from django.urls import reverse
  5. from django.utils import timezone
  6. class BlogSitemap(Sitemap):
  7. """Sitemap pour les articles de blog"""
  8. changefreq = "weekly"
  9. priority = 0.8
  10. protocol = 'https'
  11. def items(self):
  12. return Blog.objects.filter(b_publier=True).order_by('-b_publdate')
  13. def lastmod(self, obj):
  14. return obj.b_publdate
  15. def location(self, obj):
  16. return reverse('blog_play', args=[obj.b_titre_slugify])
  17. class CategorySitemap(Sitemap):
  18. """Sitemap pour les catégories"""
  19. changefreq = "monthly"
  20. priority = 0.6
  21. protocol = 'https'
  22. def items(self):
  23. return Cat_Blog.objects.all()
  24. def lastmod(self, obj):
  25. # Utilise la date du dernier article publié dans cette catégorie
  26. last_article = Blog.objects.filter(
  27. b_publier=True,
  28. b_cat=obj
  29. ).order_by('-b_publdate').first()
  30. if last_article and last_article.b_publdate:
  31. return last_article.b_publdate
  32. # Retourne la date actuelle si aucun article
  33. return timezone.now()
  34. def location(self, obj):
  35. return reverse('blog_tag', args=[obj.cb_titre_slgify])
  36. class PageSitemap(Sitemap):
  37. """Sitemap pour les pages statiques"""
  38. changefreq = "monthly"
  39. priority = 0.5
  40. protocol = 'https'
  41. def items(self):
  42. return Page.objects.filter(p_publier=True, p_type='page')
  43. def lastmod(self, obj):
  44. # Si la page a un champ de date de modification, l'utiliser
  45. # Sinon retourner la date actuelle
  46. return getattr(obj, 'p_date_modification', timezone.now())
  47. def location(self, obj):
  48. return obj.p_adresse
  49. class StaticViewSitemap(Sitemap):
  50. """Sitemap pour les vues statiques"""
  51. priority = 0.5
  52. changefreq = 'daily' # Changé à 'daily' car l'index change souvent
  53. protocol = 'https'
  54. def items(self):
  55. return ['blog_index']
  56. def lastmod(self, item):
  57. # Date du dernier article publié
  58. last_article = Blog.objects.filter(b_publier=True).order_by('-b_publdate').first()
  59. if last_article and last_article.b_publdate:
  60. return last_article.b_publdate
  61. return timezone.now()
  62. def location(self, item):
  63. return reverse(item)