| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- #!/usr/bin/env python3
- """
- Script de test et migration du sitemap
- 1. Sauvegarde l'ancien sitemap statique
- 2. Teste le nouveau sitemap dynamique
- 3. Vérifie les URLs générées
- """
- import os
- import sys
- import shutil
- from datetime import datetime
- from pathlib import Path
- # Ajouter le répertoire parent au path pour importer Django
- sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
- # Configuration Django
- os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'duhaz_blog.settings')
- import django
- django.setup()
- from blog.models import Blog, Cat_Blog
- from core.models import Page
- from blog.sitemaps import BlogSitemap, CategorySitemap, PageSitemap, StaticViewSitemap
- from django.urls import reverse
- def backup_old_sitemap():
- """Sauvegarde l'ancien sitemap statique"""
- old_sitemap = Path(__file__).parent.parent / 'static' / 'sitemap.xml'
-
- if old_sitemap.exists():
- timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
- backup_path = old_sitemap.parent / f'sitemap_backup_{timestamp}.xml'
-
- print(f"📦 Sauvegarde de l'ancien sitemap...")
- shutil.copy2(old_sitemap, backup_path)
- print(f"✅ Sauvegardé vers : {backup_path}")
-
- # Renommer l'ancien pour qu'il ne soit plus utilisé
- old_renamed = old_sitemap.parent / f'sitemap_OLD_{timestamp}.xml'
- old_sitemap.rename(old_renamed)
- print(f"✅ Ancien sitemap renommé : {old_renamed}")
-
- return True
- else:
- print("ℹ️ Aucun ancien sitemap trouvé")
- return False
- def test_sitemap():
- """Teste le nouveau sitemap dynamique"""
- print("\n" + "="*70)
- print("TEST DU SITEMAP DYNAMIQUE")
- print("="*70 + "\n")
-
- # Test BlogSitemap
- print("📝 Test BlogSitemap...")
- blog_sitemap = BlogSitemap()
- blog_items = list(blog_sitemap.items())
- print(f" Articles trouvés : {len(blog_items)}")
-
- if blog_items:
- print(f" ✓ Premier article : {blog_items[0].b_titre}")
- print(f" ✓ URL : {blog_sitemap.location(blog_items[0])}")
- print(f" ✓ Dernière modif : {blog_sitemap.lastmod(blog_items[0])}")
-
- # Test CategorySitemap
- print("\n🏷️ Test CategorySitemap...")
- cat_sitemap = CategorySitemap()
- cat_items = list(cat_sitemap.items())
- print(f" Catégories trouvées : {len(cat_items)}")
-
- if cat_items:
- print(f" ✓ Première catégorie : {cat_items[0].cb_titre}")
- print(f" ✓ URL : {cat_sitemap.location(cat_items[0])}")
- print(f" ✓ Dernière modif : {cat_sitemap.lastmod(cat_items[0])}")
-
- # Test PageSitemap
- print("\n📄 Test PageSitemap...")
- page_sitemap = PageSitemap()
- page_items = list(page_sitemap.items())
- print(f" Pages trouvées : {len(page_items)}")
-
- if page_items:
- print(f" ✓ Première page : {page_items[0]}")
-
- # Test StaticViewSitemap
- print("\n🏠 Test StaticViewSitemap...")
- static_sitemap = StaticViewSitemap()
- static_items = list(static_sitemap.items())
- print(f" Vues statiques : {len(static_items)}")
-
- for item in static_items:
- print(f" ✓ Vue : {item}")
- print(f" ✓ URL : {static_sitemap.location(item)}")
-
- # Résumé
- total = len(blog_items) + len(cat_items) + len(page_items) + len(static_items)
- print("\n" + "="*70)
- print("RÉSUMÉ")
- print("="*70)
- print(f"Total URLs dans le sitemap : {total}")
- print(f" - Articles de blog : {len(blog_items)}")
- print(f" - Catégories : {len(cat_items)}")
- print(f" - Pages statiques : {len(page_items)}")
- print(f" - Vues statiques : {len(static_items)}")
-
- return total
- def main():
- print("\n🚀 MIGRATION VERS SITEMAP DYNAMIQUE")
- print("="*70 + "\n")
-
- # Étape 1 : Sauvegarde
- backup_old_sitemap()
-
- # Étape 2 : Test
- total_urls = test_sitemap()
-
- # Étape 3 : Instructions
- print("\n" + "="*70)
- print("PROCHAINES ÉTAPES")
- print("="*70)
- print("\n1. ✅ Sitemap dynamique configuré")
- print("2. 🌐 URL du sitemap : https://www.duhaz.fr/sitemap.xml")
- print("3. 📊 Soumettre à Google Search Console :")
- print(" - Aller sur https://search.google.com/search-console")
- print(" - Sitemaps → Ajouter un sitemap")
- print(" - Entrer : sitemap.xml")
- print("4. 🔄 Le sitemap se met à jour automatiquement")
- print("\n⚠️ IMPORTANT : Redémarrez Django pour activer les changements")
- print(" python manage.py runserver")
-
- print("\n✅ Migration terminée avec succès !")
- if __name__ == "__main__":
- main()
|