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

models.py 47.8 KB
Newer Older
Janez K's avatar
Janez K committed
1
2
3
4
5
6
7
8
9
10
11
12
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
import workflows.library

import time

from picklefield.fields import PickledObjectField

from workflows.thumbs import ThumbnailField

from mothra.settings import DEBUG
Janez K's avatar
Janez K committed
13
from mothra.settings import USE_CONCURRENCY
Janez K's avatar
Janez K committed
14

Janez K's avatar
Janez K committed
15
if USE_CONCURRENCY:
Janez K's avatar
Janez K committed
16
17
    from workflows.tasks import runWidgetAsync, runForLoopIteration

Janez K's avatar
Janez K committed
18
from workflows.tasks import executeWidgetFunction, executeWidgetProgressBar, executeWidgetStreaming, executeWidgetWithRequest, runWidget, executeWidgetPostInteract
19

Janez K's avatar
Janez K committed
20
21
22
class WidgetException(Exception):
    pass

Janez K's avatar
Janez K committed
23
24
25
26
class Connection(models.Model):
    output = models.ForeignKey("Output",related_name="connections")
    input = models.ForeignKey("Input",related_name="connections")
    workflow = models.ForeignKey("Workflow",related_name="connections")
Janez K's avatar
Janez K committed
27

Janez K's avatar
Janez K committed
28
29
30
31
class Category(models.Model):
    name = models.CharField(max_length=50)
    parent = models.ForeignKey('self',related_name="children",null=True,blank=True)
    user = models.ForeignKey(User,null=True,blank=True,related_name="categories")
Janez K's avatar
Janez K committed
32

Janez K's avatar
Janez K committed
33
    workflow = models.ForeignKey('Workflow',null=True,blank=True,related_name="categories")
34
35
36

    order = models.PositiveIntegerField(default=1)

Janez K's avatar
Janez K committed
37
38
    uid = models.CharField(max_length=250,blank=True,default='')

Janez K's avatar
Janez K committed
39
40
    class Meta:
        verbose_name_plural = "categories"
41
42
        ordering = ('order','name',)

Janez K's avatar
Janez K committed
43
44
45
46
47
48
49
    def __unicode__(self):
        if self.parent is None:
            return unicode(self.name)
        else:
            return unicode(unicode(self.parent)+" :: "+self.name)

class Workflow(models.Model):
dejan's avatar
dejan committed
50
51
52
53
    name = models.CharField(max_length=200,default='Untitled workflow') # a field
    user = models.ForeignKey(User,related_name="workflows") # django relationship (ForeignKey), each Workflow is related to a single User
    public = models.BooleanField(default=False) # a field
    description = models.TextField(blank=True,default='') # a field
Janez K's avatar
Janez K committed
54
55
    widget = models.OneToOneField('Widget',related_name="workflow_link",blank=True,null=True)
    template_parent = models.ForeignKey('Workflow',blank=True,null=True,default=None,on_delete=models.SET_NULL)
Janez K's avatar
Janez K committed
56

Janez K's avatar
Janez K committed
57
    def can_be_streaming(self):
dejan's avatar
dejan committed
58
59
        """ Method checks if workflow can be streamed. Check if there is at least one widget with
        the flag abstract_widget__is_streaming on True.  """
Janez K's avatar
Janez K committed
60
61
62
63
        if self.widgets.filter(abstract_widget__is_streaming=True).count()>0:
            return True
        else:
            return False
Janez K's avatar
Janez K committed
64

Janez K's avatar
Janez K committed
65
    def is_for_loop(self):
dejan's avatar
dejan committed
66
67
        """ Method checks if workflow is a for loop. Checks if at least one widget is 
        type for_input. """
Janez K's avatar
Janez K committed
68
69
70
        if self.widgets.filter(type='for_input').count()>0:
            return True
        else:
Janez K's avatar
Janez K committed
71
72
            return False

dejan's avatar
dejan committed
73
74
75
76
77
78
79
80
    def is_cross_validation(self):
        """ Method checks if workflow is a for loop. Checks if at least one widget is 
        type cv input. """
        if self.widgets.filter(type='cv_input').count()>0:
            return True
        else:
            return False

Janez K's avatar
Janez K committed
81
    def get_ready_to_run(self):
dejan's avatar
dejan committed
82
        """ Method prepares this workflows widgets. Returns a list of widget id-s. """
Janez K's avatar
Janez K committed
83
84
85
86
        widgets = self.widgets.all()
        unfinished_list = []
        for w in widgets:
            if not w.finished and not w.running:
dejan's avatar
dejan committed
87
                """ if widget isn't finished and is not running than true"""
Janez K's avatar
Janez K committed
88
89
90
91
                ready_to_run = True
                connections = self.connections.filter(input__widget=w)
                for c in connections:
                    if not c.output.widget.finished:
dejan's avatar
dejan committed
92
                        """ if widget not finished than true """
Janez K's avatar
Janez K committed
93
94
95
96
97
                        ready_to_run = False
                        break
                if ready_to_run:
                    unfinished_list.append(w.id)
        return unfinished_list
Janez K's avatar
Janez K committed
98

Janez K's avatar
Janez K committed
99
    def get_runnable_widgets(self):
dejan's avatar
dejan committed
100
101
        """ Method is the same as get_ready_to_run method. The difference is only that this method
        returns a list widgets as objects (and not only id-s).  """
Janez K's avatar
Janez K committed
102
103
104
105
        widgets = self.widgets.all()
        unfinished_list = []
        for w in widgets:
            if not w.finished and not w.running:
dejan's avatar
dejan committed
106
                """ if widget isn't finished and is not running than true"""
Janez K's avatar
Janez K committed
107
108
109
110
111
112
113
114
115
116
117
                ready_to_run = True
                connections = self.connections.filter(input__widget=w)
                for c in connections:
                    if not c.output.widget.finished:
                        ready_to_run = False
                        break
                if ready_to_run:
                    unfinished_list.append(w)
        return unfinished_list

    def run_for_loop(self):
dejan's avatar
dejan committed
118
119
        """ Method runs the workflow for loop. The use of [0] at the end of lines is because
        there can be only one for loop in one workflow. This way we take the first one. """
Janez K's avatar
Janez K committed
120
        #clear for_input and for_output
dejan's avatar
dejan committed
121
        print("run_for_loop")
Janez K's avatar
Janez K committed
122
123
124
125
126
        fi = self.widgets.filter(type='for_input')[0]
        fo = self.widgets.filter(type='for_output')[0]
        outer_output = fo.inputs.all()[0].outer_output
        outer_output.value=[]
        outer_output.save()
Janez K's avatar
Janez K committed
127

dejan's avatar
dejan committed
128
129
        input_list = fi.outputs.all()[0].outer_input.value # get all inputs from outer part
        progress_total = len(input_list) # for progress bar
Janez K's avatar
Janez K committed
130
131
        current_iteration = 0
        for i in input_list:
dejan's avatar
dejan committed
132
133
134
135
136
            print(i);
            """ Different parameters on which the widgets are going to be run"""
            fi.unfinish() # resets widgets, (read all widgets.finished=false)
            fo.unfinish() # resets widgets, (read all widgets.finished=false)
            proper_output = fi.outputs.all()[0] # inner output
Janez K's avatar
Janez K committed
137
138
            proper_output.value = i
            proper_output.save()
dejan's avatar
dejan committed
139
            fi.finished=True # set the input widget as finished
Janez K's avatar
Janez K committed
140
            fi.save()
Janez K's avatar
Janez K committed
141
            if not USE_CONCURRENCY or 1==1:
dejan's avatar
dejan committed
142
                """ This if statement is always true. """
Janez K's avatar
Janez K committed
143
144
145
146
                unfinished_list = self.get_runnable_widgets()
                try:
                    while len(unfinished_list)>0:
                        for w in unfinished_list:
dejan's avatar
dejan committed
147
                            w.run(True) # run the widget
Janez K's avatar
Janez K committed
148
                            total = self.widgets.count()
Janez K's avatar
Janez K committed
149
                            completed = self.widgets.filter(finished=True).count()
Janez K's avatar
Janez K committed
150
151
152
153
154
155
                            self.widget.progress = (int)((current_iteration*100.0/progress_total)+(((completed*1.0)/total)*(100/progress_total)))
                            self.widget.save()
                        unfinished_list = self.get_runnable_widgets()
                except:
                    raise
            else:
dejan's avatar
dejan committed
156
                """ This part is never executed  """
Janez K's avatar
Janez K committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
                unfinished_list = self.get_runnable_widgets()
                try:
                    statuses = {}
                    total = self.widgets.count()
                    completed = self.widgets.filter(finished=True).count()
                    while len(unfinished_list)>0:
                        for w in unfinished_list:
                            if statuses.has_key(w.pk):
                                if statuses[w.pk].failed():
                                    raise Exception(statuses[w.pk].info[0])
                            else:
                                statuses[w.pk]=runWidgetAsync.delay(w)
                            completed = self.widgets.filter(finished=True).count()
                            if self.widget:
                                self.widget.progress = (int)((current_iteration*100.0/progress_total)+(((completed*1.0)/total)*(100/progress_total)))
                                self.widget.save()
                        is_running=True
                        unfinished_list = self.get_runnable_widgets()
                        while len(unfinished_list)==0 and is_running:
                            unfinished_list = self.get_runnable_widgets()
                            is_running = False
                            for st in statuses.values():
                                if st.status == 'PENDING':
                                    is_running = True
                                else:
                                    st.get()
                                    completed = self.widgets.filter(finished=True).count()
                                    if self.widget:
                                        self.widget.progress = (int)((current_iteration*100.0/progress_total)+(((completed*1.0)/total)*(100/progress_total)))
Janez K's avatar
Janez K committed
186
                                        self.widget.save()
Janez K's avatar
Janez K committed
187
188
189
190
191
                            unfinished_list = self.get_runnable_widgets()
                except:
                    raise
            current_iteration = current_iteration+1

dejan's avatar
dejan committed
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
    def run_cross_validation(self):
        """ Method runs cross_validation. """
        #clear for_input and for_output
        print("run_cross_validation")
        import random as rand
        fi = self.widgets.filter(type='cv_input')[0]
        fo = self.widgets.filter(type='cv_output')[0]
        outer_output = fo.inputs.all()[0].outer_output
        outer_output.value=[]
        outer_output.save()

        # get all inputs from outer part
        input_list = fi.outputs.all()[0].outer_input.value 
        input_fold = fi.outputs.all()[1].outer_input.value
        input_seed = fi.outputs.all()[2].outer_input.value

        if input_fold != None:
            #check if we have an input
            input_fold = int(fi.outputs.all()[1].outer_input.value)
        else:
            input_fold = 10
        
        if input_seed != None:
            #check if we have an input
            input_seed = int(fi.outputs.all()[2].outer_input.value)
        else:
            input_seed = rand.randint(1, 100000000)

220
221
222
223
224
225
226
227
228
229
        # Special case when reading from a DB
        input_type = input_list.__class__.__name__
        context = None
        if input_type == 'DBContext':
            context = input_list
            input_list = context.orng_tables.get(context.target_table, None)

        if not input_list:
            raise Exception('CrossValidation: Empty input list!')

dejan's avatar
dejan committed
230
231
232
233
        progress_total = len(input_list) # for progress bar
        current_iteration = 0

        # create folds
234
        rand.seed(input_seed)
dejan's avatar
dejan committed
235
        rand.shuffle(input_list)
236
237
238
239
240
241
242
243
244
245
246

        folds = []
        if hasattr(input_list, "get_items_ref"):
            # Orange table on input, so we cannot do slices
            indices = range(len(input_list))
            folds_indices = [indices[i::input_fold] for i in range(input_fold)]
            folds = []
            for fold_indices in folds_indices:
                folds.append(input_list.get_items_ref(fold_indices))
        else:
            folds = [input_list[i::input_fold] for i in range(input_fold)]
dejan's avatar
dejan committed
247
248
249
250
251
252
253
254
255
256

        # pass forward the seed
        proper_output = fi.outputs.all()[2] # inner output
        proper_output.value = input_seed
        proper_output.save()

        # this for loop delets all previous results
        for i in fo.inputs.all():
            if not i.parameter:
                if i.connections.count() > 0:
257
258
                    i.value = []
                    i.save()
dejan's avatar
dejan committed
259
260

        for i in range(len(folds)):
261
262
263
264
265
266
267
268
269
270
            output_train = folds[:i] + folds[i+1:]
            output_test = folds[i]
            if input_type == 'DBContext':
                output_train_obj = context.copy()
                output_train_obj.orng_tables[context.target_table] = output_train
                output_test_obj = context.copy()
                output_test_obj.orng_tables[context.target_table] = output_test
                output_train = output_train_obj
                output_test = output_test_obj

dejan's avatar
dejan committed
271
272
273
274
            """ Different parameters on which the widgets are going to be run"""
            fi.unfinish() # resets widgets, (read all widgets.finished=false)
            fo.unfinish() # resets widgets, (read all widgets.finished=false)
            proper_output = fi.outputs.all()[0] # inner output
275
            proper_output.value = output_train
dejan's avatar
dejan committed
276
277
            proper_output.save()
            proper_output = fi.outputs.all()[1] # inner output
278
            proper_output.value = output_test
dejan's avatar
dejan committed
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
            proper_output.save()
            fi.finished=True # set the input widget as finished
            fi.save()
            if not USE_CONCURRENCY or 1==1:
                """ This if statement is always true. """
                unfinished_list = self.get_runnable_widgets()
                try:
                    while len(unfinished_list)>0:
                        for w in unfinished_list:
                            w.run(True) # run the widget
                            total = self.widgets.count()
                            completed = self.widgets.filter(finished=True).count()
                            self.widget.progress = (int)((current_iteration*100.0/progress_total)+(((completed*1.0)/total)*(100/progress_total)))
                            self.widget.save()
                        unfinished_list = self.get_runnable_widgets()
                except:
                    raise
            current_iteration = current_iteration+1

Janez K's avatar
Janez K committed
298
    def run(self):
Janez K's avatar
Janez K committed
299
        if not USE_CONCURRENCY or not self.widget:
Janez K's avatar
Janez K committed
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
            unfinished_list = self.get_runnable_widgets()
            try:
                total = self.widgets.count()
                completed = self.widgets.filter(finished=True).count()
                while len(unfinished_list)>0:
                    for w in unfinished_list:
                        w.run(True)
                        #runWidgetAsync.delay(w)
                        completed = self.widgets.filter(finished=True).count()
                        if self.widget:
                            self.widget.progress = (int)(((completed*1.0)/total)*100)
                            self.widget.save()
                    unfinished_list = self.get_runnable_widgets()
            except:
                raise
        else:
            unfinished_list = self.get_runnable_widgets()
            try:
                statuses = {}
                total = self.widgets.count()
                completed = self.widgets.filter(finished=True).count()
                while len(unfinished_list)>0:
                    for w in unfinished_list:
                        if statuses.has_key(w.pk):
                            if statuses[w.pk].failed():
                                raise Exception(statuses[w.pk].info[0])
                        else:
                            statuses[w.pk]=runWidgetAsync.delay(w)
                        completed = self.widgets.filter(finished=True).count()
                        if self.widget:
                            self.widget.progress = (int)(((completed*1.0)/total)*100)
                            self.widget.save()
                    is_running=True
Janez K's avatar
Janez K committed
333
                    unfinished_list = self.get_runnable_widgets()
Janez K's avatar
Janez K committed
334
335
336
337
338
339
340
341
342
343
344
                    while len(unfinished_list)==0 and is_running:
                        unfinished_list = self.get_runnable_widgets()
                        is_running = False
                        for st in statuses.values():
                            if st.status == 'PENDING':
                                is_running = True
                            else:
                                st.get()
                                completed = self.widgets.filter(finished=True).count()
                                if self.widget:
                                    self.widget.progress = (int)(((completed*1.0)/total)*100)
Janez K's avatar
Janez K committed
345
                                    self.widget.save()
Janez K's avatar
Janez K committed
346
                        time.sleep(1)
Janez K's avatar
Janez K committed
347
                    unfinished_list = self.get_runnable_widgets()
Janez K's avatar
Janez K committed
348
349
350
351
            except:
                raise
    def rename(self,new_name):
        self.name = new_name
Janez K's avatar
Janez K committed
352
353
        self.save()

Janez K's avatar
Janez K committed
354
355
    @models.permalink
    def get_absolute_url(self):
Janez K's avatar
Janez K committed
356
357
        return ('open workflow', [str(self.id)])

Janez K's avatar
Janez K committed
358
359
360
    @models.permalink
    def get_copy_url(self):
        return ('copy workflow', [str(self.id)])
Janez K's avatar
Janez K committed
361

Janez K's avatar
Janez K committed
362
363
364
    @models.permalink
    def get_info_url(self):
        return ('workflow information', [str(self.id)])
Janez K's avatar
Janez K committed
365

Janez K's avatar
Janez K committed
366
367
    def __unicode__(self):
        return unicode(self.name)
Janez K's avatar
Janez K committed
368

Janez K's avatar
Janez K committed
369
    class Meta:
Janez K's avatar
Janez K committed
370
        ordering = ['name']
Janez K's avatar
Janez K committed
371
372

class AbstractWidget(models.Model):
Janez K's avatar
Janez K committed
373
374
    name = models.CharField(max_length=200,help_text='Name is the name that will be displayed in the widget repository and under the actual widget itself.')
    action = models.CharField(max_length=200,help_text='Action is the name of a python function that will be called when the widget is executed.')
Janez K's avatar
Janez K committed
375
    wsdl = models.URLField(max_length=200,blank=True,help_text='WSDL and WSDL method are used if the widget is a call of a Web Service. Web Service widgets are usually not entered in the admin panel, but in the application itself by importing a Web Service.')
Janez K's avatar
Janez K committed
376
    wsdl_method = models.CharField(max_length=200,blank=True,default='')
Janez K's avatar
Janez K committed
377
378
379
    description = models.TextField(blank=True,help_text='Description is used for a human readable description of what a widget does. A user will see this when he right clicks the widget and clicks help.')
    category = models.ForeignKey(Category,related_name="widgets",help_text='Category determines to which category this widget belongs. Categories can be nested.')
    visualization_view = models.CharField(max_length=200,blank=True,default='',help_text='Visualization view is (like the action) a python function that is a view that will render a template.')
380
    streaming_visualization_view = models.CharField(max_length=200,blank=True,default='',help_text='Visualization view is (like the action) a python function that is a view that will render a template.')
Janez K's avatar
Janez K committed
381
382
    user = models.ForeignKey(User,blank=True,null=True,related_name="widgets",help_text='If the User field is blank, everyone will see the widget, otherwise just this user. This is mainly used for Web Service imports as they are only visible to users that imported them.')
    interactive = models.BooleanField(default=False,help_text='The widget can be interactive. This means that when a user executes the widget, the action will perform, then the interaction view will be executed and finally the Post interact action will be executed.')
Janez K's avatar
Janez K committed
383
384
    interaction_view = models.CharField(max_length=200,blank=True,default='')
    post_interact_action = models.CharField(max_length=200,blank=True,default='')
Janez K's avatar
Janez K committed
385

Janez K's avatar
Janez K committed
386
    image = ThumbnailField(blank=True,null=True,upload_to="images",size=(34,34),help_text='Image and Treeview image are deprecated and will be phased out soon. Please use the static image field.')
Janez K's avatar
Janez K committed
387
    treeview_image = ThumbnailField(blank=True,null=True,upload_to="treeview",size=(16,16))
388

Janez K's avatar
Janez K committed
389
    static_image = models.CharField(max_length=250,blank=True,default='',help_text='In the static image field just enter the filename of the image (without the path). The path will be $package_name$/icons/widget/$filename$ and $package_name$/icons/treeview/$filename$ where the treeview image is the small image that appears in the treeview on the left side and the widget image is the actual normal sized icon for the widget. IMPORTANT: the static image field only works if the package is set.')
Janez K's avatar
Janez K committed
390

Janez K's avatar
Janez K committed
391
392
    has_progress_bar = models.BooleanField(default=False,help_text='The flag has progress bar determines if the widget implements a progress bar.')
    is_streaming = models.BooleanField(default=False,help_text='The is streaming flag is currently under construction, please do not use it yet.')
Janez K's avatar
Janez K committed
393

Janez K's avatar
Janez K committed
394
    order = models.PositiveIntegerField(default=1,help_text='The Order determines the order in which the widget will be displayed in the repository. This is set automatically when sorting widgets in a single category from the admin.')
Janez K's avatar
Janez K committed
395

Janez K's avatar
Janez K committed
396
    uid = models.CharField(max_length=250,blank=True,default='',help_text='UID is set automatically when you export a package with the -u switch.')
Janez K's avatar
Janez K committed
397

Janez K's avatar
Janez K committed
398
    package = models.CharField(max_length=150,blank=True,default='',help_text='Package is the package name. You are encouraged to use packages.')
Janez K's avatar
Janez K committed
399

400
401
    windows_queue = models.BooleanField(default=False,help_text="This is used for Matjaz Jursic's widgets.")

Janez K's avatar
Janez K committed
402
403
    class Meta:
        ordering = ('order','name',)
404

Janez K's avatar
Janez K committed
405
406
407
408
409
410
411
412
413
    def set_uid(self,commit=False):
        import uuid
        self.uid = uuid.uuid4()
        if commit:
            self.save()
        for i in self.inputs.all():
            i.uid = uuid.uuid4()
            if commit:
                i.save()
bogdan's avatar
bogdan committed
414
            for option in i.options.all():
415
416
417
                option.uid = uuid.uuid4()
                if commit:
                    option.save()
Janez K's avatar
Janez K committed
418
419
420
421
422
        for o in self.outputs.all():
            o.uid = uuid.uuid4()
            if commit:
                o.save()

Janez K's avatar
Janez K committed
423
424
425
426
427
428
429
    def __unicode__(self):
        return unicode(self.name)

class AbstractInput(models.Model):
    name = models.CharField(max_length=200)
    short_name = models.CharField(max_length=3)
    description = models.TextField(blank=True)
Janez K's avatar
Janez K committed
430
    variable = models.CharField(max_length=50,help_text='The variable attribute of both the input and the output are important because this is how the data will be accessed in the python function that is executed when the widget runs.')
Janez K's avatar
Janez K committed
431
432
433
    widget = models.ForeignKey(AbstractWidget,related_name="inputs")
    required = models.BooleanField()
    parameter = models.BooleanField()
Janez K's avatar
Janez K committed
434
    multi = models.BooleanField(default=False,help_text='Inputs with this flag set will behave like this: whenever a connection is added to this input another input will be created on the fly that accepts the same data. In the action function, this will be represented as a list.')
Janez K's avatar
Janez K committed
435
436
437
    default = models.TextField(blank=True)
    PARAMETER_CHOICES = (
        ('text','Single line'),
438
        ('password', 'Password'),
Janez K's avatar
Janez K committed
439
440
441
442
443
444
        ('textarea','Multi line text'),
        ('select', 'Select box'),
        ('checkbox', 'Checkbox'),
        ('file', 'File'),
    )
    parameter_type = models.CharField(max_length=50,choices=PARAMETER_CHOICES,blank=True,null=True)
Janez K's avatar
Janez K committed
445

Janez K's avatar
Janez K committed
446
    order = models.PositiveIntegerField(default=1)
Janez K's avatar
Janez K committed
447
448

    uid = models.CharField(max_length=250,blank=True,default='')
Janez K's avatar
Janez K committed
449

Janez K's avatar
Janez K committed
450
451
    def __unicode__(self):
        return unicode(self.name)
Janez K's avatar
Janez K committed
452

Janez K's avatar
Janez K committed
453
454
    class Meta:
        ordering = ('order',)
Janez K's avatar
Janez K committed
455

Janez K's avatar
Janez K committed
456
457
458
459
class AbstractOption(models.Model):
    abstract_input = models.ForeignKey(AbstractInput,related_name="options")
    name = models.CharField(max_length=200)
    value = models.TextField(blank=True)
Janez K's avatar
Janez K committed
460
461
462

    uid = models.CharField(max_length=250,blank=True,default='')

Janez K's avatar
Janez K committed
463
464
    def __unicode__(self):
        return unicode(self.name)
Janez K's avatar
Janez K committed
465

Janez K's avatar
Janez K committed
466
    class Meta:
Janez K's avatar
Janez K committed
467
        ordering = ['name']
Janez K's avatar
Janez K committed
468
469
470
471
472

class AbstractOutput(models.Model):
    name = models.CharField(max_length=200)
    short_name = models.CharField(max_length=3)
    description = models.TextField(blank=True)
Janez K's avatar
Janez K committed
473
    variable = models.CharField(max_length=50,help_text='The variable attribute of both the input and the output are important because this is how the data will be accessed in the python function that is executed when the widget runs.')
Janez K's avatar
Janez K committed
474
    widget = models.ForeignKey(AbstractWidget,related_name="outputs")
Janez K's avatar
Janez K committed
475

Janez K's avatar
Janez K committed
476
    order = models.PositiveIntegerField(default=1)
Janez K's avatar
Janez K committed
477
478

    uid = models.CharField(max_length=250,blank=True,default='')
Janez K's avatar
Janez K committed
479

Janez K's avatar
Janez K committed
480
    class Meta:
Janez K's avatar
Janez K committed
481
482
        ordering = ('order',)

Janez K's avatar
Janez K committed
483
484
485
486
    def __unicode__(self):
        return unicode(self.name)

class Widget(models.Model):
dejan's avatar
dejan committed
487
488
    """ Widget """
    # django relationship (ForeignKey), each widget is related to a single workflow
Janez K's avatar
Janez K committed
489
    workflow = models.ForeignKey(Workflow,related_name="widgets")
dejan's avatar
dejan committed
490
491
492
    x = models.IntegerField() # a field
    y = models.IntegerField() # a field
    name = models.CharField(max_length=200) # a field
Janez K's avatar
Janez K committed
493
    abstract_widget = models.ForeignKey(AbstractWidget,related_name="instances",blank=True,null=True)
dejan's avatar
dejan committed
494
495
496
497
498
    finished = models.BooleanField(default=False) # a field
    error = models.BooleanField(default=False) # a field
    running = models.BooleanField(default=False) # a field
    interaction_waiting = models.BooleanField(default=False) # a field
    """ type of widgets """
Janez K's avatar
Janez K committed
499
500
501
502
503
504
505
    WIDGET_CHOICES = (
        ('regular','Regular widget'),
        ('subprocess','Subprocess widget'),
        ('input', 'Input widget'),
        ('output', 'Output widget'),
    )
    type = models.CharField(max_length=50,choices=WIDGET_CHOICES,default='regular')
Janez K's avatar
Janez K committed
506

Janez K's avatar
Janez K committed
507
    progress = models.IntegerField(default=0)
Janez K's avatar
Janez K committed
508

Janez K's avatar
Janez K committed
509
510
511
512
513
514
    def is_visualization(self):
        try:
            if self.abstract_widget.visualization_view != '':
                return True
        except:
            return False
Janez K's avatar
Janez K committed
515

Janez K's avatar
Janez K committed
516
517
518
519
520
521
    def ready_to_run(self):
        cons = Connection.objects.filter(input__widget=self)
        for c in cons:
            if not c.output.widget.finished:
                return False
        return True
Janez K's avatar
Janez K committed
522

Janez K's avatar
Janez K committed
523
    def unfinish(self):
Janez K's avatar
Janez K committed
524
        self.reset_descendants()
Janez K's avatar
Janez K committed
525

Janez K's avatar
Janez K committed
526
527
528
529
530
531
532
533
    def subunfinish(self):
        if self.type == 'subprocess':
            for w in self.workflow_link.widgets.all():
                w.finished=False
                w.error = False
                w.save()
                if w.type=='subprocess':
                    w.subunfinish()
Janez K's avatar
Janez K committed
534

Janez K's avatar
Janez K committed
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
    def rename(self,new_name):
        self.name = new_name
        self.save()
        if self.type=='input':
            inp = self.outputs.all()[0]
            inp.short_name = self.name[:3]
            inp.name = self.name
            inp.save()
            inp.outer_input.name = self.name
            inp.outer_input.short_name = self.name[:3]
            inp.outer_input.save()
        if self.type=='output':
            inp = self.inputs.all()[0]
            inp.short_name = self.name[:3]
            inp.name = self.name
            inp.save()
            inp.outer_output.name = self.name
            inp.outer_output.short_name = self.name[:3]
Janez K's avatar
Janez K committed
553
            inp.outer_output.save()
Janez K's avatar
Janez K committed
554
555
556
557
558
559
        try:
            w_link = self.workflow_link
            w_link.name=new_name
            w_link.save()
        except Workflow.DoesNotExist:
            pass
Janez K's avatar
Janez K committed
560

Janez K's avatar
Janez K committed
561
    def run(self,offline):
dejan's avatar
dejan committed
562
        """ This is only a hack, to make this work on windows """
Janez K's avatar
bugfix    
Janez K committed
563
564
565
566
567
568
569
        try: 
            if self.abstract_widget.windows_queue:
                t = runWidget.apply_async([self,offline],queue="windows")
                t.wait()
            else:
                self.proper_run(offline)
        except AttributeError:
Janez K's avatar
fix    
Janez K committed
570
571
572
            self.proper_run(offline)

    def proper_run(self,offline):
dejan's avatar
dejan committed
573
574
        """ This is the real start. """
        print("proper_run_widget")
Janez K's avatar
Janez K committed
575
        if not self.ready_to_run():
Janez K's avatar
Janez K committed
576
            raise WidgetException("The prerequisites for running this widget have not been met.")
Janez K's avatar
Janez K committed
577
578
579
        self.running=True
        self.save()
        if self.type == 'regular' or self.type == 'subprocess':
dejan's avatar
dejan committed
580
            """ if this is a subprocess or a regular widget than true."""
Janez K's avatar
Janez K committed
581
            if not self.abstract_widget is None:
582
                """if this is an abstract widget than true we save the widget function in a variable """
Janez K's avatar
Janez K committed
583
584
585
586
                function_to_call = getattr(workflows.library,self.abstract_widget.action)
            input_dict = {}
            outputs = {}
            for i in self.inputs.all():
dejan's avatar
dejan committed
587
                """ we walk through all the inputs """
Janez K's avatar
Janez K committed
588
589
                #gremo pogledat ce obstaja povezava in ce obstaja gremo value prebrat iz outputa
                if not i.parameter:
dejan's avatar
dejan committed
590
                    """ if there is a connection than true and read the output value """
Janez K's avatar
Janez K committed
591
592
593
594
595
596
597
598
599
600
601
602
603
                    if i.connections.count() > 0:
                        i.value = i.connections.all()[0].output.value
                        i.save()
                    else:
                        i.value = None
                        i.save()
                if i.multi_id == 0:
                    input_dict[i.variable]=i.value
                else:
                    if not i.variable in input_dict:
                        input_dict[i.variable]=[]
                    if not i.value==None:
                        input_dict[i.variable].append(i.value)
Janez K's avatar
Janez K committed
604
            start = time.time()
Janez K's avatar
Janez K committed
605
606
            try:
                if not self.abstract_widget is None:
dejan's avatar
dejan committed
607
608
                    """ again, if this objects is an abstract widget than true and check certain parameters,
                    else check if is_for_loop"""
Janez K's avatar
Janez K committed
609
                    if self.abstract_widget.wsdl != '':
dejan's avatar
dejan committed
610
                        """ if abstrac widget is a web service """
Janez K's avatar
Janez K committed
611
612
613
                        input_dict['wsdl']=self.abstract_widget.wsdl
                        input_dict['wsdl_method']=self.abstract_widget.wsdl_method
                    if self.abstract_widget.has_progress_bar:
dejan's avatar
dejan committed
614
                        """ if abstrac widget has a progress bar """
Janez K's avatar
fix    
Janez K committed
615
                        outputs = function_to_call(input_dict,self)
Janez K's avatar
Janez K committed
616
                    elif self.abstract_widget.is_streaming:
dejan's avatar
dejan committed
617
                        """ if abstrac widget is a stream """
Janez K's avatar
fix    
Janez K committed
618
                        outputs = function_to_call(input_dict,self,None)
Janez K's avatar
Janez K committed
619
                    else:
dejan's avatar
dejan committed
620
                        """ else run abstract widget function """
Janez K's avatar
fix    
Janez K committed
621
                        outputs = function_to_call(input_dict)
Janez K's avatar
Janez K committed
622
623
                else:
                    if self.workflow_link.is_for_loop():
dejan's avatar
dejan committed
624
625
626
                        """ if this is object is a for loop than true and run;
                        else false and run workflow """
                        print("proper_run_is_for_loop")
Janez K's avatar
Janez K committed
627
                        self.workflow_link.run_for_loop()
628
                        #print self.outputs.all()[0].value
dejan's avatar
dejan committed
629
630
                    elif self.workflow_link.is_cross_validation():
                        self.workflow_link.run_cross_validation()
Janez K's avatar
Janez K committed
631
632
633
634
635
636
637
638
                    else:
                        self.workflow_link.run()
            except:
                self.error=True
                self.running=False
                self.finished=False
                self.save()
                raise
Janez K's avatar
Janez K committed
639
640
            elapsed = (time.time()-start)
            outputs['clowdflows_elapsed']=elapsed
Janez K's avatar
Janez K committed
641
            for o in self.outputs.all():
dejan's avatar
dejan committed
642
                """ we walk through all the outputs """
Janez K's avatar
Janez K committed
643
                if not self.abstract_widget is None:
dejan's avatar
dejan committed
644
645
                    """ if this object is an abstract widget than true and save output
                    else look for outputs in workflow """
Janez K's avatar
Janez K committed
646
647
648
649
                    try:
                        o.value = outputs[o.variable]
                    except:
                        pass
Janez K's avatar
Janez K committed
650
651
652
653
654
655
656
                    o.save()
                else:
                    #gremo v outpute pogledat
                    if not self.workflow_link.is_for_loop():
                        o.value = o.inner_input.value
                        o.save()
            if self.abstract_widget is None:
dejan's avatar
dejan committed
657
                """ if object is widget than true and configure parameters """
Janez K's avatar
Janez K committed
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
                self.finished=True
                self.running=False
                self.error=False
                self.save()
            else:
                if not self.abstract_widget.interactive or offline:
                    self.finished=True
                    self.running=False
                    self.error=False
                    self.save()
            cons = Connection.objects.filter(output__widget=self)
            for c in cons:
                c.input.widget.unfinish()
            return outputs
        elif self.type == 'for_input':
dejan's avatar
dejan committed
673
674
            """ if object is an input widget for for loop than read all input values and finish """
            #print("for_input")
Janez K's avatar
Janez K committed
675
676
            for o in self.outputs.all():
                o.value=o.outer_input.value
dejan's avatar
dejan committed
677
                #print(o.outer_input.value)
Janez K's avatar
Janez K committed
678
679
680
681
682
683
                o.save()
            self.finished=True
            self.running=False
            self.error=False
            self.save()
        elif self.type == 'for_output':
dejan's avatar
dejan committed
684
685
686
            """ if object is an output widget for for loop, then read output values and 
            configure parameters"""
            #print("for_output")
Janez K's avatar
Janez K committed
687
688
            for i in self.inputs.all():
                if not i.parameter:
dejan's avatar
dejan committed
689
                    """ if there is a connection than true and read the output value """
Janez K's avatar
Janez K committed
690
691
692
693
694
695
696
697
698
699
                    if i.connections.count() > 0:
                        i.value = i.connections.all()[0].output.value
                        i.save()
                        i.outer_output.value.append(i.value)
                        i.outer_output.save()
                        self.finished=True
            self.finished=True
            self.running=False
            self.error=False
            self.save()
dejan's avatar
dejan committed
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
        elif self.type == 'cv_input':
            """ if object is an input widget for cross validation 
            than read all input values and finish """
            for o in self.outputs.all():
                #print('cv_input')
                o.value=o.outer_input.value
                o.save()
            self.finished=True
            self.running=False
            self.error=False
            self.save()
        elif self.type == 'cv_output':
            """ if object is an output widget for cross validation, 
            then read output values and configure parameters"""
            for i in self.inputs.all():
                if not i.parameter:
                    """ if there is a connection than true and read the output value """
                    if i.connections.count() > 0:
                        if i.value is None:
                            i.value = [i.connections.all()[0].output.value]
                        else:
                            i.value = [i.connections.all()[0].output.value] + i.value
                        #print i.value
                        i.save()
                        i.outer_output.value.append(i.value)
                        i.outer_output.save()
                        self.finished=True
            self.finished=True
            self.running=False
            self.error=False
            self.save()
Janez K's avatar
Janez K committed
731
        elif self.type == 'input':
dejan's avatar
dejan committed
732
            """ if object is an input widget for for loop than read all input values and finish """
Janez K's avatar
Janez K committed
733
734
735
736
737
738
739
740
            for o in self.outputs.all():
                o.value=o.outer_input.value
                o.save()
            self.finished=True
            self.running=False
            self.error=False
            self.save()
        elif self.type == 'output':
dejan's avatar
dejan committed
741
742
            """ if object is an output widget, then read output values and 
            configure parameters"""
Janez K's avatar
Janez K committed
743
744
            for i in self.inputs.all():
                if not i.parameter:
dejan's avatar
dejan committed
745
                    """ if there is a connection than true and read the output value """
Janez K's avatar
Janez K committed
746
747
748
749
750
751
                    if i.connections.count() > 0:
                        i.value = i.connections.all()[0].output.value
                        i.save()
                        i.outer_output.value = i.value
                        i.outer_output.save()
                        self.finished=True
Janez K's avatar
Janez K committed
752
            self.finished=True
Janez K's avatar
Janez K committed
753
754
755
            self.running=False
            self.error=False
        return None
Janez K's avatar
Janez K committed
756

757
    def reset(self,offline):
Janez K's avatar
Janez K committed
758
759
760
761
762
763
764
        #for i in self.inputs.all():
        #    if not i.parameter:
        #        i.value = None
        #        i.save()
        #for i in self.outputs.all():
        #    i.value = None
        #    i.save()
765
766
767
768
        self.finished = False
        self.error = False
        self.running = False
        self.save()
Janez K's avatar
Janez K committed
769
770
        if self.type == 'subprocess':
            self.subunfinish()
771

Janez K's avatar
Janez K committed
772
    def reset_descendants(self):
dejan's avatar
dejan committed
773
        """ Method resets all the widget connections/descendants. """
Janez K's avatar
Janez K committed
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
        pairs = []
        for c in self.workflow.connections.select_related("output","input").defer("output__value","input__value").all():
            if not (c.output.widget_id,c.input.widget_id) in pairs:
                pairs.append((c.output.widget_id,c.input.widget_id))
        next = {}                
        for p in pairs:
            if not next.has_key(p[0]):
                next[p[0]]=set()
            next[p[0]].add(p[1])
        widgets_that_need_reset = set([self.pk,])
        current_widgets_that_need_reset = set([self.pk,])
        while len(current_widgets_that_need_reset)>0:
            new_widgets_that_need_reset = set()
            for w_id in current_widgets_that_need_reset:
                try:
                    for p in next.get(w_id):
                        new_widgets_that_need_reset.add(p)
                        widgets_that_need_reset.add(p)
                except:
                    pass
            current_widgets_that_need_reset = new_widgets_that_need_reset
        Widget.objects.filter(id__in=widgets_that_need_reset).update(finished=False,error=False,running=False)
        subprocesses = Widget.objects.filter(id__in=widgets_that_need_reset,type='subprocess')
        for w in subprocesses:
            w.subunfinish()
        return widgets_that_need_reset

    def reset_descendants_slow(self):
Janez K's avatar
Janez K committed
802
        #find all descendants and reset them as well
Janez K's avatar
Janez K committed
803
        widgets = list(self.workflow.widgets.prefetch_related('inputs','outputs','inputs__connections','outputs__connections','outputs__connections__input','inputs__connections__output'))
Janez K's avatar
Janez K committed
804
805
806
807
808
809
810
811
812
813
814
815
816
        widgets_dict = {}
        widgets_that_need_reset = set([self.pk,])
        current_widgets_that_need_reset = set([self.pk,])
        for w in widgets:
            widgets_dict[w.pk]=w
        while len(current_widgets_that_need_reset)>0:
            new_widgets_that_need_reset = set()
            for w_id in current_widgets_that_need_reset:
                for o in widgets_dict[w_id].outputs.all():
                    for c in o.connections.all():
                        new_widgets_that_need_reset.add(c.input.widget_id)
                        widgets_that_need_reset.add(c.input.widget_id)
            current_widgets_that_need_reset = new_widgets_that_need_reset
Janez K's avatar
Janez K committed
817
        Widget.objects.filter(id__in=widgets_that_need_reset).update(finished=False,error=False,running=False)
Janez K's avatar
Janez K committed
818
        for w in widgets_that_need_reset:
Janez K's avatar
Janez K committed
819
820
821
            if widgets_dict[w].type == 'subprocess':
                widgets_dict[w].subunfinish()
        #    widgets_dict[w].reset(False)
Janez K's avatar
Janez K committed
822
823
        return widgets_that_need_reset

Janez K's avatar
Janez K committed
824
825
    def run_post(self,request):
        if not self.ready_to_run():
Janez K's avatar
Janez K committed
826
            raise WidgetException("The prerequisites for running this widget have not been met.")
Janez K's avatar
Janez K committed
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
        self.running=True
        self.save()
        function_to_call = getattr(workflows.library,self.abstract_widget.post_interact_action)
        input_dict = {}
        outputs = {}
        output_dict = {}
        for o in self.outputs.all():
            output_dict[o.variable]=o.value
        for i in self.inputs.all():
            #gremo pogledat ce obstaja povezava in ce obstaja gremo value prebrat iz outputa
            if not i.parameter:
                if i.connections.count() > 0:
                    i.value = i.connections.all()[0].output.value
                    i.save()
                else:
                    i.value = None
                    i.save()
            if i.multi_id == 0:
                input_dict[i.variable]=i.value
            else:
                if not i.variable in input_dict:
                    input_dict[i.variable]=[]
                if not i.value==None:
                    input_dict[i.variable].append(i.value)
        try:
            if not self.abstract_widget is None:
853
                if self.abstract_widget.windows_queue:
Janez K's avatar
Janez K committed
854
                    t = executeWidgetPostInteract.apply_async([self,input_dict,output_dict,request],queue="windows")
855
856
                    outputs = t.wait()
                else:
Janez K's avatar
Janez K committed
857
                    outputs = executeWidgetPostInteract(self,input_dict,output_dict,request)
Janez K's avatar
Janez K committed
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
            else:
                self.workflow_link.run()
        except:
            self.error=True
            self.running=False
            self.finished=False
            self.save()
            raise
        for o in self.outputs.all():
            o.value = outputs[o.variable]
            o.save()
        self.finished=True
        self.running=False
        self.error=False
        self.interaction_waiting=False
        self.save()
        cons = Connection.objects.filter(output__widget=self)
        for c in cons:
            c.input.widget.unfinish()
Janez K's avatar
Janez K committed
877
878
        return outputs

Janez K's avatar
Janez K committed
879
880
    def __unicode__(self):
        return unicode(self.name)
Janez K's avatar
Janez K committed
881

Janez K's avatar
Janez K committed
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
class Input(models.Model):
    name = models.CharField(max_length=200)
    short_name = models.CharField(max_length=3)
    description = models.TextField(blank=True,null=True)
    variable = models.CharField(max_length=50)
    widget = models.ForeignKey(Widget,related_name="inputs")
    required = models.BooleanField()
    parameter = models.BooleanField()
    value = PickledObjectField(null=True)
    multi_id = models.IntegerField(default=0)
    inner_output = models.ForeignKey('Output',related_name="outer_input_rel",blank=True,null=True) #za subprocess
    outer_output = models.ForeignKey('Output',related_name="inner_input_rel",blank=True,null=True) #za subprocess
    PARAMETER_CHOICES = (
        ('text','Single line'),
        ('textarea','Multi line text'),
        ('select', 'Select box'),
Janez K's avatar
Janez K committed
898
899
        ('file', 'File field'),
        ('checkbox', 'Checkbox'),
Janez K's avatar
Janez K committed
900
901
    )
    parameter_type = models.CharField(max_length=50,choices=PARAMETER_CHOICES,blank=True,null=True)
902
903
904
905
    order = models.PositiveIntegerField(default=1)

    class Meta:
        ordering = ('order',)
Janez K's avatar
Janez K committed
906

Janez K's avatar
Janez K committed
907
    def __unicode__(self):
Janez K's avatar
Janez K committed
908
909
        return unicode(self.name)

dejan's avatar
dejan committed
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
"""class InputCrossValidation(models.Model):
    name = models.CharField(max_length=200)
    short_name = models.CharField(max_length=3)
    description = models.TextField(blank=True,null=True)
    variable = models.CharField(max_length=50)
    widget = models.ForeignKey(Widget,related_name="inputs2")
    required = models.BooleanField()
    parameter = models.BooleanField()
    value = PickledObjectField(null=True)
    multi_id = models.IntegerField(default=0)
    inner_output1 = models.ForeignKey('OutputCrossValidation',related_name="outer_input_rel",blank=True,null=True) #za subprocess
    #inner_output2 = models.ForeignKey('OutputCrossValidation',related_name="outer_input_rel",blank=True,null=True) #za subprocess
    outer_output = models.ForeignKey('OutputCrossValidation',related_name="inner_input_rel",blank=True,null=True) #za subprocess
    PARAMETER_CHOICES = (
        ('text','Single line'),
        ('textarea','Multi line text'),
        ('select', 'Select box'),
    )
    parameter_type = models.CharField(max_length=50,choices=PARAMETER_CHOICES,blank=True,null=True)
    order = models.PositiveIntegerField(default=1)

    class Meta:
        ordering = ('order',)

    def __unicode__(self):
        return unicode(self.name)

class OutputCrossValidation(models.Model):
    name = models.CharField(max_length=200)
    short_name = models.CharField(max_length=5)
    description = models.TextField(blank=True)
    variable = models.CharField(max_length=50)
    widget = models.ForeignKey(Widget,related_name="outputs")
    value = PickledObjectField(null=True)
    inner_input = models.ForeignKey(InputCrossValidation,related_name="outer_output_rel",blank=True,null=True) #za subprocess
    outer_input = models.ForeignKey(InputCrossValidation,related_name="inner_output_rel",blank=True,null=True) #za subprocess
    order = models.PositiveIntegerField(default=1)

    class Meta:
        ordering = ('order',)

    def __unicode__(self):
        return unicode(self.name)"""

Janez K's avatar
Janez K committed
954
955
956
957
class Option(models.Model):
    input = models.ForeignKey(Input,related_name="options")
    name = models.CharField(max_length=200)
    value = models.TextField(blank=True,null=True)
Janez K's avatar
Janez K committed
958

Janez K's avatar
Janez K committed
959
    class Meta:
Janez K's avatar
Janez K committed
960
        ordering = ['name']
Janez K's avatar
Janez K committed
961
962
963
964
965
966
967
968
969
970

class Output(models.Model):
    name = models.CharField(max_length=200)
    short_name = models.CharField(max_length=5)
    description = models.TextField(blank=True)
    variable = models.CharField(max_length=50)
    widget = models.ForeignKey(Widget,related_name="outputs")
    value = PickledObjectField(null=True)
    inner_input = models.ForeignKey(Input,related_name="outer_output_rel",blank=True,null=True) #za subprocess
    outer_input = models.ForeignKey(Input,related_name="inner_output_rel",blank=True,null=True) #za subprocess
971
972
973
974
    order = models.PositiveIntegerField(default=1)

    class Meta:
        ordering = ('order',)
Janez K's avatar
Janez K committed
975

Janez K's avatar
Janez K committed
976
977
978
979
980
981
    def __unicode__(self):
        return unicode(self.name)

class UserProfile(models.Model):
    user = models.OneToOneField(User,related_name="userprofile")
    active_workflow = models.ForeignKey(Workflow,related_name="users",null=True,blank=True,on_delete=models.SET_NULL)
Janez K's avatar
Janez K committed
982

Janez K's avatar
Janez K committed
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
    def __unicode__(self):
        return unicode(self.user)

def create_user_profile(sender, instance, created, **kwargs):
    profile_set = UserProfile.objects.filter(user__id = instance.id)
    if created and not profile_set.exists():
        UserProfile.objects.create(user=instance)

# nardi da k nardimo userja da se avtomatsko nardi se UserProfile
post_save.connect(create_user_profile, sender=User)

def copy_workflow(old, user, parent_widget_conversion={},parent_input_conversion={},parent_output_conversion={},parent_widget=None):
    w = Workflow()
    if parent_widget is None:
        w.name = old.name+" (copy)"
    else:
        w.name = old.name
    w.user = user
    w.public = False
    w.description = old.description
    w.template_parent = old
    if not parent_widget is None:
        w.widget = parent_widget
    w.save()
    widget_conversion = {}
    input_conversion = {}
    output_conversion = {}
    for widget in old.widgets.all():
        new_widget = Widget()
        new_widget.workflow = w
        new_widget.x = widget.x
        new_widget.y = widget.y
        new_widget.name = widget.name
        new_widget.abstract_widget = widget.abstract_widget
        new_widget.finished = widget.finished
        new_widget.error = widget.error
        new_widget.running = widget.running
        new_widget.interaction_waiting = widget.interaction_waiting
        new_widget.type = widget.type
        new_widget.progress = widget.progress
        new_widget.save()
        widget_conversion[widget.id]=new_widget.id
        for input in widget.inputs.all():
            new_input = Input()
            new_input.name = input.name
            new_input.short_name = input.short_name
            new_input.description = input.description
            new_input.variable = input.variable
            new_input.widget = new_widget
            new_input.required = input.required
            new_input.parameter = input.parameter
            new_input.value = input.value
            new_input.multi_id = input.multi_id
            #inner_output nikol ne nastavlamo
            #outer_output in njemu spremenimo inner input
            if not parent_widget is None:
                if not input.outer_output is None:
                    new_input.outer_output = Output.objects.get(pk=parent_output_conversion[input.outer_output.id])
            new_input.parameter_type = input.parameter_type
            new_input.save()
            for option in input.options.all():
                new_option = Option()
                new_option.input = new_input
                new_option.name = option.name
                new_option.value = option.value
                new_option.save()
            if not parent_widget is None:
                if not input.outer_output is None:
                    new_input.outer_output.inner_input = new_input
                    new_input.outer_output.save()
            input_conversion[input.id]=new_input.id
        for output in widget.outputs.all():
            new_output = Output()
            new_output.name = output.name
            new_output.short_name = output.short_name
            new_output.description = output.description
            new_output.variable = output.variable
            new_output.widget = new_widget
            new_output.value = output.value
            #inner input nikol ne nastavlamo
            #outer input in njemu spremenimo inner output
            if not parent_widget is None:
                if not output.outer_input is None:
                    new_output.outer_input = Input.objects.get(pk=parent_input_conversion[output.outer_input.id])
            new_output.save()
            if not parent_widget is None:
                if not output.outer_input is None:
                    new_output.outer_input.inner_output = new_output
                    new_output.outer_input.save()
            output_conversion[output.id]=new_output.id
    for connection in old.connections.all():
        new_connection = Connection()
        new_connection.workflow = w
        new_connection.output = Output.objects.get(pk=output_conversion[connection.output.id])
        new_connection.input = Input.objects.get(pk=input_conversion[connection.input.id])
        new_connection.save()
    for widget in old.widgets.filter(type='subprocess'):
        #tuki mormo vse subprocesse zrihtat
        copy_workflow(widget.workflow_link, user, widget_conversion, input_conversion, output_conversion, Widget.objects.get(pk=widget_conversion[widget.id]))
Janez K's avatar
Janez K committed
1082
    return w