buildmovesindex.py 856 B

12345678910111213141516171819202122232425262728293031323334
  1. from ...models import MovedId, OldIdRedirect
  2. from ..base import BaseCommand
  3. MAPPINGS = {
  4. 'attachment': 0,
  5. 'category': 1,
  6. 'post': 2,
  7. 'thread': 3,
  8. 'user': 4,
  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 MovedId.objects.exclude(model='label').iterator():
  19. counter += 1
  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))