Skip to content

Commit

Permalink
[#129] Handle string primary keys properly
Browse files Browse the repository at this point in the history
  • Loading branch information
javrasya committed Jan 28, 2020
1 parent 23795db commit dbe2d81
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
6 changes: 3 additions & 3 deletions river/core/classworkflowobject.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.contrib import auth
from django.contrib.contenttypes.models import ContentType
from django.db.models import F, Q, IntegerField, Min
from django.db.models import F, Q, Min, CharField
from django.db.models.functions import Cast
from django_cte import With

Expand Down Expand Up @@ -47,12 +47,12 @@ def get_available_approvals(self, as_user):
).with_cte(
those_with_max_priority
).annotate(
object_id_as_int=Cast('object_id', IntegerField()),
object_id_as_str=Cast('object_id', CharField(max_length=200)),
min_priority=those_with_max_priority.col.min_priority
).filter(min_priority=F("priority"))

return workflow_objects.join(
approvals_with_max_priority, object_id_as_int=workflow_objects.col.pk
approvals_with_max_priority, object_id_as_str=workflow_objects.col.pk
).with_cte(
workflow_objects
).filter(transition__source_state=getattr(workflow_objects.col, self.field_name + "_id"))
Expand Down
32 changes: 30 additions & 2 deletions river/tests/core/test__instance_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
from hamcrest import assert_that, equal_to, has_item, has_property, raises, calling, has_length, is_not, all_of, none, has_items

from river.models import TransitionApproval, PENDING, CANCELLED, APPROVED, Transition, JUMPED
from river.models.factories import UserObjectFactory, StateObjectFactory, TransitionApprovalMetaFactory, PermissionObjectFactory, WorkflowFactory, TransitionMetaFactory
from river.models.factories import UserObjectFactory, StateObjectFactory, TransitionApprovalMetaFactory, PermissionObjectFactory, WorkflowFactory, \
TransitionMetaFactory
from river.tests.matchers import has_permission, has_transition
from river.tests.models import BasicTestModel, ModelWithTwoStateFields
from river.tests.models import BasicTestModel, ModelWithTwoStateFields, ModelWithStringPrimaryKey
from river.tests.models.factories import BasicTestModelObjectFactory, ModelWithTwoStateFieldsObjectFactory
from river.utils.exceptions import RiverException

Expand Down Expand Up @@ -1766,3 +1767,30 @@ def test_shouldJumpToASpecificStateWhenThereAreMultipleNextState(self):
)
)
)

def test_shouldNotCrashWhenAModelObjectWithStringPrimaryKeyIsApproved(self):
state1 = StateObjectFactory(label="state1")
state2 = StateObjectFactory(label="state2")

content_type = ContentType.objects.get_for_model(ModelWithStringPrimaryKey)
authorized_permission = PermissionObjectFactory(content_type=content_type)
authorized_user = UserObjectFactory(user_permissions=[authorized_permission])
workflow = WorkflowFactory(initial_state=state1, content_type=content_type, field_name="status")

transition_meta = TransitionMetaFactory.create(
workflow=workflow,
source_state=state1,
destination_state=state2,
)
TransitionApprovalMetaFactory.create(
workflow=workflow,
transition_meta=transition_meta,
priority=0,
permissions=[authorized_permission]
)

workflow_object = ModelWithStringPrimaryKey.objects.create()

assert_that(workflow_object.status, equal_to(state1))
workflow_object.river.status.approve(as_user=authorized_user)
assert_that(workflow_object.status, equal_to(state2))
7 changes: 7 additions & 0 deletions river/tests/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from uuid import uuid4

from django.db import models

from river.models.fields.state import StateField
Expand Down Expand Up @@ -28,3 +30,8 @@ class ModelForSlowCase2(models.Model):
class ModelWithTwoStateFields(models.Model):
status1 = StateField()
status2 = StateField()


class ModelWithStringPrimaryKey(models.Model):
custom_pk = models.CharField(max_length=200, primary_key=True, default=uuid4())
status = StateField()

0 comments on commit dbe2d81

Please sign in to comment.