Projet

Général

Profil

0001-search-add-a-placeholder-attribute-to-search-input-c.patch

Nicolas Roche, 08 avril 2020 19:37

Télécharger (7,2 ko)

Voir les différences:

Subject: [PATCH] search: add a placeholder attribute to search input cell
 (#40993)

 combo/apps/search/forms.py                    |  2 +-
 .../0008_searchcell_input_placeholder.py      | 20 +++++++++++++++++++
 combo/apps/search/models.py                   |  1 +
 .../search/templates/combo/search-cell.html   |  2 +-
 tests/test_search.py                          |  6 ++++++
 5 files changed, 29 insertions(+), 2 deletions(-)
 create mode 100644 combo/apps/search/migrations/0008_searchcell_input_placeholder.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', 'input_placeholder')
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)
combo/apps/search/migrations/0008_searchcell_input_placeholder.py
1
# -*- coding: utf-8 -*-
2
# Generated by Django 1.11.18 on 2020-04-08 17:22
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='input_placeholder',
18
            field=models.CharField(blank=True, default='', max_length=64, verbose_name='Placeholder'),
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
    input_placeholder = models.CharField(_('Placeholder'), max_length=64, 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
  <input type="search" name="q" autocomplete="off" id="combo-search-input-{{ cell.pk }}" class="combo-search-input" {% if cell.autofocus %}autofocus {% endif %}/>
6
  <input type="search" name="q" autocomplete="off" id="combo-search-input-{{ cell.pk }}" class="combo-search-input" {% if cell.autofocus %}autofocus {% endif %}{% if cell.input_placeholder %}placeholder="{{ cell.input_placeholder }}" {% endif %}/>
7 7
  <button class="submit-button" aria-label="{% trans 'Search' %}">{% block submit-content %}{% trans 'Search' %}{% endblock %}</button>
8 8
</form>
9 9
{% endblock %}
10 10

  
11 11
{% block search-results %}
12 12
{% for search_service in cell.search_services %}
13 13
<div id="combo-search-results-{{ cell.pk }}-{{ forloop.counter }}" class="combo-search-results combo-search-results-{{ search_service.slug }}"></div>
14 14
{% endfor %}
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.input_placeholder = 'my placeholder'
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 'placeholder="my placeholder"' in resp
70 72

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

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

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

  
390
            # check placeholder
391
            resp.form['c%s-input_placeholder' % cells[0].get_reference()] == 'my placeholder'
392

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

  
391 397

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