add_org_data.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import os
  2. import sys
  3. import time
  4. import django
  5. import json
  6. local_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  7. print(local_path)
  8. if local_path not in sys.path:
  9. sys.path.append(local_path)
  10. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "kedong.settings")
  11. django.setup()
  12. from django.conf import settings
  13. from smartfarming.models.device import MongoDeviceType
  14. from smartfarming.models.user import UserPurview
  15. from smartfarming.models.pests_bank import MongoPestBank
  16. with open(os.path.join(local_path, "scripts/test/sa_device_type.json") , 'r', encoding='utf-8') as f:
  17. sa_device_type = json.load(f)
  18. for row in sa_device_type.get("rows"):
  19. device_type, is_created = MongoDeviceType.objects.get_or_create(
  20. id=row.get("id"),
  21. defaults=row
  22. )
  23. print(device_type.id, is_created)
  24. with open(os.path.join(local_path, "scripts/test/sa_user_purview.json") , 'r', encoding='utf-8') as f:
  25. sa_user_purview = json.load(f)
  26. for row in sa_user_purview.get("rows"):
  27. user_purview, is_created = UserPurview.objects.get_or_create(
  28. id=row.get("id"),
  29. defaults=row
  30. )
  31. print(user_purview.id, is_created)
  32. with open(os.path.join(local_path, "scripts/test/sa_pest_bank.json") , 'r', encoding='utf-8') as f:
  33. sa_pest_bank = json.load(f)
  34. for row in sa_pest_bank.get("rows"):
  35. pest_bank, is_created = MongoPestBank.objects.get_or_create(
  36. id=row.get("id"),
  37. defaults=row
  38. )
  39. print(pest_bank.id, is_created)