add_org_data.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  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. with open(os.path.join(local_path, "scripts/test/sa_device_type.json") , 'r', encoding='utf-8') as f:
  16. sa_device_type = json.load(f)
  17. for row in sa_device_type.get("rows"):
  18. device_type, is_created = MongoDeviceType.objects.get_or_create(
  19. id=row.get("id"),
  20. defaults=row
  21. )
  22. print(device_type, is_created)
  23. with open(os.path.join(local_path, "scripts/test/sa_user_purview.json") , 'r', encoding='utf-8') as f:
  24. sa_user_purview = json.load(f)
  25. for row in sa_user_purview.get("rows"):
  26. desa_user_purviewvice_type, is_created = UserPurview.objects.get_or_create(
  27. id=row.get("id"),
  28. defaults=row
  29. )
  30. print(sa_user_purview, is_created)