Django Library PyGal
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

export_userdata.py 1.5KB

12345678910111213141516171819202122232425262728293031323334353637
  1. from django.contrib.auth.models import User
  2. from django.core.management.base import BaseCommand
  3. import json
  4. from pygal.models import Tag
  5. from ._private import KEY_USERDATA_VERSION
  6. from ._private import KEY_USERDATA_FAVOURITE
  7. from ._private import KEY_USERDATA_TAGS
  8. class Command(BaseCommand):
  9. help = 'Export userdata in JSON-Format.'
  10. def handle(self, *args, **options):
  11. data = {}
  12. data[KEY_USERDATA_VERSION] = 1
  13. for user in User.objects.all():
  14. favourites = user.favourite_of.all()
  15. if len(favourites) > 0:
  16. if KEY_USERDATA_FAVOURITE not in data:
  17. data[KEY_USERDATA_FAVOURITE] = {}
  18. data[KEY_USERDATA_FAVOURITE][user.username] = [i.rel_path for i in favourites]
  19. for tag in Tag.objects.all():
  20. if KEY_USERDATA_TAGS not in data:
  21. data[KEY_USERDATA_TAGS] = {}
  22. if tag.item.rel_path not in data[KEY_USERDATA_TAGS]:
  23. data[KEY_USERDATA_TAGS][tag.item.rel_path] = []
  24. tagdata = {}
  25. tagdata['text'] = tag.text
  26. if tag.has_valid_coordinates:
  27. tagdata['topleft_x'] = tag.topleft_x
  28. tagdata['topleft_y'] = tag.topleft_y
  29. tagdata['bottomright_x'] = tag.bottomright_x
  30. tagdata['bottomright_y'] = tag.bottomright_y
  31. data[KEY_USERDATA_TAGS][tag.item.rel_path].append(tagdata)
  32. self.stdout.write(json.dumps(data, indent=4, sort_keys=True))