api: support filtering form using status label (#67266)

This commit is contained in:
Agate 2022-07-12 12:10:44 +02:00 committed by Agate Berriot
parent 3bcf1f3e2a
commit 7bfcbcdd37
2 changed files with 73 additions and 6 deletions

View File

@ -861,6 +861,60 @@ def test_api_list_formdata_order_by_rank(pub, local_user):
assert [int(x['id']) for x in resp.json] == [formdata3.id, formdata1.id]
def test_api_list_formdata_filter_status(pub, local_user):
pub.role_class.wipe()
role = pub.role_class(name='test')
role.store()
Workflow.wipe()
workflow = Workflow(name='noop')
workflow.add_status('Pending', 'new')
workflow.add_status('Ongoing', 'wip')
workflow.add_status('Completed', 'done')
workflow.store()
FormDef.wipe()
formdef = FormDef()
formdef.name = 'foo'
formdef.fields = []
formdef.workflow_id = workflow.id
formdef.workflow_roles = {'_receiver': role.id}
formdef.store()
local_user.roles = [role.id]
local_user.store()
data_class = formdef.data_class()
data_class.wipe()
new = data_class()
new.data = {}
new.user_id = local_user.id
new.just_created()
new.jump_status('new')
new.store()
wip = data_class()
wip.data = {}
wip.user_id = local_user.id
wip.just_created()
wip.jump_status('wip')
wip.store()
resp = get_app(pub).get(sign_uri('/api/forms/foo/list?filter=all', user=local_user))
assert len(resp.json) == 2
# filter on id
resp = get_app(pub).get(sign_uri('/api/forms/foo/list?filter=new', user=local_user))
assert len(resp.json) == 1
assert resp.json[0]['id'] == new.id
# filter on name
resp = get_app(pub).get(sign_uri('/api/forms/foo/list?filter=Ongoing', user=local_user))
assert len(resp.json) == 1
assert resp.json[0]['id'] == wip.id
def test_api_list_formdata_unknown_filter(pub, local_user):
pub.role_class.wipe()
role = pub.role_class(name='test')

View File

@ -89,6 +89,13 @@ from ..qommon.template import Template
from ..qommon.upload_storage import PicklableUpload
from .submission import FormFillPage
COMMON_STATUSES = [
('waiting', _('Waiting for an action'), 'waiting'),
('open', C_('formdata|Open'), 'open'),
('done', _('Done'), 'done'),
('all', _('All'), 'all'),
]
def geojson_formdatas(formdatas, geoloc_key='base', fields=None):
geojson = {'type': 'FeatureCollection', 'features': []}
@ -294,12 +301,7 @@ class ManagementDirectory(Directory):
SingleSelectWidget,
'status',
title=_('Status'),
options=[
('waiting', _('Waiting for an action'), 'waiting'),
('open', C_('formdata|Open'), 'open'),
('done', _('Done'), 'done'),
('all', _('All'), 'all'),
],
options=COMMON_STATUSES,
value=params.get('status'),
)
form.add(DateWidget, 'start', title=_('Start Date'), value=params.get('start'))
@ -2354,6 +2356,17 @@ class FormPage(Directory):
limit = None
if 'limit' in get_request().form:
limit = misc.get_int_or_400(get_request().form['limit'])
common_statuses = [id for id, name, _ in COMMON_STATUSES]
if selected_filter not in common_statuses:
# filtering by status can be done with status ID or name,
# we handle the mapping here
for status in self.formdef.workflow.possible_status or []:
if selected_filter in [status.id, status.name]:
selected_filter = status.id
break
items = FormDefUI(self.formdef).get_listing_items(
None,
selected_filter,