Browse Source

feature: filter examples by assignee

pull/2371/head
pawel 3 months ago
parent
commit
da75fc399f
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):
confirmed = BooleanFilter(field_name="states", method="filter_by_state")
label = CharFilter(method="filter_by_label")
assignee = CharFilter(method="filter_by_assignee")
def filter_by_state(self, queryset, field_name, is_confirmed: bool):
queryset = queryset.annotate(
@ -51,6 +52,9 @@ class ExampleFilter(FilterSet):
)
return queryset
def filter_by_assignee(self, queryset: QuerySet, field_name: str, assignee: str) -> QuerySet:
return queryset.filter(assignments__assignee__username=assignee)
class Meta:
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(
limit: any,
offset: string,
@ -39,13 +57,17 @@ function buildQueryParams(
params.append('confirmed', isChecked)
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()
}

Loading…
Cancel
Save