Skip to content

Commit

Permalink
Merge pull request #1732 from BLSQ/IA-3577-remove-test-campaigns
Browse files Browse the repository at this point in the history
IA-3577 Remove test campaigns from calendar download scopes
  • Loading branch information
kemar authored Oct 21, 2024
2 parents 7d6450f + 4697652 commit 13dcad0
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 7 deletions.
15 changes: 8 additions & 7 deletions plugins/polio/api/campaigns/campaigns.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,8 +925,12 @@ def create_all_rounds_scopes_csv(self, request, **kwargs):
if round_start_to:
query_rounds &= Q(started_at__lte=round_start_to)

rounds = []
rounds = Round.objects.filter(query_rounds)
rounds = (
Round.objects.select_related("campaign")
.filter(query_rounds)
.exclude(Q(campaign__isnull=True) | Q(campaign__is_test=True))
)

# get filtered rounds
rounds = self.get_filtered_rounds(rounds, request.GET)
# The csv file name base on the start from and start to dates
Expand All @@ -935,7 +939,7 @@ def create_all_rounds_scopes_csv(self, request, **kwargs):
if round_start_from
else ("--start-from-" + current_date.strftime("%d-%m-%Y") if not round_start_to else "")
)
start_to_name = "--start-to " + round_start_to.strftime("%d-%m-%Y") if round_start_to else ""
start_to_name = "--start-to-" + round_start_to.strftime("%d-%m-%Y") if round_start_to else ""

filename = "%s%s%s" % (
"all-rounds-scopes",
Expand All @@ -947,10 +951,7 @@ def create_all_rounds_scopes_csv(self, request, **kwargs):
org_units_list = []
# loop on filtered rounds and make the org_units_list to be pushed in the csv file
for round in rounds:
campaign = round.campaign
if not campaign:
continue
org_units_list += self.get_org_units_list(round, campaign)
org_units_list += self.get_org_units_list(round, round.campaign)

response = StreamingHttpResponse(
streaming_content=(iter_items(org_units_list, Echo(), columns, self.get_row)), content_type=CONTENT_TYPE_CSV
Expand Down
105 changes: 105 additions & 0 deletions plugins/polio/tests/api/test_campaigns_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import datetime

import time_machine

from iaso import models as m
from iaso.test import APITestCase

from plugins.polio.models import Campaign, Round, CampaignScope

TODAY = datetime.datetime(2024, 10, 21, 11, 0, 0, 0, tzinfo=datetime.timezone.utc)


@time_machine.travel(TODAY, tick=False)
class CampaignCreateAllRoundsScopesCsvViewTestCase(APITestCase):
"""
Test CampaignViewSet.create_all_rounds_scopes_csv.
"""

@classmethod
def setUpTestData(cls):
cls.data_source = m.DataSource.objects.create(name="Data Source")
cls.source_version = m.SourceVersion.objects.create(data_source=cls.data_source, number=1)
cls.account = m.Account.objects.create(name="Account", default_version=cls.source_version)
cls.user = cls.create_user_with_profile(
email="[email protected]",
username="john",
first_name="John",
last_name="Doe",
account=cls.account,
)

cls.org_unit_a = m.OrgUnit.objects.create(
name="A",
org_unit_type=m.OrgUnitType.objects.create(category="COUNTRY"),
validation_status=m.OrgUnit.VALIDATION_VALID,
version=cls.source_version,
)
cls.org_unit_b = m.OrgUnit.objects.create(
name="B",
org_unit_type=m.OrgUnitType.objects.create(category="COUNTRY"),
validation_status=m.OrgUnit.VALIDATION_VALID,
version=cls.source_version,
parent=cls.org_unit_a,
)
cls.org_unit_c = m.OrgUnit.objects.create(
name="C",
org_unit_type=m.OrgUnitType.objects.create(category="COUNTRY"),
validation_status=m.OrgUnit.VALIDATION_VALID,
version=cls.source_version,
parent=cls.org_unit_b,
)

cls.group = m.Group.objects.create(name="Group", domain="POLIO", source_version=cls.source_version)
cls.group.org_units.add(cls.org_unit_c)

cls.campaign = Campaign.objects.create(obr_name="Campaign OBR name", account=cls.account)
cls.round = Round.objects.create(number=1, campaign=cls.campaign, started_at=TODAY.date())
cls.campaign_scope = CampaignScope.objects.create(campaign=cls.campaign, group=cls.group)

def test_get_create_all_rounds_scopes_csv_ok(self):
self.client.force_authenticate(self.user)
response = self.client.get(
"/api/polio/campaigns/create_all_rounds_scopes_csv/?roundStartFrom=01-10-2024&roundStartTo=31-10-2024"
)

self.assertEqual(response.status_code, 200)
self.assertEqual(
response.get("Content-Disposition"),
"attachment; filename=all-rounds-scopes--start-from-01-10-2024--start-to-31-10-2024.csv",
)

response_csv = response.getvalue().decode("utf-8")
expected_csv = (
"ID,Admin 2,Admin 1,Admin 0,OBR Name,Round Number,Start date,Vaccine\r\n"
f"{self.org_unit_c.id},C,B,A,Campaign OBR name,R1,2024-10-21,\r\n"
)
self.assertEqual(response_csv, expected_csv)

def test_test_get_create_all_rounds_scopes_csv_should_exlude_test_campaigns(self):
self.round.campaign.is_test = True
self.round.campaign.save()

self.client.force_authenticate(self.user)
response = self.client.get(
"/api/polio/campaigns/create_all_rounds_scopes_csv/?roundStartFrom=01-10-2024&roundStartTo=31-10-2024"
)

self.assertEqual(response.status_code, 200)
response_csv = response.getvalue().decode("utf-8")
expected_csv = "ID,Admin 2,Admin 1,Admin 0,OBR Name,Round Number,Start date,Vaccine\r\n"
self.assertEqual(response_csv, expected_csv)

def test_test_get_create_all_rounds_scopes_csv_should_exlude_rounds_without_campaigns(self):
self.round.campaign = None
self.round.save()

self.client.force_authenticate(self.user)
response = self.client.get(
"/api/polio/campaigns/create_all_rounds_scopes_csv/?roundStartFrom=01-10-2024&roundStartTo=31-10-2024"
)

self.assertEqual(response.status_code, 200)
response_csv = response.getvalue().decode("utf-8")
expected_csv = "ID,Admin 2,Admin 1,Admin 0,OBR Name,Round Number,Start date,Vaccine\r\n"
self.assertEqual(response_csv, expected_csv)

0 comments on commit 13dcad0

Please sign in to comment.