Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change owner obfuscation of half-hitas owners to use completion date instead of purchase date #478

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions backend/hitas/services/owner.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from auditlog.context import disable_auditlog
from dateutil.relativedelta import relativedelta
from django.db import models
from django.db.models import Count, Max, OuterRef, Q, QuerySet, Subquery
from django.db.models import Count, OuterRef, Q, QuerySet, Subquery
from django.db.models.functions import Coalesce
from django.utils import timezone

from hitas.models.housing_company import HitasType, RegulationStatus
from hitas.models.owner import Owner, OwnerT
from hitas.models.ownership import Ownership, OwnershipWithApartmentCount
from hitas.utils import SQSum, subquery_count
from hitas.utils import SQSum, max_date_if_all_not_null, subquery_count


def exclude_obfuscated_owners(owners: QuerySet[Owner]) -> QuerySet[Owner]:
Expand Down Expand Up @@ -47,7 +47,7 @@ def obfuscate_owners_without_regulated_apartments() -> list[OwnerT]:
),
0,
),
_latest_half_hitas_sale_purchase_date=Subquery(
_latest_half_hitas_completion_date=Subquery(
queryset=(
Ownership.objects.select_related(
"owner",
Expand All @@ -57,18 +57,22 @@ def obfuscate_owners_without_regulated_apartments() -> list[OwnerT]:
owner__id=OuterRef("id"),
sale__apartment__building__real_estate__housing_company__hitas_type=HitasType.HALF_HITAS,
)
.annotate(_latest_purchase_date=Max("sale__purchase_date"))
.order_by("-_latest_purchase_date")
.values_list("_latest_purchase_date", flat=True)[:1]
.annotate(
_completion_date=max_date_if_all_not_null(
"sale__apartment__building__real_estate__housing_company__real_estates__buildings__apartments__completion_date"
)
)
.order_by("-_completion_date")
.values_list("_completion_date", flat=True)[:1]
),
output_field=models.DateField(null=True),
),
).filter(
(
# If owner owns any half-hitas apartments, they must have been purchased
# If owner owns any half-hitas apartments, their housing companies must have been completed
# at least 2 years ago so that the owner is allowed to be obfuscated
Q(_latest_half_hitas_sale_purchase_date__isnull=True)
| Q(_latest_half_hitas_sale_purchase_date__lte=timezone.now().date() - relativedelta(years=2))
Q(_latest_half_hitas_completion_date__isnull=True)
| Q(_latest_half_hitas_completion_date__lte=timezone.now().date() - relativedelta(years=2))
),
_owned_regulated_apartments=0,
)
Expand Down
1 change: 1 addition & 0 deletions backend/hitas/tests/services/test_services_owner.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ def test_obfuscate_owners_without_regulated_apartments__half_hitas_sales_after_r
sales=[],
building__real_estate__housing_company__regulation_status=RegulationStatus.RELEASED_BY_HITAS,
building__real_estate__housing_company__hitas_type=HitasType.HALF_HITAS,
completion_date="2020-01-01",
)

unsold_apartments_count = get_number_of_unsold_apartments(apartment.housing_company)
Expand Down
Loading