From 72da54369e739db5c435d622e6196d9ba3f022cf Mon Sep 17 00:00:00 2001 From: Manuel Kaufmann Date: Wed, 12 Jul 2023 16:03:36 +0200 Subject: [PATCH 1/4] Addons: return `ethicalads` data on `/_/addons/` endpoint This data is consumed by the sponsorship addons to display an ad using EthicalAds. Related https://github.com/readthedocs/addons/issues/67 --- readthedocs/proxito/views/hosting.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/readthedocs/proxito/views/hosting.py b/readthedocs/proxito/views/hosting.py index 29c00e0e3a8..4da3af4783c 100644 --- a/readthedocs/proxito/views/hosting.py +++ b/readthedocs/proxito/views/hosting.py @@ -3,6 +3,7 @@ import packaging import structlog from django.conf import settings +from django.contrib.auth.models import AnonymousUser from django.http import Http404, JsonResponse from django.views import View @@ -296,6 +297,29 @@ def _v0(self, project, version, build, filename): if version and version.build_data: data.update(version.build_data) + # Update this data with ethicalads + if "readthedocsext.donate" in settings.INSTALLED_APPS: + from readthedocsext.donate.utils import ( + get_campaign_types, + get_project_keywords, + get_publisher, + is_ad_free_project, + is_ad_free_user, + ) + + data["addons"].update( + { + "ethicalads": { + # NOTE: this endpoint is not authenticated, the user checks are done over an annonymous user for now + "ad_free": is_ad_free_user(AnonymousUser()) + or is_ad_free_project(project), + "campaign_types": get_campaign_types(AnonymousUser(), project), + "keywords": get_project_keywords(project), + "publisher": get_publisher(project), + }, + } + ) + return data def _v1(self, project, version, build, filename): From 451e8a043fa6f147b7ac032982282a39708762de Mon Sep 17 00:00:00 2001 From: Manuel Kaufmann Date: Wed, 12 Jul 2023 16:26:16 +0200 Subject: [PATCH 2/4] Addons: return whether or not EthicalAd is enabled --- readthedocs/proxito/views/hosting.py | 1 + 1 file changed, 1 insertion(+) diff --git a/readthedocs/proxito/views/hosting.py b/readthedocs/proxito/views/hosting.py index 4da3af4783c..9a303f2efb3 100644 --- a/readthedocs/proxito/views/hosting.py +++ b/readthedocs/proxito/views/hosting.py @@ -310,6 +310,7 @@ def _v0(self, project, version, build, filename): data["addons"].update( { "ethicalads": { + "enabled": True, # NOTE: this endpoint is not authenticated, the user checks are done over an annonymous user for now "ad_free": is_ad_free_user(AnonymousUser()) or is_ad_free_project(project), From 996d07bc7b24df2c0a00973d2e9753692a3b7096 Mon Sep 17 00:00:00 2001 From: Manuel Kaufmann Date: Wed, 12 Jul 2023 17:34:53 +0200 Subject: [PATCH 3/4] Lint: avoid `prospector` failing because missing import `prospector` doesn't have access to this repository because it's private. So, we are skipping the check for this line. --- readthedocs/proxito/views/hosting.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readthedocs/proxito/views/hosting.py b/readthedocs/proxito/views/hosting.py index 9a303f2efb3..982c78edd9d 100644 --- a/readthedocs/proxito/views/hosting.py +++ b/readthedocs/proxito/views/hosting.py @@ -299,7 +299,7 @@ def _v0(self, project, version, build, filename): # Update this data with ethicalads if "readthedocsext.donate" in settings.INSTALLED_APPS: - from readthedocsext.donate.utils import ( + from readthedocsext.donate.utils import ( # noqa get_campaign_types, get_project_keywords, get_publisher, From a68216ffbe2098523e36fcbc2781eeb095421883 Mon Sep 17 00:00:00 2001 From: Manuel Kaufmann Date: Wed, 12 Jul 2023 17:41:34 +0200 Subject: [PATCH 4/4] Sponsorship: comment explaining the required setting --- readthedocs/proxito/views/hosting.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/readthedocs/proxito/views/hosting.py b/readthedocs/proxito/views/hosting.py index 982c78edd9d..a9fdc622a50 100644 --- a/readthedocs/proxito/views/hosting.py +++ b/readthedocs/proxito/views/hosting.py @@ -312,6 +312,8 @@ def _v0(self, project, version, build, filename): "ethicalads": { "enabled": True, # NOTE: this endpoint is not authenticated, the user checks are done over an annonymous user for now + # + # NOTE: it requires ``settings.USE_PROMOS=True`` to return ``ad_free=false`` here "ad_free": is_ad_free_user(AnonymousUser()) or is_ad_free_project(project), "campaign_types": get_campaign_types(AnonymousUser(), project),