Skip to content

Versions: enforce using project.versions instead of Version.filter(project=project) #12411

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 1 addition & 3 deletions readthedocs/api/v2/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from rest_framework_api_key.permissions import KeyParser

from readthedocs.api.v2.models import BuildAPIKey
from readthedocs.builds.models import Version


class IsOwner(permissions.BasePermission):
Expand Down Expand Up @@ -39,9 +38,8 @@ def has_permission(self, request, view):
project = view._get_project()
version = view._get_version()
has_access = (
Version.objects.public(
project.versions.public(
user=request.user,
project=project,
only_active=False,
)
.filter(pk=version.pk)
Expand Down
3 changes: 1 addition & 2 deletions readthedocs/api/v2/views/core_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

from readthedocs.api.v2.permissions import HasBuildAPIKey
from readthedocs.builds.constants import LATEST
from readthedocs.builds.models import Version
from readthedocs.core.templatetags.core_tags import make_document_url
from readthedocs.projects.models import Project

Expand Down Expand Up @@ -61,7 +60,7 @@ def docurl(request):

project = get_object_or_404(Project, slug=project)
version = get_object_or_404(
Version.objects.public(request.user, project=project, only_active=False),
project.versions.public(request.user, only_active=False),
slug=version,
)
return Response(
Expand Down
5 changes: 2 additions & 3 deletions readthedocs/builds/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from readthedocs.builds.constants import BUILD_FINAL_STATES
from readthedocs.builds.constants import BUILD_STATE_FINISHED
from readthedocs.builds.constants import EXTERNAL
from readthedocs.builds.models import Version
from readthedocs.builds.constants import INTERNAL
from readthedocs.core.filters import FilteredModelChoiceFilter
from readthedocs.core.filters import ModelFilterSet

Expand Down Expand Up @@ -67,9 +67,8 @@ def get_version_queryset(self):
# Copied from the version listing view. We need this here as this is
# what allows the build version list to populate. Otherwise the
# ``all()`` queryset method is used.
return Version.internal.public(
return self.project.versions(manager=INTERNAL).public(
user=self.request.user,
project=self.project,
)

def get_state(self, queryset, _, value):
Expand Down
3 changes: 0 additions & 3 deletions readthedocs/builds/querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ def _public_only(self):
def public(
self,
user=None,
project=None,
only_active=True,
include_hidden=True,
only_built=False,
Expand All @@ -101,8 +100,6 @@ def public(
queryset = self.all()
else:
queryset = self._add_from_user_projects(queryset, user, admin=True, member=True)
if project:
queryset = queryset.filter(project=project)
if only_active:
queryset = queryset.filter(active=True)
if only_built:
Expand Down
5 changes: 2 additions & 3 deletions readthedocs/builds/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
from requests.utils import quote

from readthedocs.builds.constants import BUILD_FINAL_STATES
from readthedocs.builds.constants import INTERNAL
from readthedocs.builds.filters import BuildListFilter
from readthedocs.builds.models import Build
from readthedocs.builds.models import Version
from readthedocs.core.filters import FilterContextMixin
from readthedocs.core.permissions import AdminPermission
from readthedocs.core.utils import cancel_build
Expand Down Expand Up @@ -55,9 +55,8 @@ class BuildList(
filterset_class = BuildListFilter

def _get_versions(self, project):
return Version.internal.public(
project.versions(manager=INTERNAL).public(
user=self.request.user,
project=project,
)

def get_project(self):
Expand Down
4 changes: 1 addition & 3 deletions readthedocs/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1103,9 +1103,7 @@ def latest_internal_build(self):
return self.builds(manager=INTERNAL).select_related("version").first()

def active_versions(self):
from readthedocs.builds.models import Version

versions = Version.internal.public(project=self, only_active=True)
versions = self.versions(manager=INTERNAL).public(only_active=True)
return versions.filter(built=True, active=True) | versions.filter(
active=True, uploaded=True
)
Expand Down
12 changes: 8 additions & 4 deletions readthedocs/projects/views/private.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from vanilla import UpdateView

from readthedocs.analytics.models import PageView
from readthedocs.builds.constants import INTERNAL
from readthedocs.builds.forms import RegexAutomationRuleForm
from readthedocs.builds.forms import VersionForm
from readthedocs.builds.models import AutomationRuleMatch
Expand Down Expand Up @@ -234,10 +235,13 @@ def get_success_url(self):

class ProjectVersionEditMixin(ProjectVersionMixin):
def get_queryset(self):
return Version.internal.public(
user=self.request.user,
project=self.get_project(),
only_active=False,
return (
self.get_project()
.versions(manager=INTERNAL)
.public(
user=self.request.user,
only_active=False,
)
)

def form_valid(self, form):
Expand Down
6 changes: 2 additions & 4 deletions readthedocs/projects/views/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,8 @@ class ProjectDetailViewBase(
filterset_class = ProjectVersionListFilterSet

def _get_versions(self, project):
return Version.internal.public(
return project.versions(manager=INTERNAL).public(
user=self.request.user,
project=project,
)

def get_queryset(self):
Expand Down Expand Up @@ -407,9 +406,8 @@ def project_versions(request, project_slug):
slug=project_slug,
)

versions = Version.internal.public(
versions = project.versions(manager=INTERNAL).public(
user=request.user,
project=project,
only_active=False,
)
active_versions = versions.filter(active=True)
Expand Down
3 changes: 0 additions & 3 deletions readthedocs/proxito/views/hosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,7 @@ def _get_versions(self, request, project):
- They are active
- They are not hidden
"""
# NOTE: Use project.versions, not Version.objects,
# so all results share the same instance of project.
return project.versions(manager=INTERNAL).public(
project=project,
user=request.user,
only_active=True,
only_built=True,
Expand Down
10 changes: 5 additions & 5 deletions readthedocs/proxito/views/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

from readthedocs.api.mixins import CDNCacheTagsMixin
from readthedocs.builds.constants import EXTERNAL
from readthedocs.builds.constants import INTERNAL
from readthedocs.builds.constants import LATEST
from readthedocs.builds.constants import STABLE
from readthedocs.builds.models import Version
from readthedocs.core.mixins import CDNCacheControlMixin
from readthedocs.core.resolver import Resolver
from readthedocs.core.unresolver import InvalidExternalVersionError
Expand Down Expand Up @@ -713,7 +713,7 @@ def get(self, request):

def _get_hidden_paths(self, project):
"""Get the absolute paths of the public hidden versions of `project`."""
hidden_versions = Version.internal.public(project=project).filter(hidden=True)
hidden_versions = project.versions(manager=INTERNAL).public().filter(hidden=True)
resolver = Resolver()
hidden_paths = [
resolver.resolve_path(project, version_slug=version.slug) for version in hidden_versions
Expand Down Expand Up @@ -803,8 +803,7 @@ def changefreqs_generator():
yield from itertools.chain(changefreqs, itertools.repeat("monthly"))

project = request.unresolved_domain.project
public_versions = Version.internal.public(
project=project,
public_versions = project.versions(manager=INTERNAL).public(
only_active=True,
include_hidden=False,
)
Expand Down Expand Up @@ -851,7 +850,8 @@ def changefreqs_generator():
if project.translations.exists():
for translation in project.translations.all():
translated_version = (
Version.internal.public(project=translation)
translation.versions(manager=INTERNAL)
.public()
.filter(slug=version.slug)
.first()
)
Expand Down
8 changes: 4 additions & 4 deletions readthedocs/rtd_tests/tests/test_version_querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.test import TestCase
from django_dynamic_fixture import get

from readthedocs.builds.constants import EXTERNAL, LATEST
from readthedocs.builds.constants import EXTERNAL, INTERNAL, LATEST
from readthedocs.builds.models import Version
from readthedocs.projects.constants import PRIVATE, PUBLIC
from readthedocs.projects.models import Project
Expand Down Expand Up @@ -122,7 +122,7 @@ def test_public_user(self):
self.assertEqual(set(query), versions)

def test_public_project(self):
query = Version.objects.public(user=self.user, project=self.project)
query = self.project.versions.public(user=self.user)
versions = {
self.version,
self.version_latest,
Expand Down Expand Up @@ -243,7 +243,7 @@ def test_public_user(self):
self.assertEqual(set(query), versions)

def test_public_project(self):
query = Version.internal.public(user=self.user, project=self.project)
query = self.project.versions(manager=INTERNAL).public(user=self.user)
versions = {
self.version,
self.version_latest,
Expand Down Expand Up @@ -341,7 +341,7 @@ def test_public_user(self):
self.assertEqual(set(query), versions)

def test_public_project(self):
query = Version.external.public(user=self.user, project=self.project)
query = self.project.versions(manager=EXTERNAL).public(user=self.user)
versions = {
self.external_version_public,
self.external_version_private,
Expand Down
6 changes: 3 additions & 3 deletions readthedocs/search/api/v2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from readthedocs.api.mixins import CDNCacheTagsMixin
from readthedocs.api.v2.permissions import IsAuthorizedToViewVersion
from readthedocs.builds.models import Version
from readthedocs.builds.constants import INTERNAL
from readthedocs.core.utils.extend import SettingsOverrideObject
from readthedocs.projects.models import Feature
from readthedocs.projects.models import Project
Expand Down Expand Up @@ -116,9 +116,9 @@ def _get_project_version(self, project, version_slug, include_hidden=True):
:param include_hidden: If hidden versions should be considered.
"""
return (
Version.internal.public(
project.versions(manager=INTERNAL)
.public(
user=self.request.user,
project=project,
only_built=True,
include_hidden=include_hidden,
)
Expand Down
6 changes: 3 additions & 3 deletions readthedocs/search/api/v3/executor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from functools import cached_property
from itertools import islice

from readthedocs.builds.models import Version
from readthedocs.builds.constants import INTERNAL
from readthedocs.projects.models import Project
from readthedocs.search.api.v3.queryparser import SearchQueryParser
from readthedocs.search.faceted_search import PageSearch
Expand Down Expand Up @@ -168,9 +168,9 @@ def _get_project_version(self, project, version_slug, include_hidden=True):
:param include_hidden: If hidden versions should be considered.
"""
return (
Version.internal.public(
project.versions(manager=INTERNAL)
.public(
user=self.request.user,
project=project,
only_built=True,
include_hidden=include_hidden,
)
Expand Down