Skip to content

Commit c3a0268

Browse files
authored
Addons: prepare Proxito and dashboard to enable them by default (#11513)
* Addons: prepare Proxito and dashboard to enable them by default Prepare Proxito's code and dashboard to enable addons by default starting on October 7th, as planned: https://about.readthedocs.com/blog/2024/07/addons-by-default/ Note this code is temporary and can be deployed _before_ reaching that particular date. The idea is to remove this code once this behavior becomes the default. Related #11474 * Update tests * Revert "Update tests" This reverts commit 235514a. * Allow "for Business" to disable addons completely Keep the ability to let commercial users to disable Addons completely. This means that our Cloudflare working won't even inject them. * Create `AddonsConfig` on project import/add * Typo
1 parent 971b11c commit c3a0268

File tree

3 files changed

+65
-17
lines changed

3 files changed

+65
-17
lines changed

readthedocs/projects/forms.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
"""Project forms."""
22

3+
import datetime
34
import json
45
from random import choice
56
from re import fullmatch
67
from urllib.parse import urlparse
78

9+
import pytz
810
from allauth.socialaccount.models import SocialAccount
911
from django import forms
1012
from django.conf import settings
1113
from django.contrib.auth.models import User
1214
from django.db.models import Q
1315
from django.urls import reverse
16+
from django.utils import timezone
1417
from django.utils.translation import gettext_lazy as _
1518

1619
from readthedocs.builds.constants import INTERNAL
@@ -677,14 +680,24 @@ class Meta:
677680

678681
def __init__(self, *args, **kwargs):
679682
self.project = kwargs.pop("project", None)
683+
684+
tzinfo = pytz.timezone("America/Los_Angeles")
685+
addons_enabled_by_default = timezone.now() > datetime.datetime(
686+
2024, 10, 7, 0, 0, 0, tzinfo=tzinfo
687+
)
688+
680689
addons, created = AddonsConfig.objects.get_or_create(project=self.project)
681690
if created:
682-
addons.enabled = False
691+
addons.enabled = addons_enabled_by_default
683692
addons.save()
684693

685694
kwargs["instance"] = addons
686695
super().__init__(*args, **kwargs)
687696

697+
# Keep the ability to disable addons completely on Read the Docs for Business
698+
if not settings.RTD_ALLOW_ORGANIZATIONS and addons_enabled_by_default:
699+
self.fields.pop("enabled")
700+
688701
def clean(self):
689702
if (
690703
self.cleaned_data["flyout_sorting"] == ADDONS_FLYOUT_SORTING_CUSTOM_PATTERN

readthedocs/projects/views/private.py

+4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
WebHookForm,
7575
)
7676
from readthedocs.projects.models import (
77+
AddonsConfig,
7778
Domain,
7879
EmailHook,
7980
EnvironmentVariable,
@@ -423,6 +424,9 @@ def done(self, form_list, **kwargs):
423424
# attributes directly from other forms
424425
project = basics_form.save()
425426

427+
# Create an AddonsConfig object for this project.
428+
AddonsConfig.objects.get_or_create(project=project)
429+
426430
self.finish_import_project(self.request, project)
427431

428432
return HttpResponseRedirect(

readthedocs/proxito/middleware.py

+47-16
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
66
Additional processing is done to get the project from the URL in the ``views.py`` as well.
77
"""
8+
import datetime
89
import re
910
from urllib.parse import urlparse
1011

12+
import pytz
1113
import structlog
1214
from corsheaders.middleware import (
1315
ACCESS_CONTROL_ALLOW_METHODS,
@@ -17,6 +19,7 @@
1719
from django.core.exceptions import SuspiciousOperation
1820
from django.shortcuts import redirect
1921
from django.urls import reverse
22+
from django.utils import timezone
2023
from django.utils.deprecation import MiddlewareMixin
2124
from django.utils.encoding import iri_to_uri
2225
from django.utils.html import escape
@@ -31,7 +34,7 @@
3134
unresolver,
3235
)
3336
from readthedocs.core.utils import get_cache_tag
34-
from readthedocs.projects.models import Project
37+
from readthedocs.projects.models import Feature, Project
3538
from readthedocs.proxito.cache import add_cache_tags, cache_response, private_response
3639
from readthedocs.proxito.redirects import redirect_to_https
3740

@@ -289,23 +292,51 @@ def add_hosting_integrations_headers(self, request, response):
289292
version_slug = getattr(request, "path_version_slug", "")
290293

291294
if project_slug:
292-
force_addons = Project.objects.filter(
293-
slug=project_slug,
294-
addons__enabled=True,
295-
).exists()
296-
if force_addons:
297-
response["X-RTD-Force-Addons"] = "true"
298-
return
299-
300-
if version_slug:
301-
addons = Version.objects.filter(
302-
project__slug=project_slug,
303-
slug=version_slug,
304-
addons=True,
295+
tzinfo = pytz.timezone("America/Los_Angeles")
296+
addons_enabled_by_default = timezone.now() > datetime.datetime(
297+
2024,
298+
10,
299+
7,
300+
0,
301+
0,
302+
0,
303+
tzinfo=tzinfo,
304+
)
305+
if addons_enabled_by_default:
306+
addons = Project.objects.filter(
307+
slug=project_slug, addons__enabled=True
308+
).exists()
309+
310+
if addons:
311+
response["X-RTD-Force-Addons"] = "true"
312+
return
313+
314+
else:
315+
# TODO: remove "else" code once DISABLE_SPHINX_MANIPULATION and addons becomes the default
316+
# https://about.readthedocs.com/blog/2024/07/addons-by-default/
317+
disable_sphinx_manipulation_enabled = Feature.objects.filter(
318+
feature_id=Feature.DISABLE_SPHINX_MANIPULATION,
319+
projects__slug=Project.objects.filter(slug=project_slug).first(),
305320
).exists()
306321

307-
if addons:
308-
response["X-RTD-Hosting-Integrations"] = "true"
322+
force_addons = Project.objects.filter(
323+
slug=project_slug,
324+
addons__enabled=True,
325+
).exists()
326+
327+
if force_addons or disable_sphinx_manipulation_enabled:
328+
response["X-RTD-Force-Addons"] = "true"
329+
return
330+
331+
if version_slug:
332+
addons = Version.objects.filter(
333+
project__slug=project_slug,
334+
slug=version_slug,
335+
addons=True,
336+
).exists()
337+
338+
if addons:
339+
response["X-RTD-Hosting-Integrations"] = "true"
309340

310341
def add_cors_headers(self, request, response):
311342
"""

0 commit comments

Comments
 (0)