Skip to content

Commit

Permalink
Flight request saves the requested targeting
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfischer committed Nov 6, 2023
1 parent 5c22472 commit b4626a2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
14 changes: 14 additions & 0 deletions adserver/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,10 @@ def create_form_helper(self):
def save(self, commit=True):
assert commit, "Delayed saving is not supported on this form"

if not self.instance.targeting_parameters:
# This can happen if the flight was setup with no targeting at all
self.instance.targeting_parameters = {}

# This is already the default but we are setting it explicitly to be defensive
self.instance.live = False

Expand All @@ -785,6 +789,16 @@ def save(self, commit=True):
else self.advertiser.campaigns.first()
)

# Set the regions and/or topics they set in the form
if self.cleaned_data["regions"]:
self.instance.targeting_parameters["include_regions"] = self.cleaned_data[
"regions"
]
if self.cleaned_data["topics"]:
self.instance.targeting_parameters["include_topics"] = self.cleaned_data[
"topics"
]

instance = super().save(commit)

# Duplicate the advertisements into the new flight
Expand Down
24 changes: 23 additions & 1 deletion adserver/tests/test_advertiser_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from ..models import Campaign
from ..models import Flight
from ..models import Publisher
from ..models import Region
from ..models import Topic
from ..utils import get_ad_day
from .common import ONE_PIXEL_PNG_BYTES

Expand Down Expand Up @@ -512,6 +514,20 @@ def test_flight_request_view(self):
backend = get_backend()
backend.reset_messages()

# Set regions and topics that are available to be chosen
Region.objects.filter(slug__in=Region.NON_OVERLAPPING_REGIONS).update(
selectable=True
)
Topic.objects.filter(
slug__in=(
"devops",
"backend-web",
"frontend-web",
"data-science",
"security-privacy",
)
).update(selectable=True)

new_name = "My New Flight"
today = get_ad_day().date()
budget = "3599"
Expand All @@ -522,10 +538,14 @@ def test_flight_request_view(self):
"end_date": today + datetime.timedelta(days=20),
"advertisements": [],
"budget": budget,
"regions": ["us-ca"],
"topics": ["devops"],
"note": note,
}

resp = self.client.post(url, data=data, follow=True)
resp = self.client.post(
url + "?old_flight=&next=step-2", data=data, follow=True
)
self.assertEqual(resp.status_code, 200)
self.assertContains(resp, f"Successfully setup a new")
self.assertContains(resp, f"notified your account manager")
Expand All @@ -547,6 +567,8 @@ def test_flight_request_view(self):
new_flight = Flight.objects.filter(name=new_name).first()
self.assertIsNotNone(new_flight)
self.assertFalse(new_flight.live)
self.assertEqual(new_flight.targeting_parameters["include_regions"], ["us-ca"])
self.assertEqual(new_flight.targeting_parameters["include_topics"], ["devops"])

backend.reset_messages()

Expand Down

0 comments on commit b4626a2

Please sign in to comment.