import_flask.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. import json
  2. import re
  3. import sys
  4. from django.core.management.base import BaseCommand, CommandError
  5. from django.contrib.auth import get_user_model
  6. from rxmapp.models import RapportoRicezione, TipoRadio
  7. class Command(BaseCommand):
  8. help = "Closes the specified poll for voting"
  9. date_re = re.compile(r"\d\d\d\d-\d\d-\d\d")
  10. def add_arguments(self, parser):
  11. pass
  12. def handle(self, *args, **options):
  13. data = json.load(sys.stdin)
  14. user = get_user_model().objects.filter(is_superuser=True).first()
  15. tiporadio = TipoRadio.objects.all().first()
  16. cnt = 0
  17. for rapporto_in in data["rapporti"]:
  18. rdata = {
  19. "author": user,
  20. "tipo_radio": tiporadio,
  21. "created": self.date_re.search(rapporto_in["explaination"]).group(0),
  22. }
  23. for key in ("lat", "lng", "comprensibile", "stabilita"):
  24. rdata[key] = rapporto_in[key]
  25. r, created = RapportoRicezione.objects.get_or_create(**rdata)
  26. if created:
  27. cnt += 1
  28. self.stdout.write(self.style.SUCCESS("%d rapporti importati" % cnt))