Skip to content

Commit

Permalink
Use real edits for exclusions
Browse files Browse the repository at this point in the history
  • Loading branch information
eadpearce committed Oct 25, 2024
1 parent f4f927d commit 7c8d2c0
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 9 deletions.
108 changes: 108 additions & 0 deletions quotas/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1905,6 +1905,114 @@ def test_quota_update_existing_origin_exclusion_remove(
) == ["Delete", "Delete", "Update", "Update"]


def test_quota_update_existing_origin_and_exclusions_remove(
client_with_current_workbasket,
date_ranges,
):
country1 = factories.CountryFactory.create()
country2 = factories.CountryFactory.create()
country3 = factories.CountryFactory.create()
geo_group = factories.GeoGroupFactory.create()
membership1 = factories.GeographicalMembershipFactory.create(
member=country1,
geo_group=geo_group,
)
membership2 = factories.GeographicalMembershipFactory.create(
member=country2,
geo_group=geo_group,
)

origin1 = factories.QuotaOrderNumberOriginFactory.create(geographical_area=country3)
quota = origin1.order_number

origin2 = factories.QuotaOrderNumberOriginFactory.create(
geographical_area=geo_group,
order_number=quota,
)
exclusion1 = factories.QuotaOrderNumberOriginExclusionFactory.create(
excluded_geographical_area=membership1.member,
origin=origin2,
)
exclusion2 = factories.QuotaOrderNumberOriginExclusionFactory.create(
excluded_geographical_area=membership2.member,
origin=origin2,
)

# sanity check
tx = Transaction.objects.last()
assert quota.quotaordernumberorigin_set.approved_up_to_transaction(tx).count() == 2
assert (
origin1.quotaordernumberoriginexclusion_set.approved_up_to_transaction(
tx,
).count()
== 0
)
assert (
origin2.quotaordernumberoriginexclusion_set.approved_up_to_transaction(
tx,
).count()
== 2
)

data = {
"start_date_0": quota.valid_between.lower.day,
"start_date_1": quota.valid_between.lower.month,
"start_date_2": quota.valid_between.lower.year,
"end_date_0": "",
"end_date_1": "",
"end_date_2": "",
"category": quota.category,
"origins-0-pk": origin1.pk,
"origins-0-start_date_0": origin1.valid_between.lower.day,
"origins-0-start_date_1": origin1.valid_between.lower.month,
"origins-0-start_date_2": origin1.valid_between.lower.year,
"origins-0-end_date_0": "",
"origins-0-end_date_1": "",
"origins-0-end_date_2": "",
"origins-0-geographical_area": origin1.geographical_area.pk,
# remove the second origin and its exclusion
"submit": "Save",
}

url = reverse("quota-ui-edit", kwargs={"sid": quota.sid})
response = client_with_current_workbasket.post(url, data)

assert response.status_code == 302
assert response.url == reverse("quota-ui-confirm-update", kwargs={"sid": quota.sid})

last_tx = Transaction.objects.last()

updated_quota = (
models.QuotaOrderNumber.objects.approved_up_to_transaction(last_tx)
.filter(sid=quota.sid)
.first()
)

assert updated_quota.origins.approved_up_to_transaction(last_tx).count() == 1
updated_origin = (
updated_quota.quotaordernumberorigin_set.approved_up_to_transaction(
last_tx,
).first()
)
assert (
updated_origin.quotaordernumberoriginexclusion_set.approved_up_to_transaction(
last_tx,
).count()
== 0
)
# update quota
# update origin 1 to link to updated quota
# delete quota origin 2
# delete both quota origin 2 exclusions
assert updated_origin.transaction.workbasket.tracked_models.count() == 5
assert sorted(
[
item.get_update_type_display()
for item in updated_origin.transaction.workbasket.tracked_models.all()
],
) == ["Delete", "Delete", "Delete", "Update", "Update"]


@pytest.mark.parametrize(
"data, expected_model",
[
Expand Down
22 changes: 13 additions & 9 deletions quotas/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,6 @@ def get_form_kwargs(self):
return kwargs

def update_origins(self, instance, form_origins):
workbasket_origins = self.workbasket.tracked_models.instance_of(
models.QuotaOrderNumberOrigin,
)
existing_origin_pks = {origin.pk for origin in instance.get_current_origins()}

if form_origins:
Expand All @@ -421,20 +418,27 @@ def update_origins(self, instance, form_origins):
origin = models.QuotaOrderNumberOrigin.objects.get(
pk=origin_pk,
)
origin.new_version(
new_tx = self.workbasket.new_transaction()
make_real_edit(
tx=new_tx,
cls=models.QuotaOrderNumberOrigin,
obj=origin,
data=None,
workbasket=self.workbasket,
update_type=UpdateType.DELETE,
workbasket=WorkBasket.current(self.request),
transaction=instance.transaction,
)
# Delete the exclusions as well
exclusions = models.QuotaOrderNumberOriginExclusion.objects.filter(
origin__pk=origin_pk,
)
for exclusion in exclusions:
exclusion.new_version(
make_real_edit(
tx=new_tx,
cls=models.QuotaOrderNumberOriginExclusion,
obj=exclusion,
data=None,
workbasket=self.workbasket,
update_type=UpdateType.DELETE,
workbasket=WorkBasket.current(self.request),
transaction=instance.transaction,
)

for origin_data in form_origins:
Expand Down

0 comments on commit 7c8d2c0

Please sign in to comment.