forked from readthedocs/readthedocs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
managers.py
193 lines (146 loc) · 5.24 KB
/
managers.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"""Build and Version class model Managers."""
import structlog
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from polymorphic.managers import PolymorphicManager
from readthedocs.builds.constants import (
BRANCH,
EXTERNAL,
LATEST,
LATEST_VERBOSE_NAME,
STABLE,
STABLE_VERBOSE_NAME,
TAG,
)
from readthedocs.builds.querysets import VersionQuerySet
from readthedocs.core.utils.extend import get_override_class
log = structlog.get_logger(__name__)
__all__ = ['VersionManager']
class VersionManager(models.Manager):
"""
Version manager for manager only queries.
For queries not suitable for the :py:class:`VersionQuerySet`, such as create
queries.
"""
@classmethod
def from_queryset(cls, queryset_class, class_name=None):
# This is overridden because :py:meth:`models.Manager.from_queryset`
# uses `inspect` to retrieve the class methods, and the proxy class has
# no direct members.
queryset_class = get_override_class(
VersionQuerySet,
VersionQuerySet._default_class, # pylint: disable=protected-access
)
return super().from_queryset(queryset_class, class_name)
def create_stable(self, **kwargs):
defaults = {
'slug': STABLE,
'verbose_name': STABLE_VERBOSE_NAME,
'machine': True,
'active': True,
'identifier': STABLE,
'type': TAG,
}
defaults.update(kwargs)
return self.create(**defaults)
def create_latest(self, **kwargs):
defaults = {
'slug': LATEST,
'verbose_name': LATEST_VERBOSE_NAME,
'machine': True,
'active': True,
'identifier': LATEST,
'type': BRANCH,
}
defaults.update(kwargs)
return self.create(**defaults)
def get_object_or_log(self, **kwargs):
"""
Returns Version object or log.
It will return the Version object if found for the given kwargs,
otherwise it will log a warning along with all provided kwargs.
"""
try:
return super().get(**kwargs)
except ObjectDoesNotExist:
log.warning('Version not found for given kwargs.', kwargs=kwargs)
class InternalVersionManager(VersionManager):
"""
Version manager that only includes internal version.
It will exclude pull request/merge request versions from the queries
and only include BRANCH, TAG, UNKNOWN type Versions.
"""
def get_queryset(self):
return super().get_queryset().exclude(type=EXTERNAL)
class ExternalVersionManager(VersionManager):
"""
Version manager that only includes external version.
It will only include pull request/merge request Versions in the queries.
"""
def get_queryset(self):
return super().get_queryset().filter(type=EXTERNAL)
class InternalBuildManager(models.Manager):
"""
Build manager that only includes internal version builds.
It will exclude pull request/merge request version builds from the queries
and only include BRANCH, TAG, UNKNOWN type Version builds.
"""
def get_queryset(self):
return super().get_queryset().exclude(version__type=EXTERNAL)
class ExternalBuildManager(models.Manager):
"""
Build manager that only includes external version builds.
It will only include pull request/merge request version builds in the queries.
"""
def get_queryset(self):
return super().get_queryset().filter(version__type=EXTERNAL)
class VersionAutomationRuleManager(PolymorphicManager):
"""
Manager for VersionAutomationRule.
.. note::
This manager needs to inherit from PolymorphicManager,
since the model is a PolymorphicModel.
See https://django-polymorphic.readthedocs.io/page/managers.html
"""
def add_rule(
self, *, project, description, match_arg, version_type,
action, action_arg=None, predefined_match_arg=None,
):
"""
Append an automation rule to `project`.
The rule is created with a priority lower than the last rule
in `project`.
"""
last_priority = (
project.automation_rules
.values_list('priority', flat=True)
.order_by('priority')
.last()
)
if last_priority is None:
priority = 0
else:
priority = last_priority + 1
rule = self.create(
project=project,
priority=priority,
description=description,
match_arg=match_arg,
predefined_match_arg=predefined_match_arg,
version_type=version_type,
action=action,
action_arg=action_arg,
)
return rule
class AutomationRuleMatchManager(models.Manager):
def register_match(self, rule, version, max_registers=15):
created = self.create(
rule=rule,
match_arg=rule.get_match_arg(),
action=rule.action,
version_name=version.verbose_name,
version_type=version.type,
)
for match in self.filter(rule__project=rule.project)[max_registers:]:
match.delete()
return created