from django.contrib.auth.models import User from django.core.management.base import BaseCommand import json from pygal.models import Tag from ._private import KEY_USERDATA_VERSION from ._private import KEY_USERDATA_FAVOURITE from ._private import KEY_USERDATA_TAGS class Command(BaseCommand): help = 'Export userdata in JSON-Format.' def handle(self, *args, **options): data = {} data[KEY_USERDATA_VERSION] = 1 for user in User.objects.all(): favourites = user.favourite_of.all() if len(favourites) > 0: if KEY_USERDATA_FAVOURITE not in data: data[KEY_USERDATA_FAVOURITE] = {} data[KEY_USERDATA_FAVOURITE][user.username] = [i.rel_path for i in favourites] for tag in Tag.objects.all(): if KEY_USERDATA_TAGS not in data: data[KEY_USERDATA_TAGS] = {} if tag.item.rel_path not in data[KEY_USERDATA_TAGS]: data[KEY_USERDATA_TAGS][tag.item.rel_path] = [] tagdata = {} tagdata['text'] = tag.text if tag.has_valid_coordinates: tagdata['topleft_x'] = tag.topleft_x tagdata['topleft_y'] = tag.topleft_y tagdata['bottomright_x'] = tag.bottomright_x tagdata['bottomright_y'] = tag.bottomright_y data[KEY_USERDATA_TAGS][tag.item.rel_path].append(tagdata) self.stdout.write(json.dumps(data, indent=4, sort_keys=True))