From d46ed2b5b4ac2b3e36b4a7c42b7fc7694997f293 Mon Sep 17 00:00:00 2001 From: graham-dds Date: Wed, 19 Feb 2020 16:41:51 -0500 Subject: [PATCH] Specify clin sorting in TO <> CLIN relationship This also removes the sorted_clins property on the task_order model --- atst/models/portfolio.py | 2 +- atst/models/task_order.py | 13 ++++++------- .../task_orders/fragments/task_order_view.html | 2 +- tests/models/test_task_order.py | 2 +- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/atst/models/portfolio.py b/atst/models/portfolio.py index c006dd37..f683ac30 100644 --- a/atst/models/portfolio.py +++ b/atst/models/portfolio.py @@ -202,7 +202,7 @@ class Portfolio( try: initial_task_order: TaskOrder = self.task_orders[0] - initial_clin = initial_task_order.sorted_clins[0] + initial_clin = initial_task_order.clins[0] portfolio_data.update( { "initial_clin_amount": initial_clin.obligated_amount, diff --git a/atst/models/task_order.py b/atst/models/task_order.py index d2a63964..a931df9b 100644 --- a/atst/models/task_order.py +++ b/atst/models/task_order.py @@ -3,12 +3,13 @@ from enum import Enum from sqlalchemy import Column, DateTime, ForeignKey, String from sqlalchemy.ext.hybrid import hybrid_property from sqlalchemy.orm import relationship - +from atst.models.clin import CLIN from atst.models.base import Base import atst.models.types as types import atst.models.mixins as mixins from atst.models.attachment import Attachment from pendulum import today +from sqlalchemy import func class Status(Enum): @@ -41,15 +42,13 @@ class TaskOrder(Base, mixins.TimestampsMixin): number = Column(String, unique=True,) # Task Order Number signer_dod_id = Column(String) signed_at = Column(DateTime) - clins = relationship( - "CLIN", back_populates="task_order", cascade="all, delete-orphan" + "CLIN", + back_populates="task_order", + cascade="all, delete-orphan", + order_by=lambda: [func.substr(CLIN.number, 2), func.substr(CLIN.number, 1, 2)], ) - @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/task_orders/fragments/task_order_view.html b/templates/task_orders/fragments/task_order_view.html index c78434ee..28978c96 100644 --- a/templates/task_orders/fragments/task_order_view.html +++ b/templates/task_orders/fragments/task_order_view.html @@ -57,7 +57,7 @@ - {% for clin in task_order.sorted_clins %} + {% for clin in task_order.clins %} {{ clin.number }} {{ clin.type }} diff --git a/tests/models/test_task_order.py b/tests/models/test_task_order.py index 0b03cbf8..7b280c74 100644 --- a/tests/models/test_task_order.py +++ b/tests/models/test_task_order.py @@ -62,7 +62,7 @@ def test_clin_sorting(): CLINFactory.create(number="2001"), ] ) - assert [clin.number for clin in task_order.sorted_clins] == [ + assert [clin.number for clin in task_order.clins] == [ "0001", "1001", "2001",