Django Library PyGal
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

import_userdata.py 4.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from django.contrib.auth.models import User
  2. from django.core.management.base import BaseCommand
  3. import json
  4. from pygal.models import Item, Tag
  5. import os
  6. from ._private import KEY_USERDATA_VERSION
  7. from ._private import KEY_USERDATA_FAVOURITE
  8. from ._private import KEY_USERDATA_TAGS
  9. class Command(BaseCommand):
  10. help = 'Import userdata from a JSON-File.'
  11. def add_arguments(self, parser):
  12. parser.add_argument('filename(s)', nargs='+', type=str)
  13. def error_skipped_file(self, path, reason):
  14. self.stderr.write(' Skipped file %s caused by an error (%s) !' % (path, reason))
  15. def handle(self, *args, **options):
  16. for fn in options['filename(s)']:
  17. fn = os.path.abspath(fn)
  18. self.stdout.write('Parsing %s:' % fn)
  19. try:
  20. with open(fn, 'r') as fh:
  21. data = json.load(fh)
  22. try:
  23. version = data.pop(KEY_USERDATA_VERSION)
  24. except KeyError:
  25. version = 1
  26. except PermissionError:
  27. self.error_skipped_file(fn, 'file permission')
  28. except json.decoder.JSONDecodeError:
  29. self.error_skipped_file(fn, 'json decoding')
  30. except FileNotFoundError:
  31. self.error_skipped_file(fn, 'file not found')
  32. else:
  33. for section in data:
  34. self.stdout.write(' Importing "%s":' % section)
  35. cnt_all_datasets = 0
  36. cnt_success = 0
  37. cnt_already_exist = 0
  38. cnt_user_missing = 0
  39. cnt_item_missing = 0
  40. if section == KEY_USERDATA_FAVOURITE:
  41. for username in data[section]:
  42. try:
  43. user = User.objects.get(username=username)
  44. except User.DoesNotExist:
  45. user = None
  46. for rel_path in data[section][username]:
  47. cnt_all_datasets += 1
  48. if user is None:
  49. cnt_user_missing += 1
  50. try:
  51. i = Item.objects.get(rel_path=rel_path)
  52. except Item.DoesNotExist:
  53. cnt_item_missing += 1
  54. else:
  55. if user in i.favourite_of.all():
  56. cnt_already_exist += 1
  57. else:
  58. cnt_success += 1
  59. i.favourite_of.add(user)
  60. elif section == KEY_USERDATA_TAGS:
  61. for rel_path in data[section]:
  62. cnt_all_datasets += len(data[section][rel_path])
  63. try:
  64. i = Item.objects.get(rel_path=rel_path)
  65. except Item.DoesNotExist:
  66. cnt_item_missing += len(data[section][rel_path])
  67. else:
  68. for tag_data in data[section][rel_path]:
  69. if len(Tag.objects.filter(item=i, text=tag_data['text'])) > 0:
  70. cnt_already_exist += 1
  71. else:
  72. tag_data['item'] = i
  73. Tag(**tag_data).save()
  74. cnt_success += 1
  75. else:
  76. self.stdout.write(self.style.ERROR(' Section unknown!'))
  77. #
  78. # REPORT
  79. #
  80. if cnt_all_datasets > 0:
  81. if (cnt_success + cnt_already_exist) < cnt_all_datasets:
  82. style = self.style.WARNING
  83. self.stdout.write(style(' NOT all data imported.'))
  84. else:
  85. style = self.style.SUCCESS
  86. self.stdout.write(style(' All data imported.'))
  87. self.stdout.write(style(' %5d %s(s) successfully added.' % (cnt_success, section)))
  88. self.stdout.write(style(' %5d %s(s) already set correctly.' % (cnt_already_exist, section)))
  89. if cnt_user_missing > 0:
  90. self.stdout.write(self.style.ERROR(' %5d %s(s) not added, because the target user does not exist.' % (cnt_user_missing, section)))
  91. if cnt_item_missing > 0:
  92. self.stdout.write(self.style.ERROR(' %5d %s(s) not added, because the target item does not exist.' % (cnt_item_missing, section)))