summaryrefslogtreecommitdiff
path: root/chimere/management/commands/v3_to_v2_markers.py
blob: fad91984b306ccbb063e13e1a6b86a637131cf00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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)