Projet

Général

Profil

0001-pages-sub_slug-can-contain-only-slug-part-without-re.patch

Lauréline Guérin, 11 janvier 2021 15:46

Télécharger (11,1 ko)

Voir les différences:

Subject: [PATCH] pages: sub_slug can contain only slug part, without regex
 (#50000)

 combo/apps/wcs/__init__.py |  3 ++-
 combo/apps/wcs/models.py   |  3 ++-
 combo/data/models.py       |  4 ++++
 combo/profile/__init__.py  |  4 +++-
 tests/test_manager.py      | 14 +++++++++++++-
 tests/test_public.py       | 36 +++++++++++++++++++-----------------
 tests/test_search.py       | 31 +++++++++++++++++++------------
 tests/test_wcs.py          |  4 +++-
 8 files changed, 65 insertions(+), 34 deletions(-)
combo/apps/wcs/__init__.py
53 53
        for key, service in wcs_services.items():
54 54
            card_models = get_wcs_json(service, 'api/cards/@list')
55 55
            for card in card_models.get('data') or []:
56
                matching_pages = ([p for p in pages_with_sub_slug if '<%s_id>' % card['id'] in p.sub_slug])
56
                card_id = '%s_id' % card['id']
57
                matching_pages = ([p for p in pages_with_sub_slug if '<%s>' % card_id in p.sub_slug or p.sub_slug == card_id])
57 58
                if not matching_pages:
58 59
                    continue
59 60
                card_page = matching_pages[0]
combo/apps/wcs/models.py
812 812
        extra_context['title'] = self.cached_title
813 813

  
814 814
        pages_with_sub_slug = Page.objects.exclude(sub_slug='')
815
        matching_pages = ([p for p in pages_with_sub_slug if '<%s_id>' % self.carddef_reference.split(':')[1] in p.sub_slug])
815
        card_id = '%s_id' % self.carddef_reference.split(':')[1]
816
        matching_pages = ([p for p in pages_with_sub_slug if '<%s>' % card_id in p.sub_slug or p.sub_slug == card_id])
816 817
        if matching_pages:
817 818
            card_page = matching_pages[0]
818 819
            extra_context['card_page_base_url'] = card_page.get_online_url()
combo/data/models.py
99 99

  
100 100
def format_sub_slug(sub_slug):
101 101
    mapping = {}
102

  
103
    if 'P<' not in sub_slug:
104
        # simple sub_slug without regex
105
        sub_slug = '(?P<%s>[a-z0-9]+)' % sub_slug
102 106
    # search all named-groups in sub_slug
103 107
    for i, m in enumerate(re.finditer(r'P<[\w_-]+>', sub_slug)):
104 108
        # extract original name
combo/profile/__init__.py
45 45
        from combo.data.models import Page
46 46
        from django.conf import settings
47 47

  
48
        user_page = Page.objects.filter(sub_slug__contains='<name_id>').first()
48
        user_page = Page.objects.filter(sub_slug='name_id').first()
49
        if not user_page:
50
            user_page = Page.objects.filter(sub_slug__contains='<name_id>').first()
49 51
        if not user_page:
50 52
            return
51 53
        user_page_base_url = user_page.get_online_url()
tests/test_manager.py
423 423
    assert page.slug == 'foobar'
424 424
    assert page.sub_slug == '(?P<card-foo_id>[0-9]+)'
425 425

  
426
    resp = app.get('/manage/pages/%s/slug' % page.pk)
427
    resp.form['sub_slug'].value = 'card-foo_id'
428
    resp = resp.form.submit().follow()
429
    page.refresh_from_db()
430
    assert page.slug == 'foobar'
431
    assert page.sub_slug == 'card-foo_id'
432

  
426 433
    # bad regexp
427
    resp = resp.click(href='.*/slug')
434
    resp = app.get('/manage/pages/%s/slug' % page.pk)
428 435
    resp.form['sub_slug'].value = '(?P< bad group name with spaces >[0-9]+)'
429 436
    resp = resp.form.submit()
430 437
    assert resp.context['form'].errors['sub_slug'] == ['Bad Regular expression.']
431 438

  
439
    resp = app.get('/manage/pages/%s/slug' % page.pk)
440
    resp.form['sub_slug'].value = ' bad group name with spaces '
441
    resp = resp.form.submit()
442
    assert resp.context['form'].errors['sub_slug'] == ['Bad Regular expression.']
443

  
432 444

  
433 445
def test_page_edit_picture(app, admin_user):
434 446
    Page.objects.all().delete()
tests/test_public.py
972 972
        assert resp.text == 'AAwhateverBBhttp://testserver/users/whatever/blah/CC'
973 973

  
974 974
        # custom behaviour for <user_id>, it will add the user to context
975
        page2.sub_slug = '(?P<user_id>[0-9]+)'
976
        page2.save()
975
        for sub_slug in ['(?P<user_id>[0-9a-z]+)', 'user_id']:
976
            page2.sub_slug = sub_slug
977
            page2.save()
977 978

  
978
        cell.template_string = 'XX{{ selected_user.username }}YY'
979
        cell.save()
979
            cell.template_string = 'XX{{ selected_user.username }}YY'
980
            cell.save()
980 981

  
981
        resp = app.get('/users/%s/' % john_doe.id, status=200)
982
        assert 'XXjohn.doeYY' in resp.text
982
            resp = app.get('/users/%s/' % john_doe.id, status=200)
983
            assert 'XXjohn.doeYY' in resp.text
983 984

  
984
        # bad user id => no selected_user
985
        page2.sub_slug = '(?P<user_id>[0-9a-z]+)'
986
        page2.save()
987
        resp = app.get('/users/9999999/', status=200)
988
        assert 'XXYY' in resp.text
989
        resp = app.get('/users/abc/', status=200)
990
        assert 'XXYY' in resp.text
985
            # bad user id => no selected_user
986
            resp = app.get('/users/9999999/', status=200)
987
            assert 'XXYY' in resp.text
988
            resp = app.get('/users/abc/', status=200)
989
            assert 'XXYY' in resp.text
991 990

  
992 991
        # custom behaviour for <name_id>, it will add the SAML user to context
993 992
        with mock.patch('combo.profile.utils.UserSAMLIdentifier') as user_saml:
......
1012 1011
            assert 'XXYY' in resp.text
1013 1012

  
1014 1013
    # sub_slug can contain '-' (if card slug for example)
1015
    page3 = Page.objects.create(title='Card Foo', slug='foo', sub_slug='(?P<card-foo-bar_id>[0-9]+)', template_name='standard')
1016
    resp = app.get('/foo/42/', status=200)
1017
    assert resp.context['card-foo-bar_id'] == '42'
1018
    assert resp.context['card_foo_bar_id'] == '42'
1014
    page3 = Page.objects.create(title='Card Foo', slug='foo', template_name='standard')
1015
    for sub_slug in ['(?P<card-foo-bar_id>[0-9]+)', 'card-foo-bar_id']:
1016
        page3.sub_slug = sub_slug
1017
        page3.save()
1018
        resp = app.get('/foo/42/', status=200)
1019
        assert resp.context['card-foo-bar_id'] == '42'
1020
        assert resp.context['card_foo_bar_id'] == '42'
1019 1021

  
1020 1022

  
1021 1023
def test_cell_slugs(app):
tests/test_search.py
682 682
        assert len([x for x in search_engines.keys() if x.startswith('cards:')]) == 0
683 683

  
684 684
        # related page exists
685
        Page.objects.create(slug='bar', title='Bar', sub_slug='(?P<card-bar_id>[a-z0-9]+)')
686
        search_engines = engines.get_engines()
687
        assert len([x for x in search_engines.keys() if x.startswith('cards:')]) == 1
688
        assert 'cards:c21f969b:card-bar' in search_engines.keys()
689
        card_engine = search_engines['cards:c21f969b:card-bar']
690
        assert card_engine['url'] == (
691
            'http://127.0.0.1:8999/api/cards/card-bar/list/'
692
            '{% if search_service.selected_custom_view %}{{ search_service.selected_custom_view }}{% endif %}'
693
            '?{% if not search_service.without_user %}NameID={{ user_nameid }}&{% endif %}q=%(q)s')
694
        assert card_engine['hit_url_template'] == '/bar/{{ id }}'
685
        page = Page.objects.create(slug='bar', title='Bar')
686
        for sub_slug in ['(?P<card-bar_id>[a-z0-9]+)', 'card-bar_id']:
687
            page.sub_slug = sub_slug
688
            page.save()
689
            search_engines = engines.get_engines()
690
            assert len([x for x in search_engines.keys() if x.startswith('cards:')]) == 1
691
            assert 'cards:c21f969b:card-bar' in search_engines.keys()
692
            card_engine = search_engines['cards:c21f969b:card-bar']
693
            assert card_engine['url'] == (
694
                'http://127.0.0.1:8999/api/cards/card-bar/list/'
695
                '{% if search_service.selected_custom_view %}{{ search_service.selected_custom_view }}{% endif %}'
696
                '?{% if not search_service.without_user %}NameID={{ user_nameid }}&{% endif %}q=%(q)s')
697
            assert card_engine['hit_url_template'] == '/bar/{{ id }}'
695 698

  
696 699

  
697 700
def test_wcs_errors(settings, app):
......
718 721
def test_wcs_add_search_engines(mock_wcs, settings, app, admin_user):
719 722
    settings.KNOWN_SERVICES = {'wcs': {'default': {'title': 'test', 'url': 'http://127.0.0.1:8999/'}}}
720 723
    settings.TEMPLATE_VARS['is_portal_agent'] = True
721
    Page.objects.create(slug='bar', title='Bar', sub_slug='(?P<card-bar_id>[a-z0-9]+)')
724
    Page.objects.create(slug='bar', title='Bar', sub_slug='card-bar_id')
722 725
    mock_wcs.return_value = {'data': [{'id': 'card-bar', 'text': 'Card Bar'}]}
723 726

  
724 727
    page = Page.objects.create(title='One', slug='one', template_name='standard')
......
857 860
    search_engines = engines.get_engines()
858 861
    assert 'users' not in search_engines.keys()
859 862

  
860
    page = Page(slug='users', title='Users', sub_slug='(?P<name_id>[a-z0-9]+)')
863
    page = Page.objects.create(slug='users', title='Users', sub_slug='(?P<name_id>[a-z0-9]+)')
864
    search_engines = engines.get_engines()
865
    assert 'users' in search_engines.keys()
866

  
867
    page.sub_slug = 'name_id'
861 868
    page.save()
862 869
    search_engines = engines.get_engines()
863 870
    assert 'users' in search_engines.keys()
tests/test_wcs.py
1191 1191
    assert '<a href="http://127.0.0.1:8999/backoffice/data/card_model_1/13/"><span class="card-title">cc</span></a>' in result
1192 1192

  
1193 1193
    # create a page with the correct subslug
1194
    Page.objects.create(slug='foo', title='Foo', sub_slug='(?P<card_model_1_id>[a-z0-9]+)')
1194
    page = Page.objects.create(slug='foo', title='Foo', sub_slug='(?P<card_model_1_id>[a-z0-9]+)')
1195 1195

  
1196 1196
    result = cell.render(context)
1197 1197
    assert '<h2>Card Model 1</h2>' in result
......
1201 1201

  
1202 1202
    cell.carddef_reference = u'default:card_model_1:foo'
1203 1203
    cell.save()
1204
    page.sub_slug = 'card_model_1_id'
1205
    page.save()
1204 1206

  
1205 1207
    result = cell.render(context)
1206 1208
    assert '<h2>Card Model 1 - bar</h2>' in result
1207
-