buildmovesindex.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. from misago.core.pgutils import batch_update
  2. from misago.datamover.management.base import BaseCommand
  3. from misago.datamover.models import MovedId, OldIdRedirect
  4. MAPPINGS = {
  5. 'category': OldIdRedirect.CATEGORY,
  6. 'post': OldIdRedirect.POST,
  7. 'thread': OldIdRedirect.THREAD,
  8. 'user': OldIdRedirect.USER,
  9. }
  10. class Command(BaseCommand):
  11. help = ("Builds moves index for redirects from old urls to new ones.")
  12. def handle(self, *args, **options):
  13. self.stdout.write("Building moves index...")
  14. counter = 1
  15. self.start_timer()
  16. for moved_id in batch_update(MovedId.objects):
  17. counter += 1
  18. if moved_id.model not in MAPPINGS:
  19. continue
  20. OldIdRedirect.objects.create(
  21. model=MAPPINGS[moved_id.model],
  22. old_id=moved_id.old_id,
  23. new_id=moved_id.new_id,
  24. )
  25. summary = "Indexed %s items in %s" % (counter, self.stop_timer())
  26. self.stdout.write(self.style.SUCCESS(summary))