sitemaps.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. # IMPORTANT : Ordonner le QuerySet pour éviter le warning de pagination
  24. return Cat_Blog.objects.all().order_by('cb_titre')
  25. def lastmod(self, obj):
  26. # Utilise la date du dernier article publié dans cette catégorie
  27. last_article = Blog.objects.filter(
  28. b_publier=True,
  29. b_cat=obj
  30. ).order_by('-b_publdate').first()
  31. if last_article and last_article.b_publdate:
  32. return last_article.b_publdate
  33. # Retourne la date actuelle si aucun article
  34. return timezone.now()
  35. def location(self, obj):
  36. return reverse('blog_tag', args=[obj.cb_titre_slgify])
  37. class PageSitemap(Sitemap):
  38. """Sitemap pour les pages statiques"""
  39. changefreq = "monthly"
  40. priority = 0.5
  41. protocol = 'https'
  42. def items(self):
  43. # IMPORTANT : Ordonner le QuerySet pour éviter le warning de pagination
  44. return Page.objects.filter(p_publier=True, p_type='page').order_by('p_adresse')
  45. def lastmod(self, obj):
  46. # Si la page a un champ de date de modification, l'utiliser
  47. # Sinon retourner la date actuelle
  48. return getattr(obj, 'p_date_modification', timezone.now())
  49. def location(self, obj):
  50. return obj.p_adresse
  51. class StaticViewSitemap(Sitemap):
  52. """Sitemap pour les vues statiques"""
  53. priority = 0.5
  54. changefreq = 'daily' # Changé à 'daily' car l'index change souvent
  55. protocol = 'https'
  56. def items(self):
  57. return ['blog_index']
  58. def lastmod(self, item):
  59. # Date du dernier article publié
  60. last_article = Blog.objects.filter(b_publier=True).order_by('-b_publdate').first()
  61. if last_article and last_article.b_publdate:
  62. return last_article.b_publdate
  63. return timezone.now()
  64. def location(self, item):
  65. return reverse(item)