Skip to content

Commit

Permalink
rework application relationship (#3912) (patch)
Browse files Browse the repository at this point in the history
### Changed

- application.order_types is a modifiable list
  • Loading branch information
islean authored Nov 6, 2024
1 parent 99d321c commit 37cf6f3
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 deletions.
11 changes: 4 additions & 7 deletions cg/server/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ def is_external_application(unused1, unused2, model, unused3):

def view_order_types(unused1, unused2, model, unused3):
del unused1, unused2, unused3
order_type_list = "<br>".join([order_type.order_type for order_type in model.order_types])
order_type_list = "<br>".join(model.order_types)
return (
Markup(f'<div style="display: inline-block; min-width: 200px;">{order_type_list}</div>')
if model.order_types
if model.order_type_applications
else ""
)

Expand Down Expand Up @@ -168,7 +168,7 @@ class ApplicationView(BaseView):
}
column_filters = ["prep_category", "is_accredited"]
column_searchable_list = ["tag", "prep_category"]
form_excluded_columns = ["category", "versions", "order_types"]
form_excluded_columns = ["category", "versions", "order_type_applications"]
form_extra_fields = {
"suitable_order_types": MultiCheckboxField(
"Order Types", choices=[(choice, choice.name) for choice in OrderType]
Expand Down Expand Up @@ -205,10 +205,7 @@ def edit_form(self, obj=None):

# Pre-select the existing order types for the application
if obj and request.method != "POST":
form.suitable_order_types.data = [
order_type.order_type for order_type in obj.order_types
]

form.suitable_order_types.data = obj.order_types
return form


Expand Down
9 changes: 7 additions & 2 deletions cg/store/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
from sqlalchemy import and_, func
from sqlalchemy.orm import Query, Session

from cg.store.models import Analysis, Application, ApplicationLimitations, ApplicationVersion
from cg.store.models import (
Analysis,
Application,
ApplicationLimitations,
ApplicationVersion,
)
from cg.store.models import Base as ModelBase
from cg.store.models import (
Case,
Expand Down Expand Up @@ -82,7 +87,7 @@ def _get_join_sample_case_order_query(self) -> Query:

def _get_join_application_ordertype_query(self) -> Query:
"""Return join application to order type query."""
return self._get_query(table=Application).join(Application.order_types)
return self._get_query(table=Application).join(Application.order_type_applications)

def _get_join_sample_application_version_query(self) -> Query:
"""Return join sample to application version query."""
Expand Down
21 changes: 18 additions & 3 deletions cg/store/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,23 @@ class Application(Base):
pipeline_limitations: Mapped[list["ApplicationLimitations"]] = orm.relationship(
back_populates="application"
)
order_types: Mapped[list["OrderTypeApplication"]] = orm.relationship(
"OrderTypeApplication", back_populates="application"
order_type_applications: Mapped[list["OrderTypeApplication"]] = orm.relationship(
"OrderTypeApplication",
back_populates="application",
)

@property
def order_types(self) -> list[OrderType]:
return [entry.order_type for entry in self.order_type_applications]

@order_types.setter
def order_types(self, order_types: list[OrderType]) -> None:
self.order_type_applications.clear()
self.order_type_applications = [
OrderTypeApplication(order_type=order_type, application_id=self.id)
for order_type in order_types
]

def __str__(self) -> str:
return self.tag

Expand Down Expand Up @@ -1205,4 +1218,6 @@ class OrderTypeApplication(Base):
application_id: Mapped[int] = mapped_column(
ForeignKey("application.id", ondelete="CASCADE"), primary_key=True
)
application: Mapped[Application] = orm.relationship("Application", back_populates="order_types")
application: Mapped[Application] = orm.relationship(
"Application", back_populates="order_type_applications"
)
2 changes: 1 addition & 1 deletion tests/store/crud/read/test_read_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_get_active_applications_by_order_type_no_application(store: Store):
# GIVEN a store with applications without order types
applications: list[Application] = store.get_applications()
for application in applications:
assert not application.order_types
assert not application.order_type_applications

# GIVEN an order type
order_type: OrderType = OrderType.PACBIO_LONG_READ
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from sqlalchemy.orm import Query

from cg.models.orders.constants import OrderType
from cg.store.filters.status_ordertype_application_filters import filter_applications_by_order_type
from cg.store.models import Application, OrderTypeApplication
from cg.store.filters.status_ordertype_application_filters import (
filter_applications_by_order_type,
)
from cg.store.models import Application
from cg.store.store import Store
from tests.store_helpers import StoreHelpers

Expand All @@ -26,11 +28,14 @@ def test_filter_applications_by_order_type(applications_store: Store, helpers: S
)

# WHEN filtering applications by order type
order_type_applications: Query = applications_store._get_query(table=OrderTypeApplication)
order_type_applications: Query = applications_store._get_query(table=Application).join(
Application.order_type_applications
)
filtered_order_type_applications: Query = filter_applications_by_order_type(
order_type_applications=order_type_applications, order_type=order_type
)

# THEN assert that only the applications with the given order type are returned
assert filtered_order_type_applications.count() == 1
assert filtered_order_type_applications.first().order_type == order_type
application = filtered_order_type_applications.first()
assert order_type in application.order_types

0 comments on commit 37cf6f3

Please sign in to comment.