# Path: django_workflow/utils.py
# def import_from(module, name):
#     module = __import__(module, fromlist=[name])
#     return getattr(module, name)
# 
# def import_from_path(full_path):
#     parts = full_path.rsplit(".", 1)
#     return import_from(parts[0], parts[1])


import threading
import json
from datetime import timedelta, datetime
from typing import Tuple, TypeVar
from django.core.exceptions import ValidationError
from django.db import models, transaction
from django.db.models import SET_NULL
from django.utils.timezone import now as django_now
from django.utils.translation import ugettext_lazy
from django.db.models.deletion import PROTECT
from django_workflow.utils import import_from, import_from_path
        elif self.condition_opt == "or":
            return any([c.check_condition(user=user, object_id=object_id, object_state=object_state, transition=transition) for c in
                self.child_conditions.all()])

    def clone(self, *, workflow: Workflow, **defaults) -> ('Condition', 'Condition'):
        #old_condition: Condition
        new_condition, old_condition = clone(self, workflow=workflow, **defaults)
        for condition in old_condition.child_conditions.all():
            new_child, old_child = condition.clone(workflow=workflow, parent_condition=new_condition)
        for function in old_condition.function_set.all():
            new_function, old_function = function.clone(workflow=workflow, condition=new_condition)
        return new_condition, old_condition


class Function(models.Model):
    workflow = models.ForeignKey(Workflow, on_delete=PROTECT, verbose_name=ugettext_lazy("Workflow"),
        editable=False)
    function_name = models.CharField(max_length=200, verbose_name=ugettext_lazy("Function"))
    function_module = models.CharField(max_length=400, verbose_name=ugettext_lazy("Module"))
    condition = models.ForeignKey(Condition, on_delete=PROTECT, verbose_name=ugettext_lazy("Condition"))

    def __unicode__(self):
        return "{} - {}.{}".format(self.condition, self.function_module, self.function_name)

    def save(self, **qwargs):
        self.workflow = self.condition.workflow
        super(Function, self).save(**qwargs)

    @property
    def function(self):

# Path: django_workflow/utils.py
# def import_from(module, name):
#     module = __import__(module, fromlist=[name])
#     return getattr(module, name)
# 
# def import_from_path(full_path):
#     parts = full_path.rsplit(".", 1)
#     return import_from(parts[0], parts[1])


# Path: django_workflow/models.py
import threading
import json
from datetime import timedelta, datetime
from typing import Tuple, TypeVar
from django.core.exceptions import ValidationError
from django.db import models, transaction
from django.db.models import SET_NULL
from django.utils.timezone import now as django_now
from django.utils.translation import ugettext_lazy
from django.db.models.deletion import PROTECT
from django_workflow.utils import import_from, import_from_path

        elif self.condition_opt == "or":
            return any([c.check_condition(user=user, object_id=object_id, object_state=object_state, transition=transition) for c in
                self.child_conditions.all()])

    def clone(self, *, workflow: Workflow, **defaults) -> ('Condition', 'Condition'):
        #old_condition: Condition
        new_condition, old_condition = clone(self, workflow=workflow, **defaults)
        for condition in old_condition.child_conditions.all():
            new_child, old_child = condition.clone(workflow=workflow, parent_condition=new_condition)
        for function in old_condition.function_set.all():
            new_function, old_function = function.clone(workflow=workflow, condition=new_condition)
        return new_condition, old_condition


class Function(models.Model):
    workflow = models.ForeignKey(Workflow, on_delete=PROTECT, verbose_name=ugettext_lazy("Workflow"),
        editable=False)
    function_name = models.CharField(max_length=200, verbose_name=ugettext_lazy("Function"))
    function_module = models.CharField(max_length=400, verbose_name=ugettext_lazy("Module"))
    condition = models.ForeignKey(Condition, on_delete=PROTECT, verbose_name=ugettext_lazy("Condition"))

    def __unicode__(self):
        return "{} - {}.{}".format(self.condition, self.function_module, self.function_name)

    def save(self, **qwargs):
        self.workflow = self.condition.workflow
        super(Function, self).save(**qwargs)

    @property
    def function(self):

        return import_from(self.function_module, self.function_name)