export_package.py 3.72 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from django.core.management.base import BaseCommand, CommandError
from workflows.models import Category, AbstractWidget, AbstractInput, AbstractOutput, AbstractOption
from django.core import serializers
from optparse import make_option
import uuid

class Command(BaseCommand):
    args = 'file_name package_name <package_name2, package_name3, ...>'
    help = 'Exports all AbstractWidgets from a package "package_name" to a file named "file_name". Exports also all needed connected models Category, AbstractInput, AbstractOutput, and AbstractOption.'
    option_list = BaseCommand.option_list + (
        make_option('-n', '--newuid',
            action="store_true",
            dest='newuid',
            default=False,
            help='UID field of all exported models will be overwritten with new random values.'),
        make_option('-a', '--all',
            action="store_true",
            dest='all',
            default=False,
            help='Export all widgets regardless of the specified package'),
        )

    def handle(self, *args, **options):
24
        if (len(args) < 2):
25
26
27
28
29
30
31
            raise CommandError('Arguments "file_name" and "package_name" are required!')

        try:
            f = open(args[0], 'w')
        except:
            raise CommandError('There was a problem with creating/overwriting given output file')

32
33
34
        result = self.export_package_string(self.stdout.write, args[1:], options['newuid'], options['all'])

        try:
35
            f.write(result.encode('utf-8'))
36
37
38
39
40
41
42
43
        except:
            raise CommandError('There was a problem with writing to the given output file')

        self.stdout.write('Export procedure successfully finished. Results written to the file.\n')

    def export_package_string(self, write, packages, newuid, all):
        result = []

44
        objs = []
45
46
47
48
        if all:
            packages = ['all']
        for package in packages:
            if all:
49
50
51
                wids = AbstractWidget.objects.all()
            else:
                wids = AbstractWidget.objects.filter(package=package)
52
53
54
55
            inps = AbstractInput.objects.filter(widget__in=[x.id for x in wids])
            outs = AbstractOutput.objects.filter(widget__in=[x.id for x in wids])
            opts = AbstractOption.objects.filter(abstract_input__in=[x.id for x in inps])
            cats = Category.objects.filter(id__in=[x.category.id for x in wids])
56
57
58
59

            #retrieve all parents
            catNum = len(cats)
            while True:
60
                cats = cats | Category.objects.filter(id__in=[x.parent.id for x in cats if x.parent != None])
61
62
63
64
65
66
67
68
69
70
71
                if catNum == len(cats):
                    break
                else:
                    catNum = len(cats)

            objs.extend(cats)
            objs.extend(wids)
            objs.extend(outs)
            objs.extend(inps)
            objs.extend(opts)

72
73
74
75
76
77
78
            if len(wids) > 0:
                write('Package "%s" contains:\n' % package)
                write('    % 4i AbstractWidget(s)\n' % len(wids))
                write('    % 4i AbstractInput(s)\n' % len(inps))
                write('    % 4i AbstractOutput(s)\n' % len(outs))
                write('    % 4i AbstractOption(s)\n' % len(opts))
                write('    % 4i Category(s)\n' % len(cats))
79
            else:
80
                write('Package "%s" was not found!\n' % package)
81
82

        #be careful uid is only changed on these instances and is not written to the database
83
        if newuid:
84
85
86
            for a in objs:
                a.uid = str(uuid.uuid4())

87
88
89
        result = ""
        if len(objs) > 0:
            result = serializers.serialize("json", objs, indent=2, ensure_ascii=False)
90

91
        return result