Commit d3f76c70 authored by matjaz's avatar matjaz

Package inport/export functionality corrected/improved. Export was rewriten so...

Package inport/export functionality corrected/improved. Export was rewriten so that it outputs Categories/widgets/Inputs/... always in the same order as well as notifies the user what it is exporting.
parent 656f6d60
from unicodedata import category
from django.core.management.base import BaseCommand, CommandError
from workflows.models import Category, AbstractWidget, AbstractInput, AbstractOutput, AbstractOption
from django.core import serializers
......@@ -29,7 +30,7 @@ class Command(BaseCommand):
except:
raise CommandError('There was a problem with creating/overwriting given output file')
result = self.export_package_string(self.stdout.write, args[1:], options['newuid'], options['all'])
result = self.export_package_string(self.stdout.write, args[1:], options['newuid'], options['all'], int(options['verbosity']))
try:
f.write(result)
......@@ -38,54 +39,101 @@ class Command(BaseCommand):
self.stdout.write('Export procedure successfully finished. Results written to the file.\n')
def export_package_string(self, write, packages, newuid, all):
result = []
def print_stataistics(self, objs, verbosity, write):
if verbosity > 0:
write('Selection contains:\n')
write(' % 4i AbstractWidget(s)\n' % len([obj for obj in objs if isinstance(obj, AbstractWidget)]))
write(' % 4i AbstractInput(s)\n' % len([obj for obj in objs if isinstance(obj, AbstractInput)]))
write(' % 4i AbstractOutput(s)\n' % len([obj for obj in objs if isinstance(obj, AbstractOutput)]))
write(' % 4i AbstractOption(s)\n' % len([obj for obj in objs if isinstance(obj, AbstractOption)]))
write(' % 4i Category(s)\n' % len([obj for obj in objs if isinstance(obj, Category)]))
if (verbosity == 1):
write('Exported categories:\n')
if (verbosity == 2):
write('Exported categories and widgets:\n')
if (verbosity == 2):
write('Exported categories, widgets, inputs, outputs and options:\n')
indent = 0
indentAddon = 0
for obj in objs:
s = ''
if isinstance(obj, Category):
indent = str(obj).count('::')
s = '% 3i. Category ===== %s =====' % (obj.order, obj)
if isinstance(obj, AbstractWidget):
s = ' % 3i. AbstractWidget: %s [%s]' % (obj.order, obj.name, obj.action)
if isinstance(obj, AbstractInput):
s = ' % 3i. AbstractInput: (%s) %s' % (obj.order, obj.short_name, obj.name)
if isinstance(obj, AbstractOutput):
s = ' % 3i. AbstractOutput: (%s) %s' % (obj.order, obj.short_name, obj.name)
if isinstance(obj, AbstractOption):
s = ' AbstractOption: %s | %s' % (obj.name, obj.value)
if isinstance(obj, Category) or (isinstance(obj, AbstractWidget) and verbosity > 1) or verbosity > 2:
write(' ' * indent + s + '\n')
self.stdout.write(
'Tip: use higher "verbosity" option numbers to se more detailed output of what is being exported.\n')
def export_package_string(self, write, packages, newuid, all, verbosity):
assert isinstance(packages, tuple)
assert isinstance(newuid, bool)
assert isinstance(all, bool)
objs = []
if all:
packages = ['all']
for package in packages:
if all:
wids = AbstractWidget.objects.all()
else:
wids = AbstractWidget.objects.filter(package=package)
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])
#retrieve all parents
catNum = len(cats)
while True:
cats = cats | Category.objects.filter(id__in=[x.parent.id for x in cats if x.parent != None])
if catNum == len(cats):
break
else:
catNum = len(cats)
objs.extend(cats)
objs.extend(wids)
objs.extend(outs)
objs.extend(inps)
objs.extend(opts)
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))
else:
write('Package "%s" was not found!\n' % package)
for topCat in Category.objects.filter(parent = None):
objs.extend(self.get_package_objs_in_category(topCat, packages, all))
if len(objs) == 0:
write('Selected package(s) were not found!\n')
return "[\\n]"
#be careful uid is only changed on these instances and is not written to the database
if newuid:
for a in objs:
a.uid = str(uuid.uuid4())
result = ""
if len(objs) > 0:
result = serializers.serialize("json", objs, indent=2, ensure_ascii=False)
self.print_stataistics(objs, verbosity, write)
result = serializers.serialize("json", objs, indent=2, ensure_ascii=False)
return result
def get_package_objs_in_category(self, cat, packages, all):
assert isinstance(cat, Category)
assert isinstance(packages, tuple)
assert isinstance(all, bool)
objs = []
objs.extend(self.get_package_wids_in_category(cat, packages, all))
for catChild in cat.children.all():
objs.extend(self.get_package_objs_in_category(catChild, packages, all))
if len(objs)>0:
objs.insert(0,cat)
return objs
def get_package_wids_in_category(self, cat, packages, all):
assert isinstance(cat, Category)
assert isinstance(packages, tuple)
assert isinstance(all, bool)
objs = []
if all:
wids = cat.widgets.all()
else:
wids = cat.widgets.filter(package__in=packages)
for wid in wids:
objs.append(wid)
for inp in wid.inputs.all():
objs.append(inp)
objs.extend(inp.options.all())
objs.extend(wid.outputs.all())
return result
\ No newline at end of file
return objs
\ No newline at end of file
......@@ -94,9 +94,9 @@ class Command(BaseCommand):
#prepare statistics
statDict = dict([('old:'+str(t),len(t.objects.all())) for t in [AbstractWidget, AbstractInput, AbstractOutput, AbstractOption, Category]])
for objFile in objsFile:
for modelType in [AbstractWidget, AbstractInput, AbstractOutput, AbstractOption, Category]:
for operation in ['mod','add','del']:
statDict[operation+':'+str(type(objFile.object))]=0
statDict[operation+':'+str(modelType)]=0
#sort so that AbstractWidgets come in front (are processsed first in the following block)
#objsFileNew = []
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment