Commits

Peter Wright  committed 12667aa

Added ‘transition_used’ field to ‘StateObjectRelation’.

  • Participants
  • Parent commits 11aee7a

Comments (0)

Files changed (3)

File workflows/migrations/0002_auto__add_field_stateobjectrelation_transition_used.py

+# -*- coding: utf-8 -*-
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+
+class Migration(SchemaMigration):
+
+    def forwards(self, orm):
+        # Adding field 'StateObjectRelation.transition_used'
+        db.add_column('workflows_stateobjectrelation', 'transition_used',
+                      self.gf('django.db.models.fields.related.ForeignKey')(default=None, to=orm['workflows.Transition'], null=True, blank=True),
+                      keep_default=False)
+
+
+    def backwards(self, orm):
+        # Deleting field 'StateObjectRelation.transition_used'
+        db.delete_column('workflows_stateobjectrelation', 'transition_used_id')
+
+
+    models = {
+        'contenttypes.contenttype': {
+            'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
+            'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
+        },
+        'permissions.permission': {
+            'Meta': {'object_name': 'Permission'},
+            'codename': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '100'}),
+            'content_types': ('django.db.models.fields.related.ManyToManyField', [], {'blank': 'True', 'related_name': "'content_types'", 'null': 'True', 'symmetrical': 'False', 'to': "orm['contenttypes.ContentType']"}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '100'})
+        },
+        'permissions.role': {
+            'Meta': {'ordering': "('name',)", 'object_name': 'Role'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '100'})
+        },
+        'workflows.condition': {
+            'Meta': {'object_name': 'Condition'},
+            'failure_template': ('django.db.models.fields.TextField', [], {'default': "''", 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '100'})
+        },
+        'workflows.state': {
+            'Meta': {'ordering': "('name',)", 'object_name': 'State'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'transitions': ('django.db.models.fields.related.ManyToManyField', [], {'blank': 'True', 'related_name': "'states'", 'null': 'True', 'symmetrical': 'False', 'to': "orm['workflows.Transition']"}),
+            'workflow': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'states'", 'to': "orm['workflows.Workflow']"})
+        },
+        'workflows.stateinheritanceblock': {
+            'Meta': {'object_name': 'StateInheritanceBlock'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'permission': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['permissions.Permission']"}),
+            'state': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['workflows.State']"})
+        },
+        'workflows.stateobjectrelation': {
+            'Meta': {'unique_together': "(('content_type', 'content_id', 'state'),)", 'object_name': 'StateObjectRelation'},
+            'content_id': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True', 'blank': 'True'}),
+            'content_type': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'state_object'", 'null': 'True', 'to': "orm['contenttypes.ContentType']"}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'state': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['workflows.State']"}),
+            'transition_used': ('django.db.models.fields.related.ForeignKey', [], {'default': 'None', 'to': "orm['workflows.Transition']", 'null': 'True', 'blank': 'True'})
+        },
+        'workflows.statepermissionrelation': {
+            'Meta': {'object_name': 'StatePermissionRelation'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'permission': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['permissions.Permission']"}),
+            'role': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['permissions.Role']"}),
+            'state': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['workflows.State']"})
+        },
+        'workflows.transition': {
+            'Meta': {'unique_together': "(('name', 'workflow'),)", 'object_name': 'Transition'},
+            'category': ('django.db.models.fields.CharField', [], {'max_length': '100', 'db_index': 'True'}),
+            'destination': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'destination_state'", 'null': 'True', 'to': "orm['workflows.State']"}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'optional_conditions': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "'++'", 'blank': 'True', 'db_table': "'workflows_transition_optional_conditions'", 'to': "orm['workflows.Condition']"}),
+            'permission': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['permissions.Permission']", 'null': 'True', 'blank': 'True'}),
+            'required_conditions': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "'+'", 'blank': 'True', 'db_table': "'workflows_transition_required_conditions'", 'to': "orm['workflows.Condition']"}),
+            'workflow': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'transitions'", 'to': "orm['workflows.Workflow']"})
+        },
+        'workflows.workflow': {
+            'Meta': {'object_name': 'Workflow'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'initial_state': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'workflow_state'", 'null': 'True', 'to': "orm['workflows.State']"}),
+            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '100'}),
+            'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['permissions.Permission']", 'through': "orm['workflows.WorkflowPermissionRelation']", 'symmetrical': 'False'})
+        },
+        'workflows.workflowmodelrelation': {
+            'Meta': {'object_name': 'WorkflowModelRelation'},
+            'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']", 'unique': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'workflow': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'wmrs'", 'to': "orm['workflows.Workflow']"})
+        },
+        'workflows.workflowobjectrelation': {
+            'Meta': {'unique_together': "(('content_type', 'content_id'),)", 'object_name': 'WorkflowObjectRelation'},
+            'content_id': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True', 'blank': 'True'}),
+            'content_type': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'workflow_object'", 'null': 'True', 'to': "orm['contenttypes.ContentType']"}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'workflow': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'wors'", 'to': "orm['workflows.Workflow']"})
+        },
+        'workflows.workflowpermissionrelation': {
+            'Meta': {'unique_together': "(('workflow', 'permission'),)", 'object_name': 'WorkflowPermissionRelation'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'permission': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'permissions'", 'to': "orm['permissions.Permission']"}),
+            'workflow': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['workflows.Workflow']"})
+        }
+    }
+
+    complete_apps = ['workflows']

File workflows/models.py

 
     state
         The state of content. This must be a State instance.
+
+    transition_used
+        The transition that was used to move the instance into its
+        current state. This must be a Transition instance.
     """
     content_type = models.ForeignKey(
             ContentType,
     content_id = models.PositiveIntegerField(_(u"Content id"), blank=True, null=True)
     content = generic.GenericForeignKey(ct_field="content_type", fk_field="content_id")
     state = models.ForeignKey(State, verbose_name=_(u"State"))
+    transition_used = models.ForeignKey(
+            Transition,
+            null=True,
+            blank=True,
+            default=None,
+            verbose_name=_(u"Transition"),
+            )
 
     def __unicode__(self):
         return "%s %s - %s" % (self.content_type.name, self.content_id, self.state.name)

File workflows/utils.py

         return sor.state
 
 
-def set_state(obj, state):
+def set_state(obj, state, transition_used=None):
     """Sets the state for the passed object to the passed state and updates
     the permissions for the object.
 
     try:
         sor = StateObjectRelation.objects.get(content_type=ctype, content_id=obj.id)
     except StateObjectRelation.DoesNotExist:
-        sor = StateObjectRelation.objects.create(content=obj, state=state)
+        sor = StateObjectRelation.objects.create(
+                content=obj,
+                state=state,
+                transition_used=transition_used,
+                )
     else:
         sor.state = state
+        sor.transition_used = transition_used
         sor.save()
     update_permissions(obj)
 
             return False
 
         # All good, move ‘obj’ into the new state.
-        set_state(obj, transition.destination)
+        set_state(obj, state=transition.destination, transition_used=transition)
         return True
     else:
         return False