syncfixtures.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from optparse import make_option
  2. import os.path
  3. import pkgutil
  4. from django.core.management.base import BaseCommand
  5. from misago.models import Fixture
  6. from misago.utils.fixtures import load_fixture, update_fixture
  7. import misago.fixtures
  8. class Command(BaseCommand):
  9. """
  10. Loads Misago fixtures
  11. """
  12. help = 'Load Misago fixtures'
  13. option_list = BaseCommand.option_list + (
  14. make_option('--quiet',
  15. action='store_true',
  16. dest='quiet',
  17. default=False,
  18. help='Dont display output from this message'),
  19. )
  20. def handle(self, *args, **options):
  21. if not options['quiet']:
  22. self.stdout.write('\nLoading data from fixtures...')
  23. fixture_data = {}
  24. for fixture in Fixture.objects.all():
  25. fixture_data[fixture.name] = fixture
  26. loaded = 0
  27. updated = 0
  28. fixtures_path = os.path.dirname(misago.fixtures.__file__)
  29. for _, name, _ in pkgutil.iter_modules([fixtures_path]):
  30. if name in fixture_data:
  31. if update_fixture('misago.fixtures.' + name):
  32. updated += 1
  33. if not options['quiet']:
  34. self.stdout.write('Updating "%s" fixture...' % name)
  35. else:
  36. if load_fixture('misago.fixtures.' + name):
  37. loaded += 1
  38. Fixture.objects.create(name=name)
  39. if not options['quiet']:
  40. self.stdout.write('Loading "%s" fixture...' % name)
  41. if not options['quiet']:
  42. self.stdout.write('\nLoaded %s fixtures and updated %s fixtures.\n' % (loaded, updated))