Skip to content

Commit

Permalink
Merge pull request #780 from readthedocs/davidfischer/notify-ad-image…
Browse files Browse the repository at this point in the history
…-changes

Slack alert when ad images change
  • Loading branch information
davidfischer committed Aug 16, 2023
2 parents b0003dd + 6ad799d commit d6ff4a0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
13 changes: 12 additions & 1 deletion adserver/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from .models import Publisher
from .models import Region
from .models import Topic
from .tasks import notify_on_ad_image_change
from .validators import TargetingParametersValidator


Expand Down Expand Up @@ -953,7 +954,17 @@ def save(self, commit=True):
if not self.instance.slug:
# Only needed on create
self.instance.slug = self.generate_slug()
return super().save(commit)

new_instance = super().save(commit)

# Check if the image has changed
# We alert on this as a secondary check for malicious images
# https://docs.djangoproject.com/en/3.2/ref/forms/api/#django.forms.Form.changed_data
if new_instance.image and "image" in self.changed_data:
log.debug("Image field has changed: %s", new_instance.image.url)
notify_on_ad_image_change.apply_async(args=[new_instance.pk])

return new_instance

class Meta:
model = Advertisement
Expand Down
17 changes: 17 additions & 0 deletions adserver/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,23 @@ def calculate_ad_ctrs(days=7, min_views=1_000):
ad.save()


@app.task()
def notify_on_ad_image_change(advertisement_id):
ad = Advertisement.objects.filter(id=advertisement_id).first()
if not ad or not ad.image:
log.warning("Invalid ad passed to 'notify_on_ad_image_change'")
return

ad_url = generate_absolute_url(ad.get_absolute_url())
message = f"Ad <{ad_url}|{ad.name}> had its image changed to {ad.image.url}"

log.info(message)
slack_message(
"adserver/slack/generic-message.slack",
{"text": message},
)


@app.task()
def notify_of_completed_flights():
"""Send a note and close flights which completed in the last day."""
Expand Down

0 comments on commit d6ff4a0

Please sign in to comment.