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 2 commits
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
25 changes: 25 additions & 0 deletions readthedocs/projects/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from readthedocs.invitations.models import Invitation
from readthedocs.oauth.models import RemoteRepository
from readthedocs.projects.models import (
AddonsConfig,
Domain,
EmailHook,
EnvironmentVariable,
Expand Down Expand Up @@ -493,6 +494,30 @@ def clean_alias(self):
return alias


class AddonsConfigForm(forms.ModelForm):

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

project = forms.CharField(widget=forms.HiddenInput(), required=False)

class Meta:
model = AddonsConfig
fields = ("enabled", "project")

def __init__(self, *args, **kwargs):
self.project = kwargs.pop("project", None)
kwargs["instance"] = getattr(self.project, "addons", None)
super().__init__(*args, **kwargs)

try:
self.fields["enabled"].initial = self.project.addons.enabled
except AddonsConfig.DoesNotExist:
self.fields["enabled"].initial = False

def clean_project(self):
return self.project


class UserForm(forms.Form):

"""Project owners form."""
Expand Down
115 changes: 115 additions & 0 deletions readthedocs/projects/migrations/0106_add_addons_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# Generated by Django 4.2.5 on 2023-09-14 09: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="AddonsConfig",
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"
),
),
(
"enabled",
models.BooleanField(
default=True,
help_text="Enable/Disable all the addons on this project",
),
),
("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_enabled", 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_enabled",
models.BooleanField(default=True),
),
(
"project",
models.OneToOneField(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="addons",
to="projects.project",
),
),
],
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.addonsconfig",
),
),
],
options={
"get_latest_by": "modified",
"abstract": False,
},
),
]
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,72 @@ def subproject_prefix(self):
return unsafe_join_url_path(prefix, self.alias, "/")


class AddonsConfig(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]"

project = models.OneToOneField(
"Project",
related_name="addons",
null=True,
blank=True,
on_delete=models.CASCADE,
)

enabled = models.BooleanField(
default=True,
help_text="Enable/Disable all the addons on this project",
)

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

# Docdiff
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(null=True, blank=True)

# External version warning
external_version_warning_enabled = 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_enabled = 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("AddonsConfig", on_delete=models.CASCADE)
name = models.CharField(max_length=128)
syntaxt = models.CharField(max_length=128)


class Project(models.Model):

"""Project model."""
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 @@ -9,6 +9,7 @@
from readthedocs.projects.backends.views import ImportWizardView
from readthedocs.projects.views import private
from readthedocs.projects.views.private import (
AddonsConfigUpdate,
AutomationRuleDelete,
AutomationRuleList,
AutomationRuleMove,
Expand Down Expand Up @@ -204,6 +205,16 @@

urlpatterns += domain_urls

addons_urls = [
re_path(
r"^(?P<project_slug>[-\w]+)/addons/edit/$$",
AddonsConfigUpdate.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 @@ -46,6 +46,7 @@
from readthedocs.oauth.utils import update_webhook
from readthedocs.projects.filters import ProjectListFilterSet
from readthedocs.projects.forms import (
AddonsConfigForm,
DomainForm,
EmailHookForm,
EnvironmentVariableForm,
Expand Down Expand Up @@ -168,6 +169,15 @@ def get_success_url(self):
return reverse('projects_detail', args=[self.object.slug])


class AddonsConfigUpdate(ProjectAdminMixin, PrivateViewMixin, CreateView, UpdateView):
form_class = AddonsConfigForm
success_message = _("Project addons updated")
template_name = "projects/addons_form.html"

def get_success_url(self):
return reverse("projects_addons", args=[self.object.project.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