Skip to content

Unify feature check for organization/project #8920

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 24 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
13 changes: 7 additions & 6 deletions readthedocs/builds/tests/test_build_queryset.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import pytest

import django_dynamic_fixture as fixture
from django.conf import settings
import pytest

from readthedocs.builds.querysets import BuildQuerySet
from readthedocs.builds.models import Build, Version
from readthedocs.builds.models import Build
from readthedocs.organizations.models import Organization
from readthedocs.projects.models import Project, Feature
from readthedocs.projects.models import Project


@pytest.mark.django_db
class TestBuildQuerySet:

@pytest.fixture(autouse=True)
def setup_method(self, settings):
settings.RTD_ALL_FEATURES_ENABLED = True

def test_concurrent_builds(self):
project = fixture.get(
Project,
Expand Down
5 changes: 4 additions & 1 deletion readthedocs/organizations/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ def test_add_owner(self):
self.assertTrue(user_b in self.organization.owners.all())


@override_settings(RTD_ALLOW_ORGANIZATIONS=True)
@override_settings(
RTD_ALLOW_ORGANIZATIONS=True,
RTD_ALL_FEATURES_ENABLED=True,
)
class OrganizationSecurityLogTests(TestCase):

def setUp(self):
Expand Down
27 changes: 15 additions & 12 deletions readthedocs/organizations/views/private.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from readthedocs.audit.models import AuditLog
from readthedocs.core.history import UpdateChangeReasonPostView
from readthedocs.core.mixins import PrivateViewMixin
from readthedocs.core.utils.extend import SettingsOverrideObject
from readthedocs.organizations.forms import (
OrganizationSignupForm,
OrganizationTeamProjectForm,
Expand All @@ -26,6 +25,7 @@
OrganizationView,
)
from readthedocs.projects.utils import get_csv_file
from readthedocs.subscriptions.models import PlanFeature


# Organization views
Expand Down Expand Up @@ -170,12 +170,13 @@ def post(self, request, *args, **kwargs):
return resp


class OrganizationSecurityLogBase(PrivateViewMixin, OrganizationMixin, ListView):
class OrganizationSecurityLog(PrivateViewMixin, OrganizationMixin, ListView):

"""Display security logs related to this organization."""

model = AuditLog
template_name = 'organizations/security_log.html'
feature_type = PlanFeature.TYPE_AUDIT_LOGS

def get(self, request, *args, **kwargs):
download_data = request.GET.get('download', False)
Expand Down Expand Up @@ -273,14 +274,16 @@ def get_queryset(self):
)
return self.filter.qs

def _get_retention_days_limit(self, organization): # noqa
"""From how many days we need to show data for this project?"""
return settings.RTD_AUDITLOGS_DEFAULT_RETENTION_DAYS

def _is_enabled(self, organization): # noqa
"""Should we show audit logs for this organization?"""
return True

def _get_retention_days_limit(self, organization):
"""From how many days we need to show data for this organization?"""
return PlanFeature.objects.get_feature_value(
organization,
type=self.feature_type,
default=settings.RTD_AUDITLOGS_DEFAULT_RETENTION_DAYS,
)

class OrganizationSecurityLog(SettingsOverrideObject):
_default_class = OrganizationSecurityLogBase
def _is_enabled(self, organization):
return PlanFeature.objects.has_feature(
organization,
type=self.feature_type,
)
9 changes: 8 additions & 1 deletion readthedocs/projects/querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def max_concurrent_builds(self, project):

- project
- organization
- plan
- default setting

:param project: project to be checked
Expand All @@ -105,6 +106,8 @@ def max_concurrent_builds(self, project):
:returns: number of max concurrent builds for the project
:rtype: int
"""
from readthedocs.subscriptions.models import PlanFeature

max_concurrent_organization = None
organization = project.organizations.first()
if organization:
Expand All @@ -113,7 +116,11 @@ def max_concurrent_builds(self, project):
return (
project.max_concurrent_builds or
max_concurrent_organization or
settings.RTD_MAX_CONCURRENT_BUILDS
PlanFeature.objects.get_feature_value(
project,
type=PlanFeature.TYPE_CONCURRENT_BUILDS,
default=settings.RTD_MAX_CONCURRENT_BUILDS,
)
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 had an override just to check for the value from the plan on .com.

Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this method contain all the logic? I mean, also check for project.max_concurrent_builds or max_concurrent_organization as well?

Otherwise, it's only being called for the default value, but it won't affect the project's value.

Copy link
Member

Choose a reason for hiding this comment

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

It seems my suggestion is already applied, right?

)

def prefetch_latest_build(self):
Expand Down
68 changes: 32 additions & 36 deletions readthedocs/projects/views/private.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Project views for authenticated users."""

import structlog

from allauth.socialaccount.models import SocialAccount
from django.conf import settings
from django.contrib import messages
Expand Down Expand Up @@ -40,7 +39,6 @@
)
from readthedocs.core.history import UpdateChangeReasonPostView
from readthedocs.core.mixins import ListViewWithForm, PrivateViewMixin
from readthedocs.core.utils.extend import SettingsOverrideObject
from readthedocs.integrations.models import HttpExchange, Integration
from readthedocs.oauth.services import registry
from readthedocs.oauth.tasks import attach_webhook
Expand Down Expand Up @@ -79,6 +77,8 @@
ProjectRelationListMixin,
)
from readthedocs.search.models import SearchQuery
from readthedocs.subscriptions.models import PlanFeature


log = structlog.get_logger(__name__)

Expand Down Expand Up @@ -747,6 +747,7 @@ class DomainMixin(ProjectAdminMixin, PrivateViewMixin):
model = Domain
form_class = DomainForm
lookup_url_kwarg = 'domain_pk'
feature_type = PlanFeature.TYPE_CNAME

def get_success_url(self):
return reverse('projects_domains', args=[self.get_project().slug])
Expand All @@ -758,11 +759,13 @@ def get_context_data(self, **kwargs):
return context

def _is_enabled(self, project):
"""Should we allow custom domains for this project?"""
return True
return PlanFeature.objects.has_feature(
project,
type=self.feature_type,
)


class DomainListBase(DomainMixin, ListViewWithForm):
class DomainList(DomainMixin, ListViewWithForm):

def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
Expand All @@ -777,12 +780,7 @@ def get_context_data(self, **kwargs):
return ctx


class DomainList(SettingsOverrideObject):

_default_class = DomainListBase


class DomainCreateBase(DomainMixin, CreateView):
class DomainCreate(DomainMixin, CreateView):

def post(self, request, *args, **kwargs):
project = self.get_project()
Expand All @@ -801,12 +799,7 @@ def get_success_url(self):
)


class DomainCreate(SettingsOverrideObject):

_default_class = DomainCreateBase


class DomainUpdateBase(DomainMixin, UpdateView):
class DomainUpdate(DomainMixin, UpdateView):

def post(self, request, *args, **kwargs):
project = self.get_project()
Expand All @@ -815,11 +808,6 @@ def post(self, request, *args, **kwargs):
return HttpResponse('Action not allowed', status=401)


class DomainUpdate(SettingsOverrideObject):

_default_class = DomainUpdateBase


class DomainDelete(DomainMixin, DeleteView):

pass
Expand Down Expand Up @@ -1062,10 +1050,11 @@ class RegexAutomationRuleUpdate(RegexAutomationRuleMixin, UpdateView):
pass


class SearchAnalyticsBase(ProjectAdminMixin, PrivateViewMixin, TemplateView):
class SearchAnalytics(ProjectAdminMixin, PrivateViewMixin, TemplateView):

template_name = 'projects/projects_search_analytics.html'
http_method_names = ['get']
feature_type = PlanFeature.TYPE_SEARCH_ANALYTICS

def get(self, request, *args, **kwargs):
download_data = request.GET.get('download', False)
Expand Down Expand Up @@ -1149,21 +1138,25 @@ def _get_csv_data(self):

def _get_retention_days_limit(self, project):
"""From how many days we need to show data for this project?"""
return settings.RTD_ANALYTICS_DEFAULT_RETENTION_DAYS
return PlanFeature.objects.get_feature_value(
project,
type=self.feature_type,
default=settings.RTD_ANALYTICS_DEFAULT_RETENTION_DAYS,
)

def _is_enabled(self, project):
"""Should we show search analytics for this project?"""
return True


class SearchAnalytics(SettingsOverrideObject):
_default_class = SearchAnalyticsBase
return PlanFeature.objects.has_feature(
project,
type=self.feature_type,
)
Comment on lines 1161 to +1166
Copy link
Member

Choose a reason for hiding this comment

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

This method should go in a mixin since it's shared among multiple classes.



class TrafficAnalyticsViewBase(ProjectAdminMixin, PrivateViewMixin, TemplateView):
class TrafficAnalyticsView(ProjectAdminMixin, PrivateViewMixin, TemplateView):

template_name = 'projects/project_traffic_analytics.html'
http_method_names = ['get']
feature_type = PlanFeature.TYPE_PAGEVIEW_ANALYTICS

def get(self, request, *args, **kwargs):
download_data = request.GET.get('download', False)
Expand Down Expand Up @@ -1239,12 +1232,15 @@ def _get_csv_data(self):

def _get_retention_days_limit(self, project):
"""From how many days we need to show data for this project?"""
return settings.RTD_ANALYTICS_DEFAULT_RETENTION_DAYS
return PlanFeature.objects.get_feature_value(
project,
type=self.feature_type,
default=settings.RTD_ANALYTICS_DEFAULT_RETENTION_DAYS,
)

def _is_enabled(self, project):
"""Should we show traffic analytics for this project?"""
return True


class TrafficAnalyticsView(SettingsOverrideObject):
_default_class = TrafficAnalyticsViewBase
return PlanFeature.objects.has_feature(
project,
type=self.feature_type,
)
17 changes: 15 additions & 2 deletions readthedocs/proxito/tests/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copied from .org

from readthedocs.projects.constants import SSL_STATUS_VALID
import django_dynamic_fixture as fixture
import pytest
from django.conf import settings
Expand Down Expand Up @@ -78,5 +79,17 @@ def setUp(self):
self.project.add_subproject(self.subproject_alias, alias='this-is-an-alias')

# These can be set to canonical as needed in specific tests
self.domain = fixture.get(Domain, project=self.project, domain='docs1.example.com', https=True)
self.domain2 = fixture.get(Domain, project=self.project, domain='docs2.example.com', https=True)
self.domain = fixture.get(
Domain,
project=self.project,
domain='docs1.example.com',
https=True,
ssl_status=SSL_STATUS_VALID,
)
self.domain2 = fixture.get(
Domain,
project=self.project,
domain='docs2.example.com',
https=True,
ssl_status=SSL_STATUS_VALID,
)
1 change: 1 addition & 0 deletions readthedocs/proxito/tests/test_full.py
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,7 @@ def test_sitemap_all_private_versions(self):
ALLOW_PRIVATE_REPOS=True,
PUBLIC_DOMAIN='dev.readthedocs.io',
PUBLIC_DOMAIN_USES_HTTPS=True,
RTD_ALL_FEATURES_ENABLED=True,
)
class TestCDNCache(BaseDocServing):

Expand Down
1 change: 1 addition & 0 deletions readthedocs/proxito/tests/test_redirects.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
@override_settings(
PUBLIC_DOMAIN='dev.readthedocs.io',
PUBLIC_DOMAIN_USES_HTTPS=True,
RTD_ALL_FEATURES_ENABLED=True,
)
class RedirectTests(BaseDocServing):

Expand Down
5 changes: 4 additions & 1 deletion readthedocs/rtd_tests/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from django.contrib.auth.models import User
from django.core.cache import cache
from django.test import TestCase
from django.test import TestCase, override_settings
from django.urls import reverse
from django.utils import timezone
from django_dynamic_fixture import get, new
Expand Down Expand Up @@ -331,6 +331,9 @@ def test_rebuild_invalid_specific_commit(self, mock):
self.assertEqual(r.status_code, 302)


@override_settings(
RTD_ALL_FEATURES_ENABLED=True,
)
class TestSearchAnalyticsView(TestCase):

"""Tests for search analytics page."""
Expand Down
1 change: 1 addition & 0 deletions readthedocs/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@ def DOCKER_LIMITS(self):
DEFAULT_PRIVACY_LEVEL = 'public'
DEFAULT_VERSION_PRIVACY_LEVEL = 'public'
ALLOW_ADMIN = True
RTD_ALL_FEATURES_ENABLED = True

# Organization settings
RTD_ALLOW_ORGANIZATIONS = False
Expand Down
26 changes: 26 additions & 0 deletions readthedocs/subscriptions/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,29 @@ def get_feature(self, obj, type):
plan__subscriptions__organization=organization,
)
return feature.first()

# pylint: disable=redefined-builtin
def get_feature_value(self, obj, type, default=None):
"""
Get the value of the given feature.

Use this function instead of ``get_feature().value``
when you need to respect the ``RTD_ALL_FEATURES_ENABLED`` setting.
"""
if not settings.RTD_ALL_FEATURES_ENABLED:
feature = self.get_feature(obj, type)
if feature:
return feature.value
return default
Copy link
Member

Choose a reason for hiding this comment

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

Here is where I think it needs to encapsulate more logic, so we can rely 100% on it without mixing the .get_feature_value with other logic in the code.


# pylint: disable=redefined-builtin
def has_feature(self, obj, type):
"""
Get the value of the given feature.

Use this function instead of ``bool(get_feature())``
when you need to respect the ``RTD_ALL_FEATURES_ENABLED`` setting.
"""
if settings.RTD_ALL_FEATURES_ENABLED:
return True
return self.get_feature(obj, type) is not None