syncdatasource.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from django.core.management.base import BaseCommand, CommandError
  2. from core.choices import DataSourceStatusChoices
  3. from core.models import DataSource
  4. class Command(BaseCommand):
  5. help = "Synchronize a data source from its remote upstream"
  6. def add_arguments(self, parser):
  7. parser.add_argument('name', nargs='*', help="Data source(s) to synchronize")
  8. parser.add_argument(
  9. "--all", action='store_true', dest='sync_all',
  10. help="Synchronize all data sources"
  11. )
  12. def handle(self, *args, **options):
  13. # Find DataSources to sync
  14. if options['sync_all']:
  15. datasources = DataSource.objects.all()
  16. elif options['name']:
  17. datasources = DataSource.objects.filter(name__in=options['name'])
  18. # Check for invalid names
  19. found_names = {ds['name'] for ds in datasources.values('name')}
  20. if invalid_names := set(options['name']) - found_names:
  21. raise CommandError(f"Invalid data source names: {', '.join(invalid_names)}")
  22. else:
  23. raise CommandError("Must specify at least one data source, or set --all.")
  24. if len(options['name']) > 1:
  25. self.stdout.write(f"Syncing {len(datasources)} data sources.")
  26. for i, datasource in enumerate(datasources, start=1):
  27. self.stdout.write(f"[{i}] Syncing {datasource}... ", ending='')
  28. self.stdout.flush()
  29. try:
  30. datasource.sync()
  31. self.stdout.write(datasource.get_status_display())
  32. self.stdout.flush()
  33. except Exception as e:
  34. DataSource.objects.filter(pk=datasource.pk).update(status=DataSourceStatusChoices.FAILED)
  35. raise e
  36. if len(options['name']) > 1:
  37. self.stdout.write("Finished.")