Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def remove_workflow(self):
""" Remove entirely a worflow for an instance. """
ctype = ContentType.objects.get_for_model(self)
try:
workflow = self.state.workflow
wor = workflows.models.WorkflowObjectRelation.objects.get(
content_type=ctype, content_id=self.pk
)
sor = workflows.models.StateObjectRelation.objects.get(
content_type=ctype, content_id=self.pk
)
except workflows.models.WorkflowObjectRelation.DoesNotExist:
pass
except workflows.models.StateObjectRelation.DoesNotExist:
pass
with transaction.atomic():
wor.delete()
sor.delete()
def set_state(obj, state):
"""Sets the state for the passed object to the passed state and updates
the permissions for the object.
**Parameters:**
obj
The object for which the workflow state should be set. Can be any
Django model instance.
state
The state which should be set to the passed object.
"""
ctype = ContentType.objects.get_for_model(obj)
try:
sor = StateObjectRelation.objects.get(content_type=ctype, content_id=obj.id)
except StateObjectRelation.DoesNotExist:
sor = StateObjectRelation.objects.create(content=obj, state=state)
else:
sor.state = state
sor.save()
update_permissions(obj)
def get_state(obj):
"""Returns the current workflow state for the passed object.
**Parameters:**
obj
The object for which the workflow state should be returned. Can be any
Django model instance.
"""
ctype = ContentType.objects.get_for_model(obj)
try:
sor = StateObjectRelation.objects.get(content_type=ctype, content_id=obj.id)
except StateObjectRelation.DoesNotExist:
return None
else:
return sor.state
ctype
The content type from which the passed workflow should be removed.
Must be a ContentType instance.
"""
# First delete all states, inheritance blocks and permissions from ctype's
# instances which have passed workflow.
workflow = get_workflow_for_model(ctype)
for obj in get_objects_for_workflow(workflow):
# Only take care of the given ctype.
obj_ctype = ContentType.objects.get_for_model(obj)
if ctype != obj_ctype:
continue
try:
ctype = ContentType.objects.get_for_model(obj)
sor = StateObjectRelation.objects.get(content_id=obj.id, content_type=ctype)
except StateObjectRelation.DoesNotExist:
pass
else:
sor.delete()
# Reset all permissions
permissions.utils.reset(obj)
try:
wmr = WorkflowModelRelation.objects.get(content_type=ctype)
except WorkflowModelRelation.DoesNotExist:
pass
else:
wmr.delete()