Allow TOs to be deleted, along with their associated CLINs

This commit is contained in:
richard-dds 2019-08-08 15:30:29 -04:00
parent 334a280610
commit 02900ff771
4 changed files with 44 additions and 2 deletions

View File

@ -0,0 +1,26 @@
"""clin delete cascade
Revision ID: fda6bd7e1b65
Revises: e0c6eb21771f
Create Date: 2019-08-07 16:37:02.451277
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'fda6bd7e1b65'
down_revision = 'e0c6eb21771f'
branch_labels = None
depends_on = None
def upgrade():
op.drop_constraint("clins_task_order_id_fkey", "clins")
op.create_foreign_key("clins_task_order_id_fkey", "clins", "task_orders", ["task_order_id"], ["id"], ondelete="cascade")
def downgrade():
op.drop_constraint("clins_task_order_id_fkey", "clins")
op.create_foreign_key("clins_task_order_id_fkey", "clins", "task_orders", ["task_order_id"], ["id"], ondelete="no action")

View File

@ -71,3 +71,10 @@ class TaskOrders(BaseDomainClass):
by_time_created = sorted(task_orders, key=lambda to: to.time_created) by_time_created = sorted(task_orders, key=lambda to: to.time_created)
by_status = sorted(by_time_created, key=lambda to: SORT_ORDERING.get(to.status)) by_status = sorted(by_time_created, key=lambda to: SORT_ORDERING.get(to.status))
return by_status return by_status
@classmethod
def delete(cls, task_order_id):
task_order = TaskOrders.get(task_order_id)
if task_order:
db.session.delete(task_order)
db.session.commit()

View File

@ -44,7 +44,9 @@ class TaskOrder(Base, mixins.TimestampsMixin):
signer_dod_id = Column(String) signer_dod_id = Column(String)
signed_at = Column(DateTime) signed_at = Column(DateTime)
clins = relationship("CLIN", back_populates="task_order") clins = relationship(
"CLIN", back_populates="task_order", cascade="all, delete-orphan"
)
@hybrid_property @hybrid_property
def pdf(self): def pdf(self):

View File

@ -3,7 +3,7 @@ from datetime import date, timedelta
from decimal import Decimal from decimal import Decimal
from atst.domain.task_orders import TaskOrders from atst.domain.task_orders import TaskOrders
from atst.models.attachment import Attachment from atst.models import Attachment, TaskOrder
from tests.factories import TaskOrderFactory, CLINFactory, PortfolioFactory from tests.factories import TaskOrderFactory, CLINFactory, PortfolioFactory
@ -165,3 +165,10 @@ def test_update_does_not_duplicate_clins(pdf_upload):
assert len(task_order.clins) == 2 assert len(task_order.clins) == 2
for clin in task_order.clins: for clin in task_order.clins:
assert clin.number != "456" assert clin.number != "456"
def test_delete_task_order_with_clins(session):
task_order = TaskOrderFactory.create(create_clins=[1, 2, 3])
TaskOrders.delete(task_order.id)
print(session.query(TaskOrder).filter_by(id=task_order.id).exists())