forked from readthedocs/readthedocs.org
/
querysets.py
150 lines (113 loc) · 4.84 KB
/
querysets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""Build and Version QuerySet classes."""
from django.db import models
from guardian.shortcuts import get_objects_for_user
from readthedocs.core.utils.extend import SettingsOverrideObject
from readthedocs.projects import constants
__all__ = ['VersionQuerySet', 'BuildQuerySet', 'RelatedBuildQuerySet']
class VersionQuerySetBase(models.QuerySet):
"""Versions take into account their own privacy_level setting."""
use_for_related_fields = True
def _add_user_repos(self, queryset, user):
if user.has_perm('builds.view_version'):
return self.all().distinct()
if user.is_authenticated:
user_queryset = get_objects_for_user(user, 'builds.view_version')
queryset = user_queryset | queryset
return queryset.distinct()
def public(self, user=None, project=None, only_active=True):
queryset = self.filter(privacy_level=constants.PUBLIC)
if user:
queryset = self._add_user_repos(queryset, user)
if project:
queryset = queryset.filter(project=project)
if only_active:
queryset = queryset.filter(active=True)
return queryset
def protected(self, user=None, project=None, only_active=True):
queryset = self.filter(
privacy_level__in=[constants.PUBLIC, constants.PROTECTED],
)
if user:
queryset = self._add_user_repos(queryset, user)
if project:
queryset = queryset.filter(project=project)
if only_active:
queryset = queryset.filter(active=True)
return queryset
def private(self, user=None, project=None, only_active=True):
queryset = self.filter(privacy_level__in=[constants.PRIVATE])
if user:
queryset = self._add_user_repos(queryset, user)
if project:
queryset = queryset.filter(project=project)
if only_active:
queryset = queryset.filter(active=True)
return queryset
def api(self, user=None, detail=True):
if detail:
return self.public(user, only_active=False)
queryset = self.none()
if user:
queryset = self._add_user_repos(queryset, user)
return queryset
def for_project(self, project):
"""Return all versions for a project, including translations."""
return self.filter(
models.Q(project=project) |
models.Q(project__main_language_project=project),
)
class VersionQuerySet(SettingsOverrideObject):
_default_class = VersionQuerySetBase
class BuildQuerySetBase(models.QuerySet):
"""
Build objects that are privacy aware.
i.e. they take into account the privacy of the Version that they relate to.
"""
use_for_related_fields = True
def _add_user_repos(self, queryset, user=None):
if user.has_perm('builds.view_version'):
return self.all().distinct()
if user.is_authenticated:
user_queryset = get_objects_for_user(user, 'builds.view_version')
pks = user_queryset.values_list('pk', flat=True)
queryset = self.filter(version__pk__in=pks) | queryset
return queryset.distinct()
def public(self, user=None, project=None):
queryset = self.filter(version__privacy_level=constants.PUBLIC)
if user:
queryset = self._add_user_repos(queryset, user)
if project:
queryset = queryset.filter(project=project)
return queryset
def api(self, user=None, detail=True):
if detail:
return self.public(user)
queryset = self.none()
if user:
queryset = self._add_user_repos(queryset, user)
return queryset
class BuildQuerySet(SettingsOverrideObject):
_default_class = BuildQuerySetBase
class RelatedBuildQuerySetBase(models.QuerySet):
"""For models with association to a project through :py:class:`Build`"""
use_for_related_fields = True
def _add_user_repos(self, queryset, user=None):
if user.has_perm('builds.view_version'):
return self.all().distinct()
if user.is_authenticated:
user_queryset = get_objects_for_user(user, 'builds.view_version')
pks = user_queryset.values_list('pk', flat=True)
queryset = self.filter(build__version__pk__in=pks,) | queryset
return queryset.distinct()
def public(self, user=None, project=None):
queryset = self.filter(build__version__privacy_level=constants.PUBLIC)
if user:
queryset = self._add_user_repos(queryset, user)
if project:
queryset = queryset.filter(build__project=project)
return queryset
def api(self, user=None):
return self.public(user)
class RelatedBuildQuerySet(SettingsOverrideObject):
_default_class = RelatedBuildQuerySetBase
_override_setting = 'RELATED_BUILD_MANAGER'