From 335b042e43ec1c9223d4f82d1dc9930cfa12b108 Mon Sep 17 00:00:00 2001 From: 3k3n3 <80990247+3k3n3@users.noreply.github.com> Date: Thu, 7 Sep 2023 00:38:02 +0100 Subject: [PATCH] Fixed impressions and clicks not showing for anonymous users --- ads/__init__.py | 2 +- ads/utils.py | 34 +++++++++++++++++++--------------- ads/views.py | 45 ++++++++++++++++++++------------------------- 3 files changed, 40 insertions(+), 41 deletions(-) diff --git a/ads/__init__.py b/ads/__init__.py index cbe86b0..461ff04 100644 --- a/ads/__init__.py +++ b/ads/__init__.py @@ -1,3 +1,3 @@ default_app_config = 'ads.apps.AdsConfig' -__version__ = '1.2.0' +__version__ = '1.2.1' diff --git a/ads/utils.py b/ads/utils.py index 92d6f58..7468c89 100644 --- a/ads/utils.py +++ b/ads/utils.py @@ -7,15 +7,15 @@ def get_zones_choices(): for key in sorted(settings.ADS_ZONES): - yield (key, _(settings.ADS_ZONES[key].get('name', 'Undefined'))) + yield (key, _(settings.ADS_ZONES[key].get("name", "Undefined"))) def get_client_ip(request): - x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR', None) + x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR", None) if x_forwarded_for: - ip = x_forwarded_for.split(',')[0] + ip = x_forwarded_for.split(",")[0] else: - ip = request.META.get('REMOTE_ADDR', '') + ip = request.META.get("REMOTE_ADDR", "") return ip @@ -26,18 +26,22 @@ def update_clicks(ad, request): ad=ad, session_id=request.session.session_key, defaults={ - 'click_date': timezone.now(), - 'source_ip': get_client_ip(request), - }) + "click_date": timezone.now(), + "source_ip": get_client_ip(request), + }, + ) def update_impressions(ad, request): if ad is not None: - if request.session.session_key: - impression, created = Impression.objects.get_or_create( - ad=ad, - session_id=request.session.session_key, - defaults={ - 'impression_date': timezone.now(), - 'source_ip': get_client_ip(request), - }) + # if request.session.session_key: + request.session["django-ads"] = True + request.session.save() + impression, created = Impression.objects.get_or_create( + ad=ad, + session_id=request.session.session_key, + defaults={ + "impression_date": timezone.now(), + "source_ip": get_client_ip(request), + }, + ) diff --git a/ads/views.py b/ads/views.py index 64f1f50..ccdd62a 100644 --- a/ads/views.py +++ b/ads/views.py @@ -11,49 +11,44 @@ class AdImpressionView(JSONResponseMixin, View): - json_dumps_kwargs = {u"indent": 2} + json_dumps_kwargs = {"indent": 2} def get_object(self): - zone = self.kwargs.get('zone', None) + zone = self.kwargs.get("zone", None) return Ad.objects.random_ad(zone) def get_ad_context_dict(self, zone): ad = Ad.objects.random_ad(zone) if ad: - context_dict = { - 'url': ad.get_absolute_url(), - 'images': {} - } + context_dict = {"url": ad.get_absolute_url(), "images": {}} for image in ad.images.all(): - context_dict['images'].update({ - image.device: { - 'url': image.image.url, - 'size': image.size - } - }) + context_dict["images"].update( + {image.device: {"url": image.image.url, "size": image.size}} + ) return context_dict return None - + def get(self, request, *args, **kwargs): data = {} - zones = request.GET.getlist('zones[]', []); + zones = request.GET.getlist("zones[]", []) for zone in zones: zone_conf = settings.ADS_ZONES.get(zone, {}) ad = self.get_ad_context_dict(zone) + + if ad: + # Extract ad ID and update impressions each time ad is displayed + ads = Ad.objects.get(id=ad["url"][5:-1]) + update_impressions(ads, request) + if zone_conf: - data.update({ - zone: { - 'ad': ad, - 'conf': zone_conf - } - }) + data.update({zone: {"ad": ad, "conf": zone_conf}}) context_dict = { - 'google_adsense_client': settings.ADS_GOOGLE_ADSENSE_CLIENT, - 'viewports': settings.ADS_VIEWPORTS, - 'zones': data + "google_adsense_client": settings.ADS_GOOGLE_ADSENSE_CLIENT, + "viewports": settings.ADS_VIEWPORTS, + "zones": data, } return self.render_json_response(context_dict) - + """ context_dict = { 'zone': self.kwargs.get('zone', None) @@ -74,8 +69,8 @@ def get(self, request, *args, **kwargs): return self.render_json_response(context_dict) """ -class AdClickView(SingleObjectMixin, View): +class AdClickView(SingleObjectMixin, View): def get_queryset(self): return Ad.objects.all()