Projet

Général

Profil

0001-sql-set-ordering-only-if-column-exists-50500.patch

Lauréline Guérin, 11 février 2021 15:04

Télécharger (4,07 ko)

Voir les différences:

Subject: [PATCH] sql: set ordering only if column exists (#50500)

 tests/backoffice_pages/test_custom_view.py | 68 ++++++++++++++++++++++
 wcs/sql.py                                 | 12 +++-
 2 files changed, 77 insertions(+), 3 deletions(-)
tests/backoffice_pages/test_custom_view.py
1 1
# -*- coding: utf-8 -*-
2 2
import os
3
import re
3 4

  
4 5
import pytest
5 6

  
......
467 468
    assert resp.location == 'http://example.net/backoffice/management/form-title/1/?plop'
468 469

  
469 470

  
471
def test_backoffice_custom_view_sort_field(pub):
472
    if not pub.is_using_postgresql():
473
        pytest.skip('this requires SQL')
474
        return
475

  
476
    create_superuser(pub)
477

  
478
    FormDef.wipe()
479
    pub.custom_view_class.wipe()
480
    formdef = FormDef()
481
    formdef.name = 'form title'
482
    formdef.fields = [
483
        fields.ItemField(
484
            id='1',
485
            label='field 1',
486
            type='item',
487
            items=['foo', 'bar', 'baz'],
488
            display_locations=['validation', 'summary', 'listings'],
489
        ),
490
    ]
491
    formdef.workflow_roles = {'_receiver': 1}
492
    formdef.store()
493

  
494
    formdef.data_class().wipe()
495
    formdata = formdef.data_class()()
496
    formdata.data = {'1': 'foo', '1_display': 'foo'}
497
    formdata.jump_status('new')
498
    formdata.store()
499
    formdata = formdef.data_class()()
500
    formdata.data = {'1': 'bar', '1_display': 'bar'}
501
    formdata.jump_status('new')
502
    formdata.store()
503
    formdata = formdef.data_class()()
504
    formdata.data = {'1': 'baz', '1_display': 'baz'}
505
    formdata.jump_status('new')
506
    formdata.store()
507

  
508
    custom_view = pub.custom_view_class()
509
    custom_view.title = 'shared custom test view'
510
    custom_view.formdef = formdef
511
    custom_view.visibility = 'any'
512
    custom_view.columns = {'list': [{'id': 'id'}]}
513
    custom_view.filters = {}
514
    custom_view.order_by = 'f1'
515
    custom_view.is_default = True
516
    custom_view.store()
517

  
518
    app = login(get_app(pub))
519
    resp = app.get('/backoffice/management/form-title/shared-custom-test-view/')
520
    assert resp.text.count('<tr') == 4
521
    # bar, baz, foo
522
    assert re.findall(r'<a href="(\d)/">1-(\d)</a>', resp.text) == [('2', '2'), ('3', '3'), ('1', '1')]
523

  
524
    custom_view.order_by = '-f1'
525
    custom_view.store()
526
    resp = app.get('/backoffice/management/form-title/shared-custom-test-view/')
527
    assert resp.text.count('<tr') == 4
528
    # foo, baz, bar
529
    assert re.findall(r'<a href="(\d)/">1-(\d)</a>', resp.text) == [('1', '1'), ('3', '3'), ('2', '2')]
530

  
531
    custom_view.order_by = 'unknown'
532
    custom_view.store()
533
    # unknown sort field, ignore it
534
    resp = app.get('/backoffice/management/form-title/shared-custom-test-view/')
535
    assert resp.text.count('<tr') == 4
536

  
537

  
470 538
def test_carddata_custom_view(pub):
471 539
    user = create_user(pub)
472 540

  
wcs/sql.py
1516 1516
        # [SEC_ORDER] security note: it is not possible to use
1517 1517
        # prepared statements for ORDER BY clauses, therefore input
1518 1518
        # is controlled beforehand (see misc.get_order_by_or_400).
1519
        direction = 'ASC'
1519 1520
        if order_by.startswith('-'):
1520 1521
            order_by = order_by[1:]
1521
            return ' ORDER BY %s DESC' % order_by.replace('-', '_')
1522
        else:
1523
            return ' ORDER BY %s' % order_by.replace('-', '_')
1522
            direction = 'DESC'
1523
        order_by = order_by.replace('-', '_')
1524

  
1525
        fields = [x[0] for x in cls._table_static_fields] + cls.get_data_fields()
1526
        if order_by not in fields:
1527
            return ''
1528

  
1529
        return ' ORDER BY %s %s' % (order_by, direction)
1524 1530

  
1525 1531
    @classmethod
1526 1532
    @guard_postgres
1527
-