1
0

migrate_sitemap.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/env python3
  2. """
  3. Script de test et migration du sitemap
  4. 1. Sauvegarde l'ancien sitemap statique
  5. 2. Teste le nouveau sitemap dynamique
  6. 3. Vérifie les URLs générées
  7. """
  8. import os
  9. import sys
  10. import shutil
  11. from datetime import datetime
  12. from pathlib import Path
  13. # Ajouter le répertoire parent au path pour importer Django
  14. sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
  15. # Configuration Django
  16. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'duhaz_blog.settings')
  17. import django
  18. django.setup()
  19. from blog.models import Blog, Cat_Blog
  20. from core.models import Page
  21. from blog.sitemaps import BlogSitemap, CategorySitemap, PageSitemap, StaticViewSitemap
  22. from django.urls import reverse
  23. def backup_old_sitemap():
  24. """Sauvegarde l'ancien sitemap statique"""
  25. old_sitemap = Path(__file__).parent.parent / 'static' / 'sitemap.xml'
  26. if old_sitemap.exists():
  27. timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
  28. backup_path = old_sitemap.parent / f'sitemap_backup_{timestamp}.xml'
  29. print(f"📦 Sauvegarde de l'ancien sitemap...")
  30. shutil.copy2(old_sitemap, backup_path)
  31. print(f"✅ Sauvegardé vers : {backup_path}")
  32. # Renommer l'ancien pour qu'il ne soit plus utilisé
  33. old_renamed = old_sitemap.parent / f'sitemap_OLD_{timestamp}.xml'
  34. old_sitemap.rename(old_renamed)
  35. print(f"✅ Ancien sitemap renommé : {old_renamed}")
  36. return True
  37. else:
  38. print("ℹ️ Aucun ancien sitemap trouvé")
  39. return False
  40. def test_sitemap():
  41. """Teste le nouveau sitemap dynamique"""
  42. print("\n" + "="*70)
  43. print("TEST DU SITEMAP DYNAMIQUE")
  44. print("="*70 + "\n")
  45. # Test BlogSitemap
  46. print("📝 Test BlogSitemap...")
  47. blog_sitemap = BlogSitemap()
  48. blog_items = list(blog_sitemap.items())
  49. print(f" Articles trouvés : {len(blog_items)}")
  50. if blog_items:
  51. print(f" ✓ Premier article : {blog_items[0].b_titre}")
  52. print(f" ✓ URL : {blog_sitemap.location(blog_items[0])}")
  53. print(f" ✓ Dernière modif : {blog_sitemap.lastmod(blog_items[0])}")
  54. # Test CategorySitemap
  55. print("\n🏷️ Test CategorySitemap...")
  56. cat_sitemap = CategorySitemap()
  57. cat_items = list(cat_sitemap.items())
  58. print(f" Catégories trouvées : {len(cat_items)}")
  59. if cat_items:
  60. print(f" ✓ Première catégorie : {cat_items[0].cb_titre}")
  61. print(f" ✓ URL : {cat_sitemap.location(cat_items[0])}")
  62. print(f" ✓ Dernière modif : {cat_sitemap.lastmod(cat_items[0])}")
  63. # Test PageSitemap
  64. print("\n📄 Test PageSitemap...")
  65. page_sitemap = PageSitemap()
  66. page_items = list(page_sitemap.items())
  67. print(f" Pages trouvées : {len(page_items)}")
  68. if page_items:
  69. print(f" ✓ Première page : {page_items[0]}")
  70. # Test StaticViewSitemap
  71. print("\n🏠 Test StaticViewSitemap...")
  72. static_sitemap = StaticViewSitemap()
  73. static_items = list(static_sitemap.items())
  74. print(f" Vues statiques : {len(static_items)}")
  75. for item in static_items:
  76. print(f" ✓ Vue : {item}")
  77. print(f" ✓ URL : {static_sitemap.location(item)}")
  78. # Résumé
  79. total = len(blog_items) + len(cat_items) + len(page_items) + len(static_items)
  80. print("\n" + "="*70)
  81. print("RÉSUMÉ")
  82. print("="*70)
  83. print(f"Total URLs dans le sitemap : {total}")
  84. print(f" - Articles de blog : {len(blog_items)}")
  85. print(f" - Catégories : {len(cat_items)}")
  86. print(f" - Pages statiques : {len(page_items)}")
  87. print(f" - Vues statiques : {len(static_items)}")
  88. return total
  89. def main():
  90. print("\n🚀 MIGRATION VERS SITEMAP DYNAMIQUE")
  91. print("="*70 + "\n")
  92. # Étape 1 : Sauvegarde
  93. backup_old_sitemap()
  94. # Étape 2 : Test
  95. total_urls = test_sitemap()
  96. # Étape 3 : Instructions
  97. print("\n" + "="*70)
  98. print("PROCHAINES ÉTAPES")
  99. print("="*70)
  100. print("\n1. ✅ Sitemap dynamique configuré")
  101. print("2. 🌐 URL du sitemap : https://www.duhaz.fr/sitemap.xml")
  102. print("3. 📊 Soumettre à Google Search Console :")
  103. print(" - Aller sur https://search.google.com/search-console")
  104. print(" - Sitemaps → Ajouter un sitemap")
  105. print(" - Entrer : sitemap.xml")
  106. print("4. 🔄 Le sitemap se met à jour automatiquement")
  107. print("\n⚠️ IMPORTANT : Redémarrez Django pour activer les changements")
  108. print(" python manage.py runserver")
  109. print("\n✅ Migration terminée avec succès !")
  110. if __name__ == "__main__":
  111. main()