Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adddons: allow injecting an "always live" JavaScript file #11758

Open
wants to merge 8 commits into
base: humitos/addons-embedded-http-header
Choose a base branch
from
35 changes: 35 additions & 0 deletions readthedocs/projects/migrations/0135_addons_customscript.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 4.2.16 on 2024-11-13 13:34

from django.db import migrations, models
from django_safemigrate import Safe


class Migration(migrations.Migration):
safe = Safe.before_deploy

dependencies = [
('projects', '0134_addons_load_when_embedded_notnull'),
]

operations = [
migrations.AddField(
model_name='addonsconfig',
name='customscript_enabled',
field=models.BooleanField(default=False, null=True),
),
migrations.AddField(
model_name='addonsconfig',
name='customscript_src',
field=models.CharField(blank=True, help_text='URL to a JavaScript file to inject at serve time', max_length=512, null=True),
),
migrations.AddField(
model_name='historicaladdonsconfig',
name='customscript_enabled',
field=models.BooleanField(default=False, null=True),
),
migrations.AddField(
model_name='historicaladdonsconfig',
name='customscript_src',
field=models.CharField(blank=True, help_text='URL to a JavaScript file to inject at serve time', max_length=512, null=True),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 4.2.16 on 2024-11-13 13:36

from django.db import migrations, models
from django_safemigrate import Safe


class Migration(migrations.Migration):
safe = Safe.after_deploy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice making them nullable 💯

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

customscript_enabled is not nullable, so this will still break when creating a new addons object.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous migration makes it nullable during deploy, and then marks it not nullable after, which I thought would work?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I didn't see there were two migrations. I guess Django will ask for a default value when running this migration (for the objects that were created while the field was null).


dependencies = [
('projects', '0135_addons_customscript'),
]

operations = [
migrations.AlterField(
model_name='addonsconfig',
name='customscript_enabled',
field=models.BooleanField(default=False),
),
migrations.AlterField(
model_name='historicaladdonsconfig',
name='customscript_enabled',
field=models.BooleanField(default=False),
),
]
12 changes: 12 additions & 0 deletions readthedocs/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,18 @@ class AddonsConfig(TimeStampedModel):
search_enabled = models.BooleanField(default=True)
search_default_filter = models.CharField(null=True, blank=True, max_length=128)

# User JavaScript File
customscript_enabled = models.BooleanField(default=False)

# This is a user-defined file that will be injected at serve time by our
# Cloudflare Worker if defined
customscript_src = models.CharField(
max_length=512,
null=True,
blank=True,
help_text="URL to a JavaScript file to inject at serve time",
)

# Notifications
notifications_enabled = models.BooleanField(default=True)
notifications_show_on_latest = models.BooleanField(default=True)
Expand Down
4 changes: 4 additions & 0 deletions readthedocs/proxito/tests/responses/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@
"default_filter": "project:project/latest",
"filters": []
},
"customscript": {
"enabled": false,
"src": null
},
"linkpreviews": {
"enabled": false,
"root_selector": "[role=main] a.internal",
Expand Down
4 changes: 4 additions & 0 deletions readthedocs/proxito/views/hosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,10 @@ def _v1(self, project, version, build, filename, url, request):
# "filepath": "/docs/index.rst",
# },
},
"customscript": {
"enabled": project.addons.customscript_enabled,
"src": project.addons.customscript_src,
},
"search": {
"enabled": project.addons.search_enabled,
# TODO: figure it out where this data comes from.
Expand Down