Skip to content

Addons: allow users to opt-in into the beta addons #10733

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

Merged
merged 7 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions readthedocs/projects/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
EnvironmentVariable,
Feature,
Project,
ProjectAddonsConfig,
ProjectRelationship,
WebHook,
)
Expand Down Expand Up @@ -493,6 +494,35 @@ def clean_alias(self):
return alias


class ProjectAddonsForm(forms.ModelForm):

"""Form to opt-in into new beta addons."""

enabled = forms.BooleanField(
label="Enable Read the Docs beta addons",
help_text="Opt-in into new beta addons",
required=False,
)

class Meta:
model = Project
fields = ()

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
enabled = bool(self.instance.addons)
self.fields["enabled"].initial = enabled

def save(self, commit=True):
instance = super().save(commit)
if self.cleaned_data.get("enabled", False):
self.instance.addons = ProjectAddonsConfig.objects.create()
self.instance.save()
elif self.instance.addons:
self.instance.addons.delete()
return instance


class UserForm(forms.Form):

"""Project owners form."""
Expand Down
120 changes: 120 additions & 0 deletions readthedocs/projects/migrations/0106_add_addons_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# Generated by Django 4.2.5 on 2023-09-13 14:21

import django.db.models.deletion
import django_extensions.db.fields
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("projects", "0105_remove_project_urlconf"),
]

operations = [
migrations.CreateModel(
name="ProjectAddonsConfig",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"created",
django_extensions.db.fields.CreationDateTimeField(
auto_now_add=True, verbose_name="created"
),
),
(
"modified",
django_extensions.db.fields.ModificationDateTimeField(
auto_now=True, verbose_name="modified"
),
),
("analytics_enabled", models.BooleanField(default=True)),
("doc_diff_enabled", models.BooleanField(default=True)),
("doc_diff_show_additions", models.BooleanField(default=True)),
("doc_diff_show_deletions", models.BooleanField(default=True)),
("doc_diff_root_selector", models.CharField(blank=True, null=True)),
("external_version_warning", models.BooleanField(default=True)),
("ethicalads_enabled", models.BooleanField(default=True)),
("flyout_enabled", models.BooleanField(default=True)),
("hotkeys_enabled", models.BooleanField(default=True)),
("search_enabled", models.BooleanField(default=True)),
("search_default_filter", models.CharField(blank=True, null=True)),
("stable_latest_version_warning", models.BooleanField(default=True)),
],
options={
"get_latest_by": "modified",
"abstract": False,
},
),
migrations.CreateModel(
name="AddonSearchFilter",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"created",
django_extensions.db.fields.CreationDateTimeField(
auto_now_add=True, verbose_name="created"
),
),
(
"modified",
django_extensions.db.fields.ModificationDateTimeField(
auto_now=True, verbose_name="modified"
),
),
("name", models.CharField(max_length=128)),
("syntaxt", models.CharField(max_length=128)),
(
"addons",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="projects.projectaddonsconfig",
),
),
],
options={
"get_latest_by": "modified",
"abstract": False,
},
),
migrations.AddField(
model_name="historicalproject",
name="addons",
field=models.ForeignKey(
blank=True,
db_constraint=False,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="+",
to="projects.projectaddonsconfig",
verbose_name="Addons project configuration",
),
),
migrations.AddField(
model_name="project",
name="addons",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="project",
to="projects.projectaddonsconfig",
verbose_name="Addons project configuration",
),
),
]
66 changes: 66 additions & 0 deletions readthedocs/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,60 @@ def subproject_prefix(self):
return unsafe_join_url_path(prefix, self.alias, "/")


class ProjectAddonsConfig(TimeStampedModel):

"""
Addons project configuration.

Store all the configuration for each of the addons.
Everything is enabled by default.
"""

DOC_DIFF_DEFAULT_ROOT_SELECTOR = "[role=main]"

# Analytics
analytics_enabled = models.BooleanField(default=True)

# Docdiff
doc_diff_enabled = models.BooleanField(default=True)
doc_diff_root_selector = models.CharField(null=True, blank=True)
doc_diff_show_additions = models.BooleanField(default=True)
doc_diff_show_deletions = models.BooleanField(default=True)
doc_diff_root_selector = models.CharField(null=True, blank=True)

# External version warning
external_version_warning = models.BooleanField(default=True)

# EthicalAds
ethicalads_enabled = models.BooleanField(default=True)

# Flyout
flyout_enabled = models.BooleanField(default=True)

# Hotkeys
hotkeys_enabled = models.BooleanField(default=True)

# Search
search_enabled = models.BooleanField(default=True)
search_default_filter = models.CharField(null=True, blank=True)

# Stable/Latest version warning
stable_latest_version_warning = models.BooleanField(default=True)


class AddonSearchFilter(TimeStampedModel):

"""
Addon search user defined filter.

Specific filter defined by the user to show on the search modal.
Copy link
Member

Choose a reason for hiding this comment

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

An example or use case here would probably be useful. Is this to allow users to default to subprojects enabled or similar?

Copy link
Member Author

@humitos humitos Sep 14, 2023

Choose a reason for hiding this comment

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

The idea behind this is to allow users to create filters to show in the search modal, like these ones:

Screenshot_2023-09-14_10-26-27

Copy link
Member Author

Choose a reason for hiding this comment

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

We can skip it for now if we are not sure how we are going to use this, tho.

Copy link
Member

Choose a reason for hiding this comment

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

I don't feel too strongly, just wanted to make sure we had a plan for this. I'm fine to include it if we can make the docstring obvious for the user, but perhaps it's better for the next version? 🤷

Copy link
Member Author

Choose a reason for hiding this comment

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

Making these filters configurable has been the idea from the beginning of the re-work for the search as you type. However, they are not yet configurable by the user and they won't be in this iteration because it will require more UI.

For now, we are only allowing them to opt-in into all the addons at once. In the following iteration we will be exposing specific configuration for each addon and they will be able to create these filters. This PR only creates the models we will use in the future.

"""

addons = models.ForeignKey("ProjectAddonsConfig", on_delete=models.CASCADE)
name = models.CharField(max_length=128)
syntaxt = models.CharField(max_length=128)


class Project(models.Model):

"""Project model."""
Expand All @@ -131,6 +185,18 @@ class Project(models.Model):
pub_date = models.DateTimeField(_('Publication date'), auto_now_add=True, db_index=True)
modified_date = models.DateTimeField(_('Modified date'), auto_now=True, db_index=True)

# NOTE: we are defaulting this field to ``NULL``, meaning the "new beta addons" are disabled.
# When the user enables the beta addons, we create a ``AddonsProjectConfig`` and attach it
# to this ``Project`` to override the "old integration" at serve time.
addons = models.ForeignKey(
"ProjectAddonsConfig",
verbose_name=_("Addons project configuration"),
related_name="project",
null=True,
blank=True,
on_delete=models.SET_NULL,
)

# Generally from conf.py
users = models.ManyToManyField(
User,
Expand Down
11 changes: 11 additions & 0 deletions readthedocs/projects/urls/private.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
IntegrationExchangeDetail,
IntegrationList,
IntegrationWebhookSync,
ProjectAddonsUpdate,
ProjectAdvancedUpdate,
ProjectAdvertisingUpdate,
ProjectDashboard,
Expand Down Expand Up @@ -204,6 +205,16 @@

urlpatterns += domain_urls

addons_urls = [
re_path(
r"^(?P<project_slug>[-\w]+)/addons/edit/$$",
ProjectAddonsUpdate.as_view(),
name="projects_addons",
),
]

urlpatterns += addons_urls

integration_urls = [
re_path(
r"^(?P<project_slug>{project_slug})/integrations/$".format(**pattern_opts),
Expand Down
10 changes: 10 additions & 0 deletions readthedocs/projects/views/private.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
EmailHookForm,
EnvironmentVariableForm,
IntegrationForm,
ProjectAddonsForm,
ProjectAdvancedForm,
ProjectAdvertisingForm,
ProjectBasicsForm,
Expand Down Expand Up @@ -168,6 +169,15 @@ def get_success_url(self):
return reverse('projects_detail', args=[self.object.slug])


class ProjectAddonsUpdate(ProjectMixin, UpdateView):
form_class = ProjectAddonsForm
success_message = _("Project addons updated")
template_name = "projects/addons_form.html"

def get_success_url(self):
return reverse("projects_addons", args=[self.object.slug])


class ProjectDelete(UpdateChangeReasonPostView, ProjectMixin, DeleteView):

success_message = _('Project deleted')
Expand Down
19 changes: 15 additions & 4 deletions readthedocs/proxito/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
unresolver,
)
from readthedocs.core.utils import get_cache_tag
from readthedocs.projects.models import Project
from readthedocs.proxito.cache import add_cache_tags, cache_response, private_response
from readthedocs.proxito.redirects import redirect_to_https

Expand Down Expand Up @@ -269,12 +270,22 @@ def add_hosting_integrations_headers(self, request, response):
project_slug = getattr(request, "path_project_slug", "")
version_slug = getattr(request, "path_version_slug", "")

if project_slug and version_slug:
addons = Version.objects.filter(
if project_slug:
force_addons = Project.objects.filter(
project__slug=project_slug,
slug=version_slug,
addons=True,
addons__isnull=False,
).exists()
if force_addons:
response["X-RTD-Force-Addons"] = "true"
return

if version_slug:
addons = Version.objects.filter(
project__slug=project_slug,
slug=version_slug,
addons=True,
).exists()

if addons:
response["X-RTD-Hosting-Integrations"] = "true"

Expand Down
1 change: 1 addition & 0 deletions readthedocs/proxito/views/hosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ def _v0(self, project, version, build, filename, user):
project.translations.all().only("language").order_by("language")
)
# Make one DB query here and then check on Python code
# TODO: make usage of ``Project.addons.<name>_enabled`` to decide if enabled
project_features = project.features.all().values_list("feature_id", flat=True)

data = {
Expand Down