from django.contrib.auth.models import User from django.core.management.base import BaseCommand import json from themes.models import GetSettings from themes.models import BottomBar from users.models import UserProfile from pygal.models import Item, Tag from ._private import KEY_THEME_SETTINGS from ._private import KEY_THEME_BOTTOMBAR from ._private import KEY_USER_PROFILE from ._private import KEY_USERDATA_FAVOURITE from ._private import KEY_USERDATA_TAGS from ._private import KEY_ACCESS_DATA class Command(BaseCommand): help = 'Export userdata in JSON-Format.' def handle(self, *args, **options): data = {} # # Access # if KEY_ACCESS_DATA not in data: data[KEY_ACCESS_DATA] = {} # public_access data[KEY_ACCESS_DATA][''] = [item.rel_path for item in Item.objects.filter(public_access=True)] # user_access for u in User.objects.all(): data[KEY_ACCESS_DATA][u.username] = {} data[KEY_ACCESS_DATA][u.username]['read_access'] = [item.rel_path for item in u.read_access.all()] data[KEY_ACCESS_DATA][u.username]['modify_access'] = [item.rel_path for item in u.modify_access.all()] # # Favourites # if KEY_USERDATA_FAVOURITE not in data: data[KEY_USERDATA_FAVOURITE] = {} for user in User.objects.all(): favourites = user.favourite_of.all() if len(favourites) > 0: data[KEY_USERDATA_FAVOURITE][user.username] = [i.rel_path for i in favourites] # # Tags # if KEY_USERDATA_TAGS not in data: data[KEY_USERDATA_TAGS] = {} for tag in Tag.objects.all(): tag_key = tag.export_key() if tag_key not in data[KEY_USERDATA_TAGS]: data[KEY_USERDATA_TAGS][tag_key] = [] data[KEY_USERDATA_TAGS][tag.export_key()].append(tag.export_data()) # # Settings # s = GetSettings() data[KEY_THEME_SETTINGS] = s.export_data() # # Bottombar # if KEY_THEME_BOTTOMBAR not in data: data[KEY_THEME_BOTTOMBAR] = [] for bb_entry in BottomBar.objects.all(): data[KEY_THEME_BOTTOMBAR].append(bb_entry.export_data()) # # Userprofile # if KEY_USER_PROFILE not in data: data[KEY_USER_PROFILE] = {} for profile in UserProfile.objects.all(): data[KEY_USER_PROFILE][profile.export_key()] = profile.export_data() self.stdout.write(json.dumps(data, indent=4, sort_keys=True))