Skip to content

Querysets: simplify project querysets #8154

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 1 commit into from
May 6, 2021
Merged
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
20 changes: 1 addition & 19 deletions readthedocs/projects/managers.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
from django.db import models

from readthedocs.core.utils.extend import (
get_override_class,
SettingsOverrideObject
)
from readthedocs.projects.querysets import HTMLFileQuerySet


class HTMLFileManagerBase(models.Manager):

@classmethod
def from_queryset(cls, queryset_class, class_name=None):
queryset_class = get_override_class(
HTMLFileQuerySet,
HTMLFileQuerySet._default_class, # pylint: disable=protected-access
)
return super().from_queryset(queryset_class, class_name)
class HTMLFileManager(models.Manager):

def get_queryset(self):
return super().get_queryset().filter(name__endswith='.html')


class HTMLFileManager(SettingsOverrideObject):
_default_class = HTMLFileManagerBase
21 changes: 6 additions & 15 deletions readthedocs/projects/querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ def public(self, user=None):
queryset = self._add_user_repos(queryset, user)
return queryset.distinct()

def private(self, user=None):
queryset = self.filter(privacy_level=constants.PRIVATE)
if user:
queryset = self._add_user_repos(queryset, user)
return queryset.distinct()
def for_user(self, user):
"""Return all projects that an user belongs to."""
# In .org all users of a project are admins.
return self.for_admin_user(user)

def is_active(self, project):
"""
Expand Down Expand Up @@ -122,7 +121,7 @@ def prefetch_latest_build(self):

def dashboard(self, user):
"""Get the projects for this user including the latest build."""
return self.for_admin_user(user).prefetch_latest_build()
return self.for_user(user).prefetch_latest_build()

def api(self, user=None, detail=True):
if detail:
Expand All @@ -136,7 +135,6 @@ def api(self, user=None, detail=True):

class ProjectQuerySet(SettingsOverrideObject):
_default_class = ProjectQuerySetBase
_override_setting = 'PROJECT_MANAGER'


class RelatedProjectQuerySetBase(models.QuerySet):
Expand Down Expand Up @@ -188,7 +186,6 @@ def api(self, user=None):

class RelatedProjectQuerySet(SettingsOverrideObject):
_default_class = RelatedProjectQuerySetBase
_override_setting = 'RELATED_PROJECT_MANAGER'


class ParentRelatedProjectQuerySetBase(RelatedProjectQuerySetBase):
Expand All @@ -198,7 +195,6 @@ class ParentRelatedProjectQuerySetBase(RelatedProjectQuerySetBase):

class ParentRelatedProjectQuerySet(SettingsOverrideObject):
_default_class = ParentRelatedProjectQuerySetBase
_override_setting = 'RELATED_PROJECT_MANAGER'


class ChildRelatedProjectQuerySetBase(RelatedProjectQuerySetBase):
Expand All @@ -208,7 +204,6 @@ class ChildRelatedProjectQuerySetBase(RelatedProjectQuerySetBase):

class ChildRelatedProjectQuerySet(SettingsOverrideObject):
_default_class = ChildRelatedProjectQuerySetBase
_override_setting = 'RELATED_PROJECT_MANAGER'


class FeatureQuerySet(models.QuerySet):
Expand All @@ -222,7 +217,7 @@ def for_project(self, project):
).distinct()


class HTMLFileQuerySetBase(models.QuerySet):
class HTMLFileQuerySet(models.QuerySet):

def internal(self):
"""
Expand All @@ -240,7 +235,3 @@ def external(self):
It will only include pull request/merge request Version html files in the queries.
"""
return self.filter(version__type=EXTERNAL)


class HTMLFileQuerySet(SettingsOverrideObject):
_default_class = HTMLFileQuerySetBase
39 changes: 11 additions & 28 deletions readthedocs/rtd_tests/tests/test_project_querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from django.test import TestCase
from django_dynamic_fixture import get

from readthedocs.builds.models import Version
from readthedocs.projects.constants import PRIVATE, PUBLIC
from readthedocs.projects.models import Feature, Project
from readthedocs.projects.querysets import (
Expand Down Expand Up @@ -113,33 +112,6 @@ def test_dashboard(self):
self.assertEqual(query.count(), len(self.another_user_projects))
self.assertEqual(set(query), self.another_user_projects)

def test_private(self):
query = Project.objects.private()
projects = {
self.project_private,
self.another_project_private,
self.shared_project_private,
}
self.assertEqual(query.count(), len(projects))
self.assertEqual(set(query), projects)

def test_private_user(self):
query = Project.objects.private(user=self.user)
projects = (
self.user_projects |
{self.another_project_private}
)
self.assertEqual(query.count(), len(projects))
self.assertEqual(set(query), projects)

query = Project.objects.private(user=self.another_user)
projects = (
self.another_user_projects |
{self.project_private}
)
self.assertEqual(query.count(), len(projects))
self.assertEqual(set(query), projects)

def test_public(self):
query = Project.objects.public()
projects = {
Expand Down Expand Up @@ -167,6 +139,17 @@ def test_public_user(self):
self.assertEqual(query.count(), len(projects))
self.assertEqual(set(query), projects)

def test_for_user(self):
query = Project.objects.for_user(user=self.user)
projects = self.user_projects
self.assertEqual(query.count(), len(projects))
self.assertEqual(set(query), projects)

query = Project.objects.for_user(user=self.another_user)
projects = self.another_user_projects
self.assertEqual(query.count(), len(projects))
self.assertEqual(set(query), projects)


class FeatureQuerySetTests(TestCase):

Expand Down