Project

General

Profile

0001-api-add-limit-offset-sort-to-api-users-.-forms-35403.patch

Frédéric Péters, 14 Aug 2019 02:21 PM

Download (4.71 KB)

View differences:

Subject: [PATCH] api: add limit/offset/sort to api/users/<...>/forms (#35403)

 tests/test_api.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++
 wcs/api.py        | 12 +++++++++-
 2 files changed, 70 insertions(+), 1 deletion(-)
tests/test_api.py
1385 1385
    draft_formdata = [x for x in resp.json['data'] if x['status'] == 'Draft'][0]
1386 1386
    assert draft_formdata.get('url')[-1] != '/'
1387 1387

  
1388
    formdata = formdef.data_class()()
1389
    formdata.data = {'0': 'foo@localhost', '1': 'xyy'}
1390
    formdata.user_id = local_user.id
1391
    formdata.just_created()
1392
    formdata.jump_status('new')
1393
    formdata.store()
1394

  
1395
    resp = get_app(pub).get(sign_uri('/api/user/forms', user=local_user))
1396
    assert len(resp.json['data']) == 2
1397
    resp2 = get_app(pub).get(sign_uri('/api/user/forms?sort=desc', user=local_user))
1398
    assert len(resp2.json['data']) == 2
1399
    assert resp2.json['data'][0] == resp.json['data'][1]
1400
    assert resp2.json['data'][1] == resp.json['data'][0]
1401

  
1402
def test_user_forms_limit_offset(pub, local_user):
1403
    if not pub.is_using_postgresql():
1404
        pytest.skip('this requires SQL')
1405
        return
1406

  
1407
    FormDef.wipe()
1408
    formdef = FormDef()
1409
    formdef.name = 'test limit offset'
1410
    formdef.fields = [
1411
        fields.StringField(id='0', label='foobar', varname='foobar'),
1412
        fields.StringField(id='1', label='foobar2'),]
1413
    formdef.keywords = 'hello, world'
1414
    formdef.disabled = False
1415
    formdef.enable_tracking_codes = False
1416
    formdef.store()
1417
    formdef.data_class().wipe()
1418

  
1419
    for i in range(50):
1420
        formdata = formdef.data_class()()
1421
        formdata.data = {'0': 'foo@localhost', '1': str(i)}
1422
        formdata.user_id = local_user.id
1423
        formdata.just_created()
1424
        formdata.receipt_time = (datetime.datetime.now() + datetime.timedelta(days=i)).timetuple()
1425
        formdata.jump_status('new')
1426
        formdata.store()
1427

  
1428
    resp = get_app(pub).get(sign_uri('/api/users/%s/forms' % local_user.id))
1429
    assert resp.json['err'] == 0
1430
    assert len(resp.json['data']) == 50
1431

  
1432
    resp = get_app(pub).get(sign_uri('/api/users/%s/forms?limit=10' % local_user.id))
1433
    assert resp.json['err'] == 0
1434
    assert len(resp.json['data']) == 10
1435
    assert [x['form_number_raw'] for x in resp.json['data']] == [str(x) for x in range(1, 11)]
1436

  
1437
    resp = get_app(pub).get(sign_uri('/api/users/%s/forms?limit=10&offset=45' % local_user.id))
1438
    assert resp.json['err'] == 0
1439
    assert len(resp.json['data']) == 5
1440
    assert [x['form_number_raw'] for x in resp.json['data']] == [str(x) for x in range(46, 51)]
1441

  
1442
    resp = get_app(pub).get(sign_uri('/api/users/%s/forms?limit=10&sort=desc' % local_user.id))
1443
    assert resp.json['err'] == 0
1444
    assert len(resp.json['data']) == 10
1445
    assert [x['form_number_raw'] for x in resp.json['data']] == [str(x) for x in range(50, 40, -1)]
1446

  
1388 1447
def test_user_forms_from_agent(pub, local_user):
1389 1448
    Role.wipe()
1390 1449
    role = Role(name='Foo bar')
wcs/api.py
618 618
        if FormDef.count() == 0:
619 619
            # early return, this avoids running a query against a missing SQL view.
620 620
            return []
621

  
621 622
        if get_publisher().is_using_postgresql() and not get_request().form.get('full') == 'on':
622 623
            from wcs import sql
623 624
            from qommon.storage import Equal
624
            user_forms = sql.AnyFormData.select([Equal('user_id', str(user.id))], order_by='receipt_time')
625
            order_by = 'receipt_time'
626
            if get_request().form.get('sort') == 'desc':
627
                order_by = '-receipt_time'
628
            user_forms = sql.AnyFormData.select(
629
                    [Equal('user_id', str(user.id))],
630
                    limit=get_request().form.get('limit'),
631
                    offset=get_request().form.get('offset'),
632
                    order_by=order_by)
625 633
        else:
626 634
            formdefs = FormDef.select()
627 635
            user_forms = []
......
629 637
                user_forms.extend(formdef.data_class().get_with_indexed_value(
630 638
                            'user_id', user.id))
631 639
            user_forms.sort(lambda x, y: cmp(x.receipt_time, y.receipt_time))
640
            if get_request().form.get('sort') == 'desc':
641
                user_forms.reverse()
632 642
        return user_forms
633 643

  
634 644
    def drafts(self):
635
-