Skip to content

Commit a485e32

Browse files
authored
Merge pull request doccano#2371 from synweap15/feature/filter-by-assignee
feature: filter examples by assignee
2 parents 72a4339 + 8ce9037 commit a485e32

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

backend/examples/filters.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class ExampleFilter(FilterSet):
88
confirmed = BooleanFilter(field_name="states", method="filter_by_state")
99
label = CharFilter(method="filter_by_label")
10+
assignee = CharFilter(method="filter_by_assignee")
1011

1112
def filter_by_state(self, queryset, field_name, is_confirmed: bool):
1213
queryset = queryset.annotate(
@@ -51,6 +52,9 @@ def filter_by_label(self, queryset: QuerySet, field_name: str, label: str) -> Qu
5152
)
5253
return queryset
5354

55+
def filter_by_assignee(self, queryset: QuerySet, field_name: str, assignee: str) -> QuerySet:
56+
return queryset.filter(assignments__assignee__username=assignee)
57+
5458
class Meta:
5559
model = Example
56-
fields = ("project", "text", "created_at", "updated_at", "label")
60+
fields = ("project", "text", "created_at", "updated_at", "label", "assignee")

frontend/repositories/example/apiDocumentRepository.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ function toPayload(item: ExampleItem): { [key: string]: any } {
2626
}
2727
}
2828

29+
/**
30+
* Extracts a parameter value from a query string.
31+
*
32+
* @param {string} q - The query string to extract the parameter from.
33+
* @param {string} name - The name of the parameter to extract.
34+
* @return {Array} - A tuple containing the updated query string and the extracted parameter value.
35+
* - If the parameter is not found, the extracted value will be null.
36+
*/
37+
function extractParamFromQuery(q: string, name: string): [string, string | null] {
38+
const pattern = new RegExp(`${name}:(".+?"|\\S+)`)
39+
if (pattern.test(q)) {
40+
const value = pattern.exec(q)![1]
41+
q = q.replace(pattern, '')
42+
return [q, value]
43+
}
44+
return [q, null]
45+
}
46+
2947
function buildQueryParams(
3048
limit: any,
3149
offset: string,
@@ -39,13 +57,17 @@ function buildQueryParams(
3957
params.append('confirmed', isChecked)
4058
params.append('ordering', ordering)
4159

42-
const pattern = /label:(".+?"|\S+)/
43-
if (pattern.test(q)) {
44-
const label = pattern.exec(q)![1]
45-
params.append('label', label.replace(/"/g, ''))
46-
q = q.replace(pattern, '')
47-
}
48-
params.append('q', q)
60+
const customParams = ['label', 'assignee']
61+
let updatedQuery: string = q
62+
customParams.forEach((param: string) => {
63+
let value: string | null
64+
;[updatedQuery, value] = extractParamFromQuery(updatedQuery, param)
65+
if (value !== null) {
66+
params.append(param, value)
67+
}
68+
})
69+
70+
params.append('q', updatedQuery)
4971
return params.toString()
5072
}
5173

0 commit comments

Comments
 (0)