FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
patchwork/patchwork/api/filters.py at main · getpatchwork/patchwork · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
getpatchwork
/
patchwork
Public
Notifications
You must be signed in to change notification settings
Fork
90
Star
315
Code
Issues
97
Pull requests
22
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
patchwork
/
patchwork
/
api
/
filters.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
278 lines (211 loc) · 8.38 KB
Breadcrumbs
patchwork
/
patchwork
/
api
/
filters.py
Copy path
File metadata and controls
278 lines (211 loc) · 8.38 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# Patchwork - automated patch tracking system
# Copyright (C) 2017 Stephen Finucane <stephen@that.guru>
#
# SPDX-License-Identifier: GPL-2.0-or-later
from
django
.
contrib
.
auth
.
models
import
User
from
django
.
core
.
exceptions
import
ValidationError
from
django
.
db
.
models
import
Q
from
django_filters
import
rest_framework
from
django_filters
.
rest_framework
import
FilterSet
from
django_filters
import
CharFilter
from
django_filters
import
IsoDateTimeFilter
from
django_filters
import
ModelMultipleChoiceFilter
from
django
.
forms
import
ModelMultipleChoiceField
as
BaseMultipleChoiceField
from
django
.
forms
.
widgets
import
MultipleHiddenInput
from
rest_framework
import
exceptions
from
patchwork
.
api
import
utils
from
patchwork
.
models
import
Bundle
from
patchwork
.
models
import
Check
from
patchwork
.
models
import
Cover
from
patchwork
.
models
import
Event
from
patchwork
.
models
import
Patch
from
patchwork
.
models
import
Person
from
patchwork
.
models
import
Project
from
patchwork
.
models
import
Series
from
patchwork
.
models
import
State
# custom backend
class
DjangoFilterBackend
(
rest_framework
.
DjangoFilterBackend
):
def
filter_queryset
(
self
,
request
,
queryset
,
view
):
try
:
return
super
().
filter_queryset
(
request
,
queryset
,
view
)
except
exceptions
.
ValidationError
:
return
queryset
.
none
()
# custom fields, filters
class
ModelMultipleChoiceField
(
BaseMultipleChoiceField
):
def
_get_filter
(
self
,
value
):
if
not
self
.
alternate_lookup
:
return
'pk'
,
value
try
:
return
'pk'
,
int
(
value
)
except
ValueError
:
return
self
.
alternate_lookup
,
value
def
_check_values
(
self
,
value
):
"""
Given a list of possible PK values, returns a QuerySet of the
corresponding objects. Raises a ValidationError if a given value is
invalid (not a valid PK, not in the queryset, etc.)
"""
# deduplicate given values to avoid creating many querysets or
# requiring the database backend deduplicate efficiently.
try
:
value
=
frozenset
(
value
)
except
TypeError
:
# list of lists isn't hashable, for example
raise
ValidationError
(
self
.
error_messages
[
'list'
],
code
=
'list'
,
)
q_objects
=
Q
()
for
pk
in
value
:
key
,
val
=
self
.
_get_filter
(
pk
)
try
:
# NOTE(stephenfin): In contrast to the Django implementation
# of this, we check to ensure each specified key exists and
# fail if not. If we don't this, we can end up doing nothing
# for the filtering which, to me, seems very confusing
self
.
queryset
.
get
(
**
{
key
:
val
})
except
(
ValueError
,
TypeError
,
self
.
queryset
.
model
.
DoesNotExist
):
raise
ValidationError
(
self
.
error_messages
[
'invalid_pk_value'
],
code
=
'invalid_pk_value'
,
params
=
{
'pk'
:
val
},
)
q_objects
|=
Q
(
**
{
key
:
val
})
qs
=
self
.
queryset
.
filter
(
q_objects
)
return
qs
class
BaseField
(
ModelMultipleChoiceField
):
alternate_lookup
=
None
class
BaseFilter
(
ModelMultipleChoiceFilter
):
field_class
=
BaseField
class
PersonChoiceField
(
ModelMultipleChoiceField
):
alternate_lookup
=
'email__iexact'
class
PersonFilter
(
ModelMultipleChoiceFilter
):
field_class
=
PersonChoiceField
class
ProjectChoiceField
(
ModelMultipleChoiceField
):
alternate_lookup
=
'linkname__iexact'
class
ProjectFilter
(
ModelMultipleChoiceFilter
):
field_class
=
ProjectChoiceField
class
StateChoiceField
(
ModelMultipleChoiceField
):
def
_get_filter
(
self
,
value
):
try
:
return
'pk'
,
int
(
value
)
except
ValueError
:
return
'name__iexact'
,
' '
.
join
(
value
.
split
(
'-'
))
class
StateFilter
(
ModelMultipleChoiceFilter
):
field_class
=
StateChoiceField
class
UserChoiceField
(
ModelMultipleChoiceField
):
alternate_lookup
=
'username__iexact'
class
UserFilter
(
ModelMultipleChoiceFilter
):
field_class
=
UserChoiceField
# filter sets
class
BaseFilterSet
(
FilterSet
):
@
property
def
form
(
self
):
form
=
super
(
BaseFilterSet
,
self
).
form
for
version
in
getattr
(
self
.
Meta
,
'versioned_fields'
, {}):
if
utils
.
has_version
(
self
.
request
,
version
):
continue
for
field
in
self
.
Meta
.
versioned_fields
[
version
]:
if
field
in
form
.
fields
:
del
form
.
fields
[
field
]
return
form
class
TimestampMixin
(
BaseFilterSet
):
# TODO(stephenfin): These should filter on a 'updated_at' field instead
before
=
IsoDateTimeFilter
(
lookup_expr
=
'lt'
,
field_name
=
'date'
)
since
=
IsoDateTimeFilter
(
lookup_expr
=
'gte'
,
field_name
=
'date'
)
class
SeriesFilterSet
(
TimestampMixin
,
BaseFilterSet
):
submitter
=
PersonFilter
(
queryset
=
Person
.
objects
.
all
(),
distinct
=
False
)
project
=
ProjectFilter
(
queryset
=
Project
.
objects
.
all
(),
distinct
=
False
)
class
Meta
:
model
=
Series
fields
=
(
'submitter'
,
'project'
)
def
msgid_filter
(
queryset
,
name
,
value
):
return
queryset
.
filter
(
**
{
name
:
'<'
+
value
+
'>'
})
class
CoverFilterSet
(
TimestampMixin
,
BaseFilterSet
):
project
=
ProjectFilter
(
queryset
=
Project
.
objects
.
all
(),
distinct
=
False
)
# NOTE(stephenfin): We disable the select-based HTML widgets for these
# filters as the resulting query is _huge_
series
=
BaseFilter
(
queryset
=
Project
.
objects
.
all
(),
widget
=
MultipleHiddenInput
,
distinct
=
False
,
)
submitter
=
PersonFilter
(
queryset
=
Person
.
objects
.
all
(),
distinct
=
False
)
msgid
=
CharFilter
(
method
=
msgid_filter
)
class
Meta
:
model
=
Cover
fields
=
(
'project'
,
'series'
,
'submitter'
)
class
PatchFilterSet
(
TimestampMixin
,
BaseFilterSet
):
project
=
ProjectFilter
(
queryset
=
Project
.
objects
.
all
(),
distinct
=
False
)
# NOTE(stephenfin): We disable the select-based HTML widgets for these
# filters as the resulting query is _huge_
series
=
BaseFilter
(
queryset
=
Series
.
objects
.
all
(),
widget
=
MultipleHiddenInput
,
distinct
=
False
,
)
submitter
=
PersonFilter
(
queryset
=
Person
.
objects
.
all
(),
distinct
=
False
)
delegate
=
UserFilter
(
queryset
=
User
.
objects
.
all
(),
distinct
=
False
)
state
=
StateFilter
(
queryset
=
State
.
objects
.
all
(),
distinct
=
False
)
hash
=
CharFilter
(
lookup_expr
=
'iexact'
)
msgid
=
CharFilter
(
method
=
msgid_filter
)
class
Meta
:
model
=
Patch
# NOTE(dja): ideally we want to version the hash/msgid field, but I
# can't find a way to do that which is reliable and not extremely ugly.
# The best I can come up with is manually working with request.GET
# which seems to rather defeat the point of using django-filters.
fields
=
(
'project'
,
'series'
,
'submitter'
,
'delegate'
,
'state'
,
'archived'
,
'hash'
,
'msgid'
,
)
versioned_fields
=
{
'1.2'
: (
'hash'
,
'msgid'
),
}
class
CheckFilterSet
(
TimestampMixin
,
BaseFilterSet
):
user
=
UserFilter
(
queryset
=
User
.
objects
.
all
(),
distinct
=
False
)
class
Meta
:
model
=
Check
fields
=
(
'user'
,
'state'
,
'context'
)
class
EventFilterSet
(
TimestampMixin
,
BaseFilterSet
):
# NOTE(stephenfin): We disable the select-based HTML widgets for these
# filters as the resulting query is _huge_
# TODO(stephenfin): We should really use an AJAX widget of some form here
project
=
ProjectFilter
(
queryset
=
Project
.
objects
.
all
(),
widget
=
MultipleHiddenInput
,
distinct
=
False
,
)
series
=
BaseFilter
(
queryset
=
Series
.
objects
.
all
(),
widget
=
MultipleHiddenInput
,
distinct
=
False
,
)
patch
=
BaseFilter
(
queryset
=
Patch
.
objects
.
all
(),
widget
=
MultipleHiddenInput
,
distinct
=
False
,
)
cover
=
BaseFilter
(
queryset
=
Cover
.
objects
.
all
(),
widget
=
MultipleHiddenInput
,
distinct
=
False
,
)
class
Meta
:
model
=
Event
fields
=
(
'project'
,
'category'
,
'series'
,
'patch'
,
'cover'
,
'actor'
)
versioned_fields
=
{
'1.2'
: (
'actor'
,),
}
class
BundleFilterSet
(
BaseFilterSet
):
project
=
ProjectFilter
(
queryset
=
Project
.
objects
.
all
(),
distinct
=
False
)
owner
=
UserFilter
(
queryset
=
User
.
objects
.
all
(),
distinct
=
False
)
class
Meta
:
model
=
Bundle
fields
=
(
'project'
,
'owner'
,
'public'
)
Back
|
FazBrowse Home
|
New Git URL