Projet

Général

Profil

0001-search-add-option-to-set-custom-title-for-subpages-s.patch

Frédéric Péters, 14 juillet 2020 12:38

Télécharger (9,03 ko)

Voir les différences:

Subject: [PATCH] search: add option to set custom title for subpages search
 engine (#43888)

 combo/apps/search/forms.py         |  3 ++-
 combo/apps/search/manager_views.py | 13 ++++++-----
 combo/apps/search/models.py        |  7 ++++++
 tests/test_search.py               | 35 ++++++++++++++++++++++++++----
 4 files changed, 48 insertions(+), 10 deletions(-)
combo/apps/search/forms.py
44 44
        return option_dict
45 45

  
46 46

  
47
class SelectPageForm(forms.ModelForm):
47
class TextEngineSettingsForm(forms.ModelForm):
48 48
    selected_page = forms.ModelChoiceField(
49 49
        label=_('Page'),
50 50
        required=False,
......
52 52
        help_text=_("Select a page to limit the search on this page and sub pages contents."),
53 53
        widget=SelectWithDisabled(),
54 54
    )
55
    title = forms.CharField(label=_('Custom Title'), required=False)
55 56

  
56 57
    class Meta:
57 58
        model = SearchCell
combo/apps/search/manager_views.py
21 21
from django.urls import reverse
22 22
from django.utils.translation import ugettext_lazy as _
23 23

  
24
from combo.apps.search.forms import SelectPageForm
24
from combo.apps.search.forms import TextEngineSettingsForm
25 25
from combo.apps.search.models import SearchCell
26 26
from combo.data.models import PageSnapshot
27 27

  
......
29 29
def page_search_cell_add_engine(request, page_pk, cell_reference, engine_slug):
30 30
    cell = get_object_or_404(SearchCell, pk=cell_reference.split('-')[1], page=page_pk)
31 31

  
32
    def add_slug(slug):
32
    def add_slug(slug, **options):
33 33
        if slug in cell.available_engines or slug.startswith('_text_page'):
34 34
            if not cell._search_services or not cell._search_services.get('data'):
35 35
                cell._search_services = {'data': []}
36
            if not cell._search_services.get('options'):
37
                cell._search_services['options'] = {}
36 38
            cell._search_services['data'].append(slug)
39
            cell._search_services['options'][slug] = options
37 40
            cell.save()
38 41
            PageSnapshot.take(cell.page, request=request, comment=_('changed cell "%s"') % cell)
39 42
        return HttpResponseRedirect('%s#cell-%s' % (
......
45 48
        return add_slug(engine_slug)
46 49

  
47 50
    if request.method == 'POST':
48
        form = SelectPageForm(instance=cell, data=request.POST)
51
        form = TextEngineSettingsForm(instance=cell, data=request.POST)
49 52
        if form.is_valid():
50 53
            slug = '_text'
51 54
            if form.cleaned_data['selected_page'] is not None:
52 55
                slug = '_text_page_%s' % form.cleaned_data['selected_page'].slug
53
            return add_slug(slug)
56
            return add_slug(slug, title=form.cleaned_data['title'])
54 57
    else:
55
        form = SelectPageForm(instance=cell)
58
        form = TextEngineSettingsForm(instance=cell)
56 59
    context = {
57 60
        'form': form,
58 61
        'cell': cell,
combo/apps/search/models.py
85 85
                service = engines.get('_text')
86 86
            if service and (service.get('url') or service.get('function')):
87 87
                service['slug'] = service_slug
88
                service['options'] = self._search_services.get('options', {}).get(service_slug)
88 89
                services.append(service)
89 90
        return services
90 91

  
......
168 169
            service_label = service.get('label')
169 170
            if pages:
170 171
                service_label = _('Page "%(page)s" and sub pages Contents') % {'page': pages[0].title}
172
            try:
173
                # optional label defined with engine
174
                if service['options']['title']:
175
                    service_label = service['options']['title']
176
            except (KeyError, TypeError):
177
                pass
171 178
            context = {
172 179
                'cell': cell,
173 180
                'results': results,
tests/test_search.py
449 449
    cell.save()
450 450
    resp = app.get('/ajax/search/%s/_text_page_sub-second-page/?q=baz' % cell.pk, status=200)
451 451
    assert resp.text.count('<li') == 2
452
    # search with custom title
453
    cell._search_services = {
454
        'data': ['_text', '_text_page_sub-second-page'],
455
        'options': {'_text_page_sub-second-page': {'title': 'Custom Title'}},
456
    }
457
    cell.save()
458
    resp = app.get('/ajax/search/%s/_text_page_sub-second-page/?q=baz' % cell.pk, status=200)
459
    assert 'Custom Title' in resp.text
460
    assert resp.text.count('<li') == 2
452 461

  
453 462

  
454 463
def test_search_external_links(app):
......
519 528
    resp = app.get('/manage/pages/%s/' % page.pk)
520 529
    resp = resp.click(href='.*/search_searchcell-%s/engine/search_tmpl/add/' % cell.pk)
521 530
    cell.refresh_from_db()
522
    assert cell._search_services == {'data': ['_text', 'search1', 'search_tmpl']}
531
    assert cell._search_services['data'] == ['_text', 'search1', 'search_tmpl']
523 532
    resp = app.get('/manage/pages/%s/' % page.pk)
524 533
    # '_text' is always available
525 534
    assert '/manage/search/pages/%s/cell/search_searchcell-%s/engine/_text/add/' % (page.pk, cell.pk) in resp.text
......
536 545
    assert resp.status_int == 302
537 546
    assert resp.location.endswith('/manage/pages/%s/#cell-%s' % (page.pk, cell.get_reference()))
538 547
    cell.refresh_from_db()
539
    assert cell._search_services == {'data': ['search1', 'search_tmpl']}
548
    assert cell._search_services['data'] == ['search1', 'search_tmpl']
540 549

  
541 550
    settings.COMBO_SEARCH_SERVICES = {}
542 551
    # check there's no crash if search engines are removed from config
......
555 564
    assert resp.status_int == 302
556 565
    assert resp.location.endswith('/manage/pages/%s/#cell-%s' % (page.pk, cell.get_reference()))
557 566
    cell.refresh_from_db()
558
    assert cell._search_services == {'data': ['search1', 'search_tmpl', '_text']}
567
    assert cell._search_services['data'] == ['search1', 'search_tmpl', '_text']
559 568
    resp = app.get('/manage/pages/%s/' % page.pk)
560 569
    assert '/manage/search/pages/%s/cell/search_searchcell-%s/engine/_text/add/' % (page.pk, cell.pk) in resp.text
561 570
    assert '/manage/search/pages/%s/cell/search_searchcell-%s/engine/_text/delete/' % (page.pk, cell.pk) in resp.text
......
566 575
    assert resp.status_int == 302
567 576
    assert resp.location.endswith('/manage/pages/%s/#cell-%s' % (page.pk, cell.get_reference()))
568 577
    cell.refresh_from_db()
569
    assert cell._search_services == {'data': ['search1', 'search_tmpl', '_text', '_text_page_one']}
578
    assert cell._search_services['data'] == ['search1', 'search_tmpl', '_text', '_text_page_one']
570 579
    resp = app.get('/manage/pages/%s/' % page.pk)
571 580
    assert '/manage/search/pages/%s/cell/search_searchcell-%s/engine/_text/add/' % (page.pk, cell.pk) in resp.text
572 581
    assert '/manage/search/pages/%s/cell/search_searchcell-%s/engine/_text/delete/' % (page.pk, cell.pk) in resp.text
573 582
    assert '/manage/search/pages/%s/cell/search_searchcell-%s/engine/_text_page_one/delete/' % (page.pk, cell.pk) in resp.text
574 583

  
584
    # remove engine
585
    resp = resp.click(href='.*/search_searchcell-%s/engine/_text_page_one/delete/' % cell.pk)
586
    assert resp.status_int == 302
587
    assert resp.location.endswith('/manage/pages/%s/#cell-%s' % (page.pk, cell.get_reference()))
588
    cell.refresh_from_db()
589

  
590
    # add engine on page and sub pages, with a custom title
591
    resp = app.get('/manage/pages/%s/' % page.pk)
592
    resp = resp.click(href='.*/search_searchcell-%s/engine/_text/add/' % cell.pk)
593
    resp.form['selected_page'] = page.pk
594
    resp.form['title'] = 'Custom Title'
595
    resp = resp.form.submit('submit')
596
    assert resp.status_int == 302
597
    assert resp.location.endswith('/manage/pages/%s/#cell-%s' % (page.pk, cell.get_reference()))
598
    cell.refresh_from_db()
599
    assert cell._search_services['data'] == ['search1', 'search_tmpl', '_text', '_text_page_one']
600
    assert cell._search_services['options']['_text_page_one'] == {'title': 'Custom Title'}
601

  
575 602

  
576 603
def test_manager_search_cell_order(settings, app, admin_user):
577 604
    settings.COMBO_SEARCH_SERVICES = SEARCH_SERVICES
578
-