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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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, TagExist
  5. from themes.models import GetSettings, SettingExist, BottomBar, BottomBarExist
  6. import os
  7. from ._private import KEY_USERDATA_FAVOURITE
  8. from ._private import KEY_USERDATA_TAGS
  9. from ._private import KEY_USER_PROFILE
  10. from ._private import KEY_THEME_SETTINGS
  11. from ._private import KEY_THEME_BOTTOMBAR
  12. from users.models import UserProfile, UserprofilerExist
  13. from pygal.management.commands._private import KEY_ACCESS_DATA
  14. class Command(BaseCommand):
  15. help = 'Import userdata from a JSON-File.'
  16. def add_arguments(self, parser):
  17. parser.add_argument('filename(s)', nargs='+', type=str)
  18. def error_skipped_file(self, path, reason):
  19. self.stderr.write(' Skipped file %s caused by an error (%s) !' % (path, reason))
  20. def handle(self, *args, **options):
  21. for fn in options['filename(s)']:
  22. fn = os.path.abspath(fn)
  23. self.stdout.write('Parsing %s:' % fn)
  24. try:
  25. with open(fn, 'r') as fh:
  26. data = json.load(fh)
  27. except PermissionError:
  28. self.error_skipped_file(fn, 'file permission')
  29. except json.decoder.JSONDecodeError:
  30. self.error_skipped_file(fn, 'json decoding')
  31. except FileNotFoundError:
  32. self.error_skipped_file(fn, 'file not found')
  33. else:
  34. for section in data:
  35. self.stdout.write(' Importing "%s":' % section)
  36. cnt_all_datasets = 0
  37. cnt_success = 0
  38. cnt_already_exist = 0
  39. cnt_user_missing = 0
  40. missing_users = []
  41. cnt_item_missing = 0
  42. #
  43. # Access
  44. #
  45. if section == KEY_ACCESS_DATA:
  46. for username in data[section]:
  47. if username == '':
  48. # public_access
  49. for rel_path in data[section][username]:
  50. cnt_all_datasets += 1
  51. try:
  52. item = Item.objects.get(rel_path=rel_path)
  53. except Item.DoesNotExist:
  54. cnt_item_missing += 1
  55. else:
  56. if item.public_access:
  57. cnt_already_exist += 1
  58. else:
  59. item.public_access = True
  60. item.save()
  61. cnt_success += 1
  62. else:
  63. try:
  64. user = User.objects.get(username=username)
  65. except User.DoesNotExist:
  66. user = None
  67. # read_access
  68. for rel_path in data[section][username]['read_access']:
  69. cnt_all_datasets += 1
  70. if user is None:
  71. cnt_user_missing += 1
  72. if username not in missing_users:
  73. missing_users.append(username)
  74. else:
  75. try:
  76. item = Item.objects.get(rel_path=rel_path)
  77. except Item.DoesNotExist:
  78. cnt_item_missing += 1
  79. else:
  80. if user in item.read_access.all():
  81. cnt_already_exist += 1
  82. else:
  83. item.read_access.add(user)
  84. item.save()
  85. cnt_success += 1
  86. # modify_access
  87. for rel_path in data[section][username]['modify_access']:
  88. cnt_all_datasets += 1
  89. if user is None:
  90. cnt_user_missing += 1
  91. if username not in missing_users:
  92. missing_users.append(username)
  93. else:
  94. try:
  95. item = Item.objects.get(rel_path=rel_path)
  96. except Item.DoesNotExist:
  97. cnt_item_missing += 1
  98. else:
  99. if user in item.modify_access.all():
  100. cnt_already_exist += 1
  101. else:
  102. item.modify_access.add(user)
  103. item.save()
  104. cnt_success += 1
  105. #
  106. # Favourites
  107. #
  108. elif section == KEY_USERDATA_FAVOURITE:
  109. for username in data[section]:
  110. try:
  111. user = User.objects.get(username=username)
  112. except User.DoesNotExist:
  113. user = None
  114. for rel_path in data[section][username]:
  115. cnt_all_datasets += 1
  116. if user is None:
  117. cnt_user_missing += 1
  118. if username not in missing_users:
  119. missing_users.append(username)
  120. try:
  121. i = Item.objects.get(rel_path=rel_path)
  122. except Item.DoesNotExist:
  123. cnt_item_missing += 1
  124. else:
  125. if user in i.favourite_of.all():
  126. cnt_already_exist += 1
  127. elif user is not None:
  128. cnt_success += 1
  129. i.favourite_of.add(user)
  130. #
  131. # Tags
  132. #
  133. elif section == KEY_USERDATA_TAGS:
  134. for rel_path in data[section]:
  135. cnt_all_datasets += len(data[section][rel_path])
  136. try:
  137. i = Item.objects.get(rel_path=rel_path)
  138. except Item.DoesNotExist:
  139. cnt_item_missing += len(data[section][rel_path])
  140. else:
  141. for tag_data in data[section][rel_path]:
  142. if TagExist(i, tag_data):
  143. cnt_already_exist += 1
  144. else:
  145. tag_data['item'] = i
  146. Tag(**tag_data).save()
  147. cnt_success += 1
  148. #
  149. # Settings
  150. #
  151. elif section == KEY_THEME_SETTINGS:
  152. cnt_all_datasets = 1
  153. if SettingExist(data[section]):
  154. cnt_already_exist += 1
  155. else:
  156. s = GetSettings()
  157. s.import_data(data[section])
  158. cnt_success += 1
  159. #
  160. # Bottombar
  161. #
  162. elif section == KEY_THEME_BOTTOMBAR:
  163. cnt_all_datasets = len(data[section])
  164. for bb_entry_import in data[section]:
  165. if BottomBarExist(bb_entry_import):
  166. cnt_already_exist += 1
  167. else:
  168. bb_entry = BottomBar()
  169. bb_entry.import_data(bb_entry_import)
  170. cnt_success += 1
  171. #
  172. # Userprofile
  173. #
  174. elif section == KEY_USER_PROFILE:
  175. cnt_all_datasets = len(data[section])
  176. for username in data[section]:
  177. if UserprofilerExist(username, data[section][username]):
  178. cnt_already_exist += 1
  179. else:
  180. try:
  181. user = User.objects.get(username=username)
  182. except User.DoesNotExist:
  183. cnt_user_missing += 1
  184. missing_users.append(username)
  185. else:
  186. profile = UserProfile(user=user)
  187. profile.import_data(data[section][username])
  188. cnt_success += 1
  189. else:
  190. self.stdout.write(self.style.ERROR(' Section unknown!'))
  191. #
  192. # REPORT
  193. #
  194. if cnt_all_datasets > 0:
  195. if (cnt_success + cnt_already_exist) < cnt_all_datasets:
  196. style = self.style.WARNING
  197. self.stdout.write(style(' NOT all data imported.'))
  198. else:
  199. style = self.style.SUCCESS
  200. self.stdout.write(style(' All data imported.'))
  201. self.stdout.write(style(' %5d %s(s) successfully added.' % (cnt_success, section)))
  202. self.stdout.write(style(' %5d %s(s) already set correctly.' % (cnt_already_exist, section)))
  203. if cnt_user_missing > 0:
  204. self.stdout.write(self.style.ERROR(' %5d %s(s) not added, because the target user(s) %s do(es) not exist.' % (cnt_user_missing, section, repr(missing_users))))
  205. if cnt_item_missing > 0:
  206. self.stdout.write(self.style.ERROR(' %5d %s(s) not added, because the target item does not exist. Try python manage.py rebuild_cache to create items, if you import to a scratch database.' % (cnt_item_missing, section)))