Projet

Général

Profil

0001-search-add-a-free-hint-text-to-search-cell-40993.patch

Nicolas Roche, 08 avril 2020 18:49

Télécharger (7,04 ko)

Voir les différences:

Subject: [PATCH] search: add a free hint text to search cell (#40993)

 combo/apps/search/forms.py                    |  3 ++-
 .../search/migrations/0008_searchcell_hint.py | 20 +++++++++++++++++++
 combo/apps/search/models.py                   |  1 +
 .../search/templates/combo/search-cell.html   |  3 +++
 tests/test_search.py                          |  7 +++++++
 5 files changed, 33 insertions(+), 1 deletion(-)
 create mode 100644 combo/apps/search/migrations/0008_searchcell_hint.py
combo/apps/search/forms.py
20 20

  
21 21
from . import engines
22 22
from .models import SearchCell
23 23

  
24 24

  
25 25
class SearchCellForm(forms.ModelForm):
26 26
    class Meta:
27 27
        model = SearchCell
28
        fields = ('_search_services', 'autofocus')
28
        fields = ('_search_services', 'autofocus', 'hint')
29 29

  
30 30
    def __init__(self, *args, **kwargs):
31 31
        super(SearchCellForm, self).__init__(*args, **kwargs)
32 32
        options = [(x, engines.get_engines()[x]['label']) for x in engines.get_engines().keys()]
33 33
        self.fields['_search_services'].widget = MultiSortWidget(choices=options,
34 34
                                                                 with_checkboxes=True)
35
        self.fields['hint'].widget = forms.Textarea({'cols': '20', 'rows': '2'})
combo/apps/search/migrations/0008_searchcell_hint.py
1
# -*- coding: utf-8 -*-
2
# Generated by Django 1.11.18 on 2020-04-08 16:33
3
from __future__ import unicode_literals
4

  
5
from django.db import migrations, models
6

  
7

  
8
class Migration(migrations.Migration):
9

  
10
    dependencies = [
11
        ('search', '0007_french_fts'),
12
    ]
13

  
14
    operations = [
15
        migrations.AddField(
16
            model_name='searchcell',
17
            name='hint',
18
            field=models.CharField(blank=True, default='', max_length=512, verbose_name='Hint'),
19
        ),
20
    ]
combo/apps/search/models.py
39 39

  
40 40
@register_cell_class
41 41
class SearchCell(CellBase):
42 42
    template_name = 'combo/search-cell.html'
43 43
    manager_form_template = 'combo/manager/search-cell-form.html'
44 44

  
45 45
    _search_services = JSONField(_('Search Services'), default=dict, blank=True)
46 46
    autofocus = models.BooleanField(_('Autofocus'), default=False)
47
    hint = models.CharField(_('Hint'), max_length=512, default="", blank=True)
47 48

  
48 49
    class Meta:
49 50
        verbose_name = _('Search')
50 51

  
51 52
    def is_visible(self, **kwargs):
52 53
        if not self.search_services:
53 54
            return False
54 55
        return super(SearchCell, self).is_visible(**kwargs)
combo/apps/search/templates/combo/search-cell.html
1 1
{% load i18n %}
2 2
{% block cell-content %}
3 3

  
4 4
{% block search-form %}
5 5
<form id="combo-search-form-{{ cell.pk }}" class="combo-search-form">
6 6
  <input type="search" name="q" autocomplete="off" id="combo-search-input-{{ cell.pk }}" class="combo-search-input" {% if cell.autofocus %}autofocus {% endif %}/>
7 7
  <button class="submit-button" aria-label="{% trans 'Search' %}">{% block submit-content %}{% trans 'Search' %}{% endblock %}</button>
8 8
</form>
9
<div id="combo-search-hint-{{ cell.pk }}" class="combo-search-hint, hint">
10
  {{ cell.hint }}
11
</div>
9 12
{% endblock %}
10 13

  
11 14
{% block search-results %}
12 15
{% for search_service in cell.search_services %}
13 16
<div id="combo-search-results-{{ cell.pk }}-{{ forloop.counter }}" class="combo-search-results combo-search-results-{{ search_service.slug }}"></div>
14 17
{% endfor %}
15 18
{% endblock %}
16 19

  
tests/test_search.py
56 56

  
57 57
def test_search_cell(app):
58 58
    with SearchServices(SEARCH_SERVICES):
59 59
        page = Page(title='Search', slug='search_page', template_name='standard')
60 60
        page.save()
61 61

  
62 62
        cell = SearchCell(page=page, placeholder='content', order=0)
63 63
        cell._search_services = {'data': ['search1']}
64
        cell.hint = 'my help message'
64 65
        cell.save()
65 66

  
66 67
        resp = cell.render({})
67 68
        assert 'input' in resp
68 69
        assert 'id="combo-search-input-%s"' % cell.pk in resp
69 70
        assert 'autofocus' not in resp
71
        assert 'id="combo-search-hint-%s"' % cell.pk in resp
72
        assert 'my help message' in resp
70 73

  
71 74
        cell.autofocus = True
72 75
        cell.save()
73 76
        resp = cell.render({})
74 77
        assert 'autofocus' in resp
75 78

  
76 79
        cell.slug = 'var-name'
77 80
        context = {'request': RequestFactory().get('/?q_var_name=searchme')}
......
370 373
            assert len(resp.form['c%s-_search_services' % cells[0].get_reference()].options) == 4
371 374
            # simulate reordering of options
372 375
            resp.form['c%s-_search_services' % cells[0].get_reference()].options = [
373 376
                    (u'search_tmpl', False, u'Search with template'),
374 377
                    (u'search_alternate_key', False, u'Search with alternate key'),
375 378
                    (u'_text', False, u'Page Contents'),
376 379
                    (u'search1', False, u'Search 1')]
377 380
            resp.form['c%s-_search_services' % cells[0].get_reference()].value = ['search_tmpl', '_text']
381
            resp.form['c%s-hint' % cells[0].get_reference()] = 'my help message'
378 382
            resp = resp.form.submit()
379 383
            assert resp.status_int == 302
380 384

  
381 385
            # check selected engines are selected and the first items of the list
382 386
            resp = app.get('/manage/pages/%s/' % page.id)
383 387
            assert set(resp.form['c%s-_search_services' % cells[0].get_reference()].value) == set(['search_tmpl', '_text'])
384 388
            assert resp.form['c%s-_search_services' % cells[0].get_reference()].options[0][0] == 'search_tmpl'
385 389
            assert resp.form['c%s-_search_services' % cells[0].get_reference()].options[1][0] == '_text'
386 390

  
391
            # check hint
392
            resp.form['c%s-hint' % cells[0].get_reference()] == 'my help message'
393

  
387 394
        # check there's no crash if search engines are removed from config
388 395
        resp = app.get('/manage/pages/%s/' % page.id)
389 396
        assert resp.form['c%s-_search_services' % cells[0].get_reference()].value == ['_text']
390 397

  
391 398

  
392 399
def test_manager_waiting_index_message(app, admin_user):
393 400
    Page.objects.all().delete()
394 401
    page = Page(title='One', slug='one', template_name='standard')
395
-