Création d'un compte pour un collaborateur extérieur au laboratoire depuis l'intranet ICube : https://intranet.icube.unistra.fr/fr/labs/member/profile

Commit 67f38203 authored by matjaz's avatar matjaz
Browse files

Improved import/export and added function for the export to be callable from the web

parent 36b971d2
......@@ -21,7 +21,7 @@ class Command(BaseCommand):
)
def handle(self, *args, **options):
if (len(args)<2):
if (len(args) < 2):
raise CommandError('Arguments "file_name" and "package_name" are required!')
try:
......@@ -29,25 +29,35 @@ 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'])
try:
f.write(result)
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 = []
objs = []
argsIter = iter(args)
next(argsIter)
if options['all']:
argsIter = ['all']
for package in argsIter:
if options['all']:
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])
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])
cats = cats | Category.objects.filter(id__in=[x.parent.id for x in cats if x.parent != None])
if catNum == len(cats):
break
else:
......@@ -59,28 +69,23 @@ class Command(BaseCommand):
objs.extend(inps)
objs.extend(opts)
if len(wids)>0:
self.stdout.write('Package "%s" contains:\n' % package)
self.stdout.write(' % 4i AbstractWidget(s)\n' % len(wids))
self.stdout.write(' % 4i AbstractInput(s)\n' % len(inps))
self.stdout.write(' % 4i AbstractOutput(s)\n' % len(outs))
self.stdout.write(' % 4i AbstractOption(s)\n' % len(opts))
self.stdout.write(' % 4i Category(s)\n' % len(cats))
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:
self.stdout.write('Package "%s" was not found!\n' % package)
write('Package "%s" was not found!\n' % package)
#be careful uid is only changed on these instances and is not written to the database
if options['newuid']:
if newuid:
for a in objs:
a.uid = str(uuid.uuid4())
outstr = ""
if len(objs)>0:
outstr = serializers.serialize("json", objs, indent=2, ensure_ascii=False)
try:
f.write(outstr)
except:
raise CommandError('There was a problem with writing to the given output file')
result = ""
if len(objs) > 0:
result = serializers.serialize("json", objs, indent=2, ensure_ascii=False)
self.stdout.write('Export procedure successfully finished. Results written to the file.\n')
\ No newline at end of file
return result
\ No newline at end of file
......@@ -23,20 +23,20 @@ class Command(BaseCommand):
raise CommandError('Arguments "file_name" and "package_name" are required!')
try:
instr = open(args[0], 'r').read()
string = open(args[0], 'r').read()
except:
raise CommandError('There was a problem with opening given input file')
self.importString(self, instr, options['replace'])
self.import_package_string(self, string, options['replace'])
self.stdout.write('Import procedure successfully finished.\n')
def importString(self, obj, instr, replace):
def import_package_string(self, outWriter, string, replace):
#get all objects from file and eliminate empty UID and check for UID duplicates
objsFile = list(serializers.deserialize("json", instr))
objsFile = list(serializers.deserialize("json", string))
objsFileNoUid = [x for x in objsFile if len(x.object.uid) == 0]
objsFile = [x for x in objsFile if len(x.object.uid) != 0]
if len(objsFileNoUid)>0:
obj.stdout.write('File contains %i model(s) without UID field set. Those will not be imported! If you wish to'
outWriter.stdout.write('File contains %i model(s) without UID field set. Those will not be imported! If you wish to'
' assign them random UIDs then use the "-n" option when exporting models with the "export_package"'
' command. Afterwards, you will be able to import them.\n' % len(objsFileNoUid))
if len(Counter([x.object.uid for x in objsFile])) != len(objsFile):
......@@ -52,12 +52,12 @@ class Command(BaseCommand):
cats = [x for x in objsFile if isinstance(x.object, Category)]
#ouput statistics about file
obj.stdout.write('Import contains:\n')
obj.stdout.write(' % 4i AbstractWidget(s)\n' % len(wids))
obj.stdout.write(' % 4i AbstractInput(s)\n' % len(inps))
obj.stdout.write(' % 4i AbstractOutput(s)\n' % len(outs))
obj.stdout.write(' % 4i AbstractOption(s)\n' % len(opts))
obj.stdout.write(' % 4i Category(s)\n' % len(cats))
outWriter.stdout.write('Import contains:\n')
outWriter.stdout.write(' % 4i AbstractWidget(s)\n' % len(wids))
outWriter.stdout.write(' % 4i AbstractInput(s)\n' % len(inps))
outWriter.stdout.write(' % 4i AbstractOutput(s)\n' % len(outs))
outWriter.stdout.write(' % 4i AbstractOption(s)\n' % len(opts))
outWriter.stdout.write(' % 4i Category(s)\n' % len(cats))
#get all objects from database
objsDb = []
......@@ -88,9 +88,9 @@ class Command(BaseCommand):
'by type:\n - from file: %s\n - from database: %s\n Please resolve manually!'% (objFileTypeId, objDbTypeId))
#ouput statistics about database
obj.stdout.write('Current database contains %i models,\n' % len(objsDb))
obj.stdout.write(' of which %i models have UID set,\n' % len(objsdbDict))
obj.stdout.write(' of which %i models match with the imported models and will be updated.\n' % len(idMappingDict))
outWriter.stdout.write('Current database contains %i models,\n' % len(objsDb))
outWriter.stdout.write(' of which %i models have UID set,\n' % len(objsdbDict))
outWriter.stdout.write(' of which %i models match with the imported models and will be updated.\n' % len(idMappingDict))
#prepare statistics
statDict = dict([('old:'+str(t),len(t.objects.all())) for t in [AbstractWidget, AbstractInput, AbstractOutput, AbstractOption, Category]])
......@@ -105,7 +105,7 @@ class Command(BaseCommand):
#objsFile = objsFileNew
#save models to the database - update the ids for the matching models and remove the ids (to get a new one) for the non matching models
obj.stdout.write('Merging file and database models ...')
outWriter.stdout.write('Merging file and database models ...')
importedUids = dict()
for objFile in objsFile:
objFileTypeId = str(type(objFile.object))+':'+str(objFile.object.id)
......@@ -132,10 +132,10 @@ class Command(BaseCommand):
objFile.save()
idMappingDict[objFileTypeId] = objFile.object.id
importedUids[objFile.object.uid]=True
obj.stdout.write(' done.\n')
outWriter.stdout.write(' done.\n')
#correct also the foreign keys
obj.stdout.write('Updating model\'s foreign keys ...')
outWriter.stdout.write('Updating model\'s foreign keys ...')
for objFile in wids:
objFile.object.category = Category.objects.get(id=idMappingDict[str(Category)+':'+str(objFile.old_category_id)])
objFile.save()
......@@ -152,10 +152,10 @@ class Command(BaseCommand):
if not objFile.object.parent_id is None:
objFile.object.parent = Category.objects.get(id=idMappingDict[str(Category)+':'+str(objFile.old_parent_id)])
objFile.save()
obj.stdout.write(' done.\n')
outWriter.stdout.write(' done.\n')
if replace:
obj.stdout.write('Removing unnecessary inputs/options/outputs...')
outWriter.stdout.write('Removing unnecessary inputs/options/outputs...')
for wid in [wid for wid in objsFile if isinstance(wid.object, AbstractWidget)]:
for inp in AbstractInput.objects.filter(widget = wid.object.id):
for opt in AbstractOption.objects.filter(abstract_input = inp.id):
......@@ -169,13 +169,13 @@ class Command(BaseCommand):
if not importedUids.has_key(out.uid):
statDict['del:'+str(AbstractOutput)]+=1
out.delete()
obj.stdout.write(' done.\n')
outWriter.stdout.write(' done.\n')
#update and output statistics
statDict = dict(statDict.items() + dict([('new:'+str(t),len(t.objects.all())) for t in [AbstractWidget, AbstractInput, AbstractOutput, AbstractOption, Category]]).items())
obj.stdout.write('Database models count statistics: pre-import + ( added | modified | deleted ) = after-import\n')
outWriter.stdout.write('Database models count statistics: pre-import + ( added | modified | deleted ) = after-import\n')
for t in [AbstractWidget, AbstractInput, AbstractOutput, AbstractOption, Category]:
obj.stdout.write(' % 15s: % 5i + (% 4i | % 4i | % 4i ) = % 5i\n' %
outWriter.stdout.write(' % 15s: % 5i + (% 4i | % 4i | % 4i ) = % 5i\n' %
(t.__name__,
statDict['old:'+str(t)],
statDict['add:'+str(t)],
......
......@@ -58,5 +58,7 @@ urlpatterns = patterns('',
url(r'^get-executed-status/', 'workflows.views.get_executed_status', name='get executed status'),
url(r'^reset-workflow/', 'workflows.views.reset_workflow', name='reset workflow'),
url(r'^export-package/(?P<packages>.+)/$', 'workflows.views.export_package', name='export_package'),
url(r'^latino/', include(urlpatterns_latino)),
)
\ No newline at end of file
......@@ -6,6 +6,7 @@ from django.core import serializers
from django.utils import simplejson
from workflows.urls import *
from workflows.helpers import *
from workflows.management.commands.export_package import Command
import workflows.interaction_views
import workflows.visualization_views
import sys
......@@ -1209,3 +1210,35 @@ def workflow_url(request):
return render(request,'workflow_url.html', {"workflow":request.user.userprofile.active_workflow})
else:
return HttpResponse(status=200)
@login_required
def export_package(request, packages):
try:
if not request.user.is_staff:
return HttpResponse(status=405)
except:
return HttpResponse(status=400)
newuid = request.GET.get('newuid', 'False')=='True'
all = request.GET.get('all', 'False')=='True'
packagesArray = packages.split('-')
class OutWriter:
msgs = ""
def write(self, msg):
self.msgs += msg
ov = OutWriter()
result = Command().export_package_string(ov.write, packagesArray, newuid, all)
content = '----------------------------------------\n' + \
'Export procedure message:' +\
"\n----------------------------------------\n" +\
ov.msgs + \
"\n----------------------------------------\n" + \
"Exported data:" +\
"\n----------------------------------------\n" +\
result + \
"\n----------------------------------------"
response = HttpResponse(mimetype='text/plain',content=content)
return response
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