From 41bbbe8a393e438e29ee9da7005b025d60b72d1b Mon Sep 17 00:00:00 2001 From: graham-dds Date: Tue, 3 Sep 2019 11:10:12 -0400 Subject: [PATCH] add a sorted_clins property for clin sorting logic CLINS have a special ordering: - First, they are sorted by the last three digits - Then, they are sorted by the first digit Trying to add CLIN sorting logic to the relationship field in the task order proved to be more challenging than expected. So, a separate property was defined in order to access the clins in sorted order. --- atst/models/clin.py | 2 +- atst/models/task_order.py | 4 ++++ templates/fragments/task_order_review.html | 2 +- tests/models/test_task_order.py | 19 +++++++++++++++++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/atst/models/clin.py b/atst/models/clin.py index ba64405e..da2d1d02 100644 --- a/atst/models/clin.py +++ b/atst/models/clin.py @@ -38,7 +38,7 @@ class CLIN(Base, mixins.TimestampsMixin): @property def type(self): return "Base" if self.number[0] == "0" else "Option" - + @property def is_completed(self): return all( diff --git a/atst/models/task_order.py b/atst/models/task_order.py index 8d2f03fc..bd592bea 100644 --- a/atst/models/task_order.py +++ b/atst/models/task_order.py @@ -47,6 +47,10 @@ class TaskOrder(Base, mixins.TimestampsMixin): "CLIN", back_populates="task_order", cascade="all, delete-orphan" ) + @property + def sorted_clins(self): + return sorted(self.clins, key=lambda clin: (clin.number[1:], clin.number[0])) + @hybrid_property def pdf(self): return self._pdf diff --git a/templates/fragments/task_order_review.html b/templates/fragments/task_order_review.html index 80029eb8..29b013c5 100644 --- a/templates/fragments/task_order_review.html +++ b/templates/fragments/task_order_review.html @@ -49,7 +49,7 @@ - {% for clin in task_order.clins %} + {% for clin in task_order.sorted_clins %} {{ clin.number }} {{ clin.type }} diff --git a/tests/models/test_task_order.py b/tests/models/test_task_order.py index 88dd8aa5..19a2a6af 100644 --- a/tests/models/test_task_order.py +++ b/tests/models/test_task_order.py @@ -52,6 +52,25 @@ def test_task_order_clins_are_completed(): assert not TaskOrderFactory.create(clins=[]).clins_are_completed +def test_clin_sorting(): + task_order = TaskOrderFactory.create( + clins=[ + CLINFactory.create(number="0002"), + CLINFactory.create(number="0001"), + CLINFactory.create(number="1001"), + CLINFactory.create(number="1002"), + CLINFactory.create(number="2001"), + ] + ) + assert [clin.number for clin in task_order.sorted_clins] == [ + "0001", + "1001", + "2001", + "0002", + "1002", + ] + + class TestTaskOrderStatus: @patch("atst.models.TaskOrder.is_completed", new_callable=PropertyMock) @patch("atst.models.TaskOrder.is_signed", new_callable=PropertyMock)