Skip to content

Commit

Permalink
Merge pull request #239 from City-of-Helsinki/fix-apartment-sales-list
Browse files Browse the repository at this point in the history
Fix apartment sales list showing sales not for the apartment
  • Loading branch information
matti-lamppu authored Mar 3, 2023
2 parents 876f2e3 + 93fe2a7 commit 09970e2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
30 changes: 30 additions & 0 deletions backend/hitas/tests/apis/test_api_apartment_sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,36 @@ def test__api__apartment_sale__list(api_client: HitasAPIClient):
}


@pytest.mark.django_db
def test__api__apartment_sale__list__dont_include_sales_for_other_apartments(api_client: HitasAPIClient):
apartment: Apartment = ApartmentFactory.create(sales=[])

# Sales for some other apartment
ownership: Ownership = OwnershipFactory.create(apartment__sales=[])
ApartmentSaleFactory.create(ownerships=[ownership])

url = reverse(
"hitas:apartment-sale-list",
kwargs={
"housing_company_uuid": apartment.housing_company.uuid.hex,
"apartment_uuid": apartment.uuid.hex,
},
)
response = api_client.get(url)
assert response.status_code == status.HTTP_200_OK, response.json()
assert response.json()["contents"] == []
assert response.json()["page"] == {
"size": 0,
"current_page": 1,
"total_items": 0,
"total_pages": 1,
"links": {
"next": None,
"previous": None,
},
}


# Retrieve tests


Expand Down
5 changes: 3 additions & 2 deletions backend/hitas/views/apartment_sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from hitas.models.ownership import Ownership, OwnershipLike, check_ownership_percentages
from hitas.services.apartment import prefetch_first_sale
from hitas.services.condition_of_sale import create_conditions_of_sale
from hitas.utils import lookup_id_to_uuid
from hitas.utils import lookup_id_to_uuid, lookup_model_id_by_uuid
from hitas.views.ownership import OwnershipSerializer
from hitas.views.utils import HitasModelSerializer, HitasModelViewSet

Expand Down Expand Up @@ -121,9 +121,10 @@ class ApartmentSaleViewSet(HitasModelViewSet):
model_class = ApartmentSale

def get_default_queryset(self):
apartment_id = lookup_model_id_by_uuid(self.kwargs["apartment_uuid"], Apartment)
return ApartmentSale.objects.prefetch_related(
Prefetch(
"ownerships",
Ownership.objects.select_related("owner"),
),
)
).filter(apartment_id=apartment_id)

0 comments on commit 09970e2

Please sign in to comment.