Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
LOGGER = logging.getLogger(__name__)
class Transition(BaseModel):
class Meta:
app_label = 'river'
verbose_name = _("Transition")
verbose_name_plural = _("Transitions")
objects = TransitionApprovalManager()
content_type = models.ForeignKey(app_config.CONTENT_TYPE_CLASS, verbose_name=_('Content Type'), on_delete=CASCADE)
object_id = models.CharField(max_length=50, verbose_name=_('Related Object'))
workflow_object = GenericForeignKey('content_type', 'object_id')
meta = models.ForeignKey(TransitionMeta, verbose_name=_('Meta'), related_name="transitions", on_delete=PROTECT)
workflow = models.ForeignKey(Workflow, verbose_name=_("Workflow"), related_name='transitions', on_delete=PROTECT)
source_state = models.ForeignKey(State, verbose_name=_("Source State"), related_name='transition_as_source', on_delete=PROTECT)
destination_state = models.ForeignKey(State, verbose_name=_("Destination State"), related_name='transition_as_destination', on_delete=PROTECT)
status = models.CharField(_('Status'), choices=STATUSES, max_length=100, default=PENDING)
iteration = models.IntegerField(default=0, verbose_name=_('Priority'))
@property
def next_transitions(self):
return Transition.objects.filter(
workflow=self.workflow,
workflow_object=self.workflow_object,
source_state=self.destination_state,
iteration=self.iteration + 1
)
def __init__(self, workflow_object, field_name):
self.class_workflow = getattr(workflow_object.__class__.river, field_name)
self.workflow_object = workflow_object
self.content_type = app_config.CONTENT_TYPE_CLASS.objects.get_for_model(self.workflow_object)
self.field_name = field_name
self.workflow = Workflow.objects.filter(content_type=self.content_type, field_name=self.field_name).first()
self.initialized = False
def workflow(self):
if not self._cached_workflow:
self._cached_workflow = Workflow.objects.filter(field_name=self.field_name, content_type=self._content_type).first()
return self._cached_workflow
def initial_state(self):
workflow = Workflow.objects.filter(content_type=self._content_type, field_name=self.field_name).first()
return workflow.initial_state if workflow else None
AFTER = "AFTER"
HOOK_TYPES = [
(BEFORE, _('Before')),
(AFTER, _('After')),
]
LOGGER = logging.getLogger(__name__)
class Hook(BaseModel):
class Meta:
abstract = True
callback_function = models.ForeignKey(Function, verbose_name=_("Function"), related_name='%(app_label)s_%(class)s_hooks', on_delete=PROTECT)
workflow = models.ForeignKey(Workflow, verbose_name=_("Workflow"), related_name='%(app_label)s_%(class)s_hooks', on_delete=PROTECT)
content_type = models.ForeignKey(ContentType, blank=True, null=True, on_delete=models.SET_NULL)
object_id = models.PositiveIntegerField(blank=True, null=True)
workflow_object = GenericForeignKey('content_type', 'object_id')
hook_type = models.CharField(_('When?'), choices=HOOK_TYPES, max_length=50)
def execute(self, context):
try:
self.callback_function.get()(context)
except Exception as e:
LOGGER.exception(e)
def __init__(self, workflow_object, field_name):
self.workflow_object = workflow_object
self.field_name = field_name
self.workflow = getattr(self.workflow_object.river, self.field_name)
self.status = self.workflow.on_final_state
self.content_type = ContentType.objects.get_for_model(self.workflow_object.__class__)
self.workflow = Workflow.objects.get(content_type=self.content_type, field_name=self.field_name)
from river.models.managers.transitionmetada import TransitionApprovalMetadataManager
from river.models.transitionmeta import TransitionMeta
__author__ = 'ahmetdal'
class TransitionApprovalMeta(BaseModel):
class Meta:
app_label = 'river'
verbose_name = _("Transition Approval Meta")
verbose_name_plural = _("Transition Approval Meta")
unique_together = [('workflow', 'transition_meta', 'priority')]
objects = TransitionApprovalMetadataManager()
workflow = models.ForeignKey(Workflow, verbose_name=_("Workflow"), related_name='transition_approval_metas', on_delete=PROTECT)
transition_meta = models.ForeignKey(TransitionMeta, verbose_name=_("Transition Meta"), related_name='transition_approval_meta', on_delete=PROTECT)
permissions = models.ManyToManyField(app_config.PERMISSION_CLASS, verbose_name=_('Permissions'), blank=True)
groups = models.ManyToManyField(app_config.GROUP_CLASS, verbose_name=_('Groups'), blank=True)
priority = models.IntegerField(default=0, verbose_name=_('Priority'), null=True)
parents = models.ManyToManyField('self', verbose_name='parents', related_name='children', symmetrical=False, db_index=True, blank=True)
def __str__(self):
return 'Transition: %s,Permissions: %s, Groups: %s, Order: %s' % (
self.transition_meta,
','.join(self.permissions.values_list('name', flat=True)),
','.join(self.groups.values_list('name', flat=True)), self.priority)
def post_save_model(sender, instance, *args, **kwargs):
class TransitionApproval(BaseModel):
class Meta:
app_label = 'river'
verbose_name = _("Transition Approval")
verbose_name_plural = _("Transition Approvals")
objects = TransitionApprovalManager()
content_type = models.ForeignKey(app_config.CONTENT_TYPE_CLASS, verbose_name=_('Content Type'), on_delete=CASCADE)
object_id = models.CharField(max_length=50, verbose_name=_('Related Object'))
workflow_object = GenericForeignKey('content_type', 'object_id')
meta = models.ForeignKey(TransitionApprovalMeta, verbose_name=_('Meta'), related_name="transition_approvals", null=True, blank=True, on_delete=SET_NULL)
workflow = models.ForeignKey(Workflow, verbose_name=_("Workflow"), related_name='transition_approvals', on_delete=PROTECT)
transition = models.ForeignKey(Transition, verbose_name=_("Transition"), related_name='transition_approvals', on_delete=PROTECT)
transactioner = models.ForeignKey(app_config.USER_CLASS, verbose_name=_('Transactioner'), null=True, blank=True, on_delete=SET_NULL)
transaction_date = models.DateTimeField(null=True, blank=True)
status = models.CharField(_('Status'), choices=STATUSES, max_length=100, default=PENDING)
permissions = models.ManyToManyField(app_config.PERMISSION_CLASS, verbose_name=_('Permissions'))
groups = models.ManyToManyField(app_config.GROUP_CLASS, verbose_name=_('Groups'))
priority = models.IntegerField(default=0, verbose_name=_('Priority'))
previous = TreeOneToOneField("self", verbose_name=_('Previous Transition'), related_name="next_transition", null=True, blank=True, on_delete=CASCADE)
@property
def peers(self):
class TransitionApproval(BaseModel):
class Meta:
app_label = 'river'
verbose_name = _("Transition Approval")
verbose_name_plural = _("Transition Approvals")
objects = TransitionApprovalManager()
content_type = models.ForeignKey(app_config.CONTENT_TYPE_CLASS, verbose_name=_('Content Type'), on_delete=CASCADE)
object_id = models.CharField(max_length=50, verbose_name=_('Related Object'))
workflow_object = GenericForeignKey('content_type', 'object_id')
meta = models.ForeignKey(TransitionApprovalMeta, verbose_name=_('Meta'), related_name="transition_approvals", null=True, blank=True, on_delete=SET_NULL)
workflow = models.ForeignKey(Workflow, verbose_name=_("Workflow"), related_name='transition_approvals', on_delete=PROTECT)
source_state = models.ForeignKey(State, verbose_name=_("Source State"), related_name='transition_approvals_as_source', on_delete=PROTECT)
destination_state = models.ForeignKey(State, verbose_name=_("Next State"), related_name='transition_approvals_as_destination', on_delete=PROTECT)
transactioner = models.ForeignKey(app_config.USER_CLASS, verbose_name=_('Transactioner'), null=True, blank=True, on_delete=SET_NULL)
transaction_date = models.DateTimeField(null=True, blank=True)
status = models.CharField(_('Status'), choices=STATUSES, max_length=100, default=PENDING)
skipped = models.BooleanField(_('Skip'), default=False)
permissions = models.ManyToManyField(app_config.PERMISSION_CLASS, verbose_name=_('Permissions'))
groups = models.ManyToManyField(app_config.GROUP_CLASS, verbose_name=_('Groups'))
priority = models.IntegerField(default=0, verbose_name=_('Priority'))
enabled = models.BooleanField(_('Enabled?'), default=True)