Skip to content

Commit 0fe2246

Browse files
committed
Addons: add addons.configs.load_when_embedded in the API response
Add an extra addons config to decide whether or not force the injection of addons when the page is embedded (eg. iframe). By default, we are not loading addons if embedded. Required by readthedocs/addons#415 Closes readthedocs/addons#412
1 parent 404d82a commit 0fe2246

File tree

5 files changed

+61
-5
lines changed

5 files changed

+61
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Generated by Django 4.2.16 on 2024-11-13 12:00
2+
3+
from django.db import migrations, models
4+
from django_safemigrate import Safe
5+
6+
7+
class Migration(migrations.Migration):
8+
safe = Safe.before_deploy
9+
10+
dependencies = [
11+
('projects', '0132_addons_linkpreviews_fields'),
12+
]
13+
14+
operations = [
15+
migrations.AddField(
16+
model_name='addonsconfig',
17+
name='load_when_embedded',
18+
field=models.BooleanField(default=False, null=True),
19+
),
20+
migrations.AddField(
21+
model_name='historicaladdonsconfig',
22+
name='load_when_embedded',
23+
field=models.BooleanField(default=False, null=True),
24+
),
25+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Generated by Django 4.2.16 on 2024-11-13 12:01
2+
3+
from django.db import migrations, models
4+
from django_safemigrate import Safe
5+
6+
7+
class Migration(migrations.Migration):
8+
safe = Safe.after_deploy
9+
10+
dependencies = [
11+
('projects', '0133_addons_load_when_embedded'),
12+
]
13+
14+
operations = [
15+
migrations.AlterField(
16+
model_name='addonsconfig',
17+
name='load_when_embedded',
18+
field=models.BooleanField(default=False),
19+
),
20+
migrations.AlterField(
21+
model_name='historicaladdonsconfig',
22+
name='load_when_embedded',
23+
field=models.BooleanField(default=False),
24+
),
25+
]

readthedocs/projects/models.py

+4
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ class AddonsConfig(TimeStampedModel):
167167
help_text="Enable/Disable all the addons on this project",
168168
)
169169

170+
# Whether or not load addons library when the requested page is embedded (e.g. inside an iframe)
171+
# https://github.com/readthedocs/addons/pull/415
172+
load_when_embedded = models.BooleanField(default=False)
173+
170174
# Analytics
171175

172176
# NOTE: we keep analytics disabled by default to save resources.

readthedocs/proxito/middleware.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
unresolver,
3131
)
3232
from readthedocs.core.utils import get_cache_tag
33-
from readthedocs.projects.models import Project
33+
from readthedocs.projects.models import AddonsConfig
3434
from readthedocs.proxito.cache import add_cache_tags, cache_response, private_response
3535
from readthedocs.proxito.redirects import redirect_to_https
3636

@@ -283,12 +283,11 @@ def add_hosting_integrations_headers(self, request, response):
283283
project_slug = getattr(request, "path_project_slug", "")
284284

285285
if project_slug:
286-
addons = Project.objects.filter(
287-
slug=project_slug, addons__enabled=True
288-
).exists()
286+
addons = AddonsConfig.objects.filter(project__slug=project_slug).first()
289287

290288
if addons:
291-
response["X-RTD-Force-Addons"] = "true"
289+
if addons.enabled:
290+
response["X-RTD-Force-Addons"] = "true"
292291

293292
def add_cors_headers(self, request, response):
294293
"""

readthedocs/proxito/views/hosting.py

+3
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,9 @@ def _v1(self, project, version, build, filename, url, request):
454454
# Mainly, all the fields including a Project, Version or Build will use the exact same
455455
# serializer than the keys ``project``, ``version`` and ``build`` from the top level.
456456
"addons": {
457+
"configs": {
458+
"load_when_embedded": project.addons.load_when_embedded,
459+
},
457460
"analytics": {
458461
"enabled": project.addons.analytics_enabled,
459462
# TODO: consider adding this field into the ProjectSerializer itself.

0 commit comments

Comments
 (0)