Coverage for apps/campaigns/signals.py: 48%
25 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-09-21 13:24 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-09-21 13:24 -0600
1import googlemaps
2from django.conf import settings
3from django.db.models import signals
4from django.dispatch import receiver
6from apps.accounting.models import Accounting
8from .models import (
9 GeolocationCampaign,
10 GeolocationCampaignClaim,
11 QuizzCampaignClaim,
12 SubmitCampaignClaim,
13 VideoCampaignClaim,
14)
17@receiver(signals.post_save, sender=VideoCampaignClaim)
18@receiver(signals.post_save, sender=GeolocationCampaignClaim)
19@receiver(signals.post_save, sender=SubmitCampaignClaim)
20@receiver(signals.post_save, sender=QuizzCampaignClaim)
21def update_balance_on_fan_campaign_save(sender, instance, **kwargs):
22 account, _ = Accounting.objects.get_or_create(fan=instance.fan, organization=instance.campaign.organization)
23 account.save()
26@receiver(signals.pre_save, sender=GeolocationCampaign)
27def set_latitude_longitude(sender, instance, **kwargs):
28 if instance.latitude is None or instance.longitude is None:
29 try:
30 gmaps = googlemaps.Client(key=settings.GOOGLE_API_KEY)
31 data = gmaps.geocode(instance.address)[0].get("geometry").get("location")
32 latitude = data.get("lat")
33 longitude = data.get("lng")
34 instance.latitude = latitude
35 instance.longitude = longitude
36 except googlemaps.exceptions.ApiError:
37 pass