buildmovesindex.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from misago.core.pgutils import batch_update
  2. from misago.datamover.models import MovedId, OldIdRedirect
  3. from misago.datamover.management.base import BaseCommand
  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 = (
  12. "Builds moves index for redirects from old urls to new ones."
  13. )
  14. def handle(self, *args, **options):
  15. self.stdout.write("Building moves index...")
  16. counter = 1
  17. self.start_timer()
  18. for moved_id in batch_update(MovedId.objects):
  19. counter += 1
  20. if moved_id.model not in MAPPINGS:
  21. continue
  22. OldIdRedirect.objects.create(
  23. model=MAPPINGS[moved_id.model],
  24. old_id=moved_id.old_id,
  25. new_id=moved_id.new_id,
  26. )
  27. summary = "Indexed %s items in %s" % (counter, self.stop_timer())
  28. self.stdout.write(self.style.SUCCESS(summary))