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

export_userdata.py 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from django.contrib.auth.models import User
  2. from django.core.management.base import BaseCommand
  3. import json
  4. from themes.models import GetSettings
  5. from themes.models import BottomBar
  6. from users.models import UserProfile
  7. from pygal.models import Item, Tag
  8. from ._private import KEY_THEME_SETTINGS
  9. from ._private import KEY_THEME_BOTTOMBAR
  10. from ._private import KEY_USER_PROFILE
  11. from ._private import KEY_USERDATA_FAVOURITE
  12. from ._private import KEY_USERDATA_TAGS
  13. from ._private import KEY_ACCESS_DATA
  14. class Command(BaseCommand):
  15. help = 'Export userdata in JSON-Format.'
  16. def handle(self, *args, **options):
  17. data = {}
  18. #
  19. # Access
  20. #
  21. if KEY_ACCESS_DATA not in data:
  22. data[KEY_ACCESS_DATA] = {}
  23. # public_access
  24. data[KEY_ACCESS_DATA][''] = [item.rel_path for item in Item.objects.filter(public_access=True)]
  25. # user_access
  26. for u in User.objects.all():
  27. data[KEY_ACCESS_DATA][u.username] = {}
  28. data[KEY_ACCESS_DATA][u.username]['read_access'] = [item.rel_path for item in u.read_access.all()]
  29. data[KEY_ACCESS_DATA][u.username]['modify_access'] = [item.rel_path for item in u.modify_access.all()]
  30. #
  31. # Favourites
  32. #
  33. if KEY_USERDATA_FAVOURITE not in data:
  34. data[KEY_USERDATA_FAVOURITE] = {}
  35. for user in User.objects.all():
  36. favourites = user.favourite_of.all()
  37. if len(favourites) > 0:
  38. data[KEY_USERDATA_FAVOURITE][user.username] = [i.rel_path for i in favourites]
  39. #
  40. # Tags
  41. #
  42. if KEY_USERDATA_TAGS not in data:
  43. data[KEY_USERDATA_TAGS] = {}
  44. for tag in Tag.objects.all():
  45. tag_key = tag.export_key()
  46. if tag_key not in data[KEY_USERDATA_TAGS]:
  47. data[KEY_USERDATA_TAGS][tag_key] = []
  48. data[KEY_USERDATA_TAGS][tag.export_key()].append(tag.export_data())
  49. #
  50. # Settings
  51. #
  52. s = GetSettings()
  53. data[KEY_THEME_SETTINGS] = s.export_data()
  54. #
  55. # Bottombar
  56. #
  57. if KEY_THEME_BOTTOMBAR not in data:
  58. data[KEY_THEME_BOTTOMBAR] = []
  59. for bb_entry in BottomBar.objects.all():
  60. data[KEY_THEME_BOTTOMBAR].append(bb_entry.export_data())
  61. #
  62. # Userprofile
  63. #
  64. if KEY_USER_PROFILE not in data:
  65. data[KEY_USER_PROFILE] = {}
  66. for profile in UserProfile.objects.all():
  67. data[KEY_USER_PROFILE][profile.export_key()] = profile.export_data()
  68. self.stdout.write(json.dumps(data, indent=4, sort_keys=True))