Browse Source

Merge pull request #2371 from synweap15/feature/filter-by-assignee

feature: filter examples by assignee
master
Hiroki Nakayama 2 months ago
committed by GitHub
parent
commit
a485e32423
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 8 deletions
  1. 6
      backend/examples/filters.py
  2. 36
      frontend/repositories/example/apiDocumentRepository.ts

6
backend/examples/filters.py

@ -7,6 +7,7 @@ from .models import Example
class ExampleFilter(FilterSet): class ExampleFilter(FilterSet):
confirmed = BooleanFilter(field_name="states", method="filter_by_state") confirmed = BooleanFilter(field_name="states", method="filter_by_state")
label = CharFilter(method="filter_by_label") label = CharFilter(method="filter_by_label")
assignee = CharFilter(method="filter_by_assignee")
def filter_by_state(self, queryset, field_name, is_confirmed: bool): def filter_by_state(self, queryset, field_name, is_confirmed: bool):
queryset = queryset.annotate( queryset = queryset.annotate(
@ -51,6 +52,9 @@ class ExampleFilter(FilterSet):
) )
return queryset return queryset
def filter_by_assignee(self, queryset: QuerySet, field_name: str, assignee: str) -> QuerySet:
return queryset.filter(assignments__assignee__username=assignee)
class Meta: class Meta:
model = Example model = Example
fields = ("project", "text", "created_at", "updated_at", "label")
fields = ("project", "text", "created_at", "updated_at", "label", "assignee")

36
frontend/repositories/example/apiDocumentRepository.ts

@ -26,6 +26,24 @@ function toPayload(item: ExampleItem): { [key: string]: any } {
} }
} }
/**
* Extracts a parameter value from a query string.
*
* @param {string} q - The query string to extract the parameter from.
* @param {string} name - The name of the parameter to extract.
* @return {Array} - A tuple containing the updated query string and the extracted parameter value.
* - If the parameter is not found, the extracted value will be null.
*/
function extractParamFromQuery(q: string, name: string): [string, string | null] {
const pattern = new RegExp(`${name}:(".+?"|\\S+)`)
if (pattern.test(q)) {
const value = pattern.exec(q)![1]
q = q.replace(pattern, '')
return [q, value]
}
return [q, null]
}
function buildQueryParams( function buildQueryParams(
limit: any, limit: any,
offset: string, offset: string,
@ -39,13 +57,17 @@ function buildQueryParams(
params.append('confirmed', isChecked) params.append('confirmed', isChecked)
params.append('ordering', ordering) params.append('ordering', ordering)
const pattern = /label:(".+?"|\S+)/
if (pattern.test(q)) {
const label = pattern.exec(q)![1]
params.append('label', label.replace(/"/g, ''))
q = q.replace(pattern, '')
}
params.append('q', q)
const customParams = ['label', 'assignee']
let updatedQuery: string = q
customParams.forEach((param: string) => {
let value: string | null
;[updatedQuery, value] = extractParamFromQuery(updatedQuery, param)
if (value !== null) {
params.append(param, value)
}
})
params.append('q', updatedQuery)
return params.toString() return params.toString()
} }

Loading…
Cancel
Save