from io import StringIO import json from django.core.management import call_command from django.core.management.base import BaseCommand def get_data(model_name, filters=tuple(), exclude_empty=tuple()): out = StringIO() call_command('dumpdata', 'chimere.' + model_name, stdout=out) values = json.loads(out.getvalue()) result = [] for value in values: data = value.copy() if [exc for exc in exclude_empty if not data["fields"][exc]]: continue for attr in filters: data["fields"].pop(attr) result.append(data) return result class Command(BaseCommand): help = "Marker dump for v2" def handle(self, *args, **options): new_markers = [] new_markers += get_data('Marker', filters=('weight', 'normalised_weight')) new_markers += get_data('Marker_categories') new_markers += get_data('Property', filters=('route', 'polygon'), exclude_empty=('marker',)) new_markers += get_data('PictureFile', filters=('route', 'polygon'), exclude_empty=('marker',)) new_markers += get_data('MultimediaFile', filters=('route', 'polygon'), exclude_empty=('marker',)) res = json.dumps(new_markers) self.stdout.write(res)