Projet

Général

Profil

0001-search-title-for-search-cell-57323.patch

Lauréline Guérin, 01 octobre 2021 16:21

Télécharger (13,1 ko)

Voir les différences:

Subject: [PATCH] search: title for search cell (#57323)

 combo/apps/search/forms.py                    |   2 +-
 combo/apps/search/migrations/0011_title.py    |  16 ++
 combo/apps/search/models.py                   |   1 +
 .../search/templates/combo/search-cell.html   |   3 +
 tests/test_search.py                          | 185 +++++++++---------
 5 files changed, 113 insertions(+), 94 deletions(-)
 create mode 100644 combo/apps/search/migrations/0011_title.py
combo/apps/search/forms.py
27 27
class SearchCellForm(forms.ModelForm):
28 28
    class Meta:
29 29
        model = SearchCell
30
        fields = ('autofocus', 'input_placeholder')
30
        fields = ('title', 'autofocus', 'input_placeholder')
31 31

  
32 32

  
33 33
class SelectWithDisabled(forms.Select):
combo/apps/search/migrations/0011_title.py
1
from django.db import migrations, models
2

  
3

  
4
class Migration(migrations.Migration):
5

  
6
    dependencies = [
7
        ('search', '0010_searchcell_template_name'),
8
    ]
9

  
10
    operations = [
11
        migrations.AddField(
12
            model_name='searchcell',
13
            name='title',
14
            field=models.CharField(blank=True, max_length=150, verbose_name='Title'),
15
        ),
16
    ]
combo/apps/search/models.py
53 53
    exclude_from_search = True
54 54

  
55 55
    _search_services = JSONField(_('Search Services'), default=dict, blank=True)
56
    title = models.CharField(_('Title'), max_length=150, blank=True)
56 57
    autofocus = models.BooleanField(_('Autofocus'), default=False)
57 58
    input_placeholder = models.CharField(_('Placeholder'), max_length=64, default="", blank=True)
58 59

  
combo/apps/search/templates/combo/search-cell.html
1 1
{% load i18n %}
2 2
{% block cell-content %}
3
{% if cell.title %}
4
<h2>{{ cell.title }}</h2>
5
{% endif %}
3 6

  
4 7
{% block search-form %}
5 8
<form id="combo-search-form-{{ cell.pk }}" class="combo-search-form">
tests/test_search.py
57 57
        settings.COMBO_SEARCH_SERVICES = {}
58 58

  
59 59

  
60
def test_search_cell(app):
60
def test_search_cell(settings, app):
61
    settings.COMBO_SEARCH_SERVICES = SEARCH_SERVICES
61 62
    page = Page(title='Search', slug='search_page', template_name='standard')
62 63
    page.save()
63 64

  
......
69 70
    # unknown cell pk
70 71
    resp = app.get('/ajax/search/0/search1/?q=foo', status=404)
71 72

  
72
    with SearchServices(SEARCH_SERVICES):
73
        resp = cell.render({})
74
        assert 'input' in resp
75
        assert 'id="combo-search-input-%s"' % cell.pk in resp
76
        assert 'autofocus' not in resp
77
        assert 'placeholder="my placeholder"' in resp
73
    resp = cell.render({})
74
    assert 'input' in resp
75
    assert 'id="combo-search-input-%s"' % cell.pk in resp
76
    assert 'autofocus' not in resp
77
    assert 'placeholder="my placeholder"' in resp
78 78

  
79
        cell.autofocus = True
80
        cell.save()
81
        resp = cell.render({})
82
        assert 'autofocus' in resp
79
    cell.autofocus = True
80
    cell.save()
81
    resp = cell.render({})
82
    assert 'h2' not in resp
83
    assert 'autofocus' in resp
83 84

  
84
        cell.slug = 'var-name'
85
        context = {'request': RequestFactory().get('/?q_var_name=searchme')}
86
        resp = cell.render(context)
87
        assert "$input.val('searchme');" in resp
85
    cell.slug = 'var-name'
86
    cell.title = 'Custom Title'
87
    context = {'request': RequestFactory().get('/?q_var_name=searchme')}
88
    resp = cell.render(context)
89
    assert '<h2>Custom Title</h2>' in resp
90
    assert "$input.val('searchme');" in resp
88 91

  
89
        with mock.patch('combo.apps.search.models.requests.get') as requests_get:
92
    with mock.patch('combo.apps.search.models.requests.get') as requests_get:
90 93

  
91
            response = {'err': 0, 'data': []}
92
            mock_json = mock.Mock()
93
            mock_json.json.return_value = response
94
            requests_get.return_value = mock_json
95
            resp = app.get('/ajax/search/%s/search1/?q=foo' % cell.pk, status=200)
96
            assert requests_get.call_args[0][0] == 'http://www.example.net/search/?q=foo'
97
            assert '<li>' not in resp.text
98
            assert 'no result found' in resp.text
99

  
100
            resp = app.get('/ajax/search/%s/search1/?q=foo%%23bar' % cell.pk, status=200)
101
            assert requests_get.call_args[0][0] == 'http://www.example.net/search/?q=foo%23bar'
102
            assert '<li>' not in resp.text
103
            assert 'no result found' in resp.text
104

  
105
            response['data'] = [{'url': 'http://test', 'text': 'barbarbar'}]
106
            resp = app.get('/ajax/search/%s/search1/?q=foo' % cell.pk, status=200)
107
            assert resp.text.count('<li>') == 1
108
            assert '<li><a href="http://test">barbarbar</a>' in resp.text
109
            assert 'no result found' not in resp.text
94
        response = {'err': 0, 'data': []}
95
        mock_json = mock.Mock()
96
        mock_json.json.return_value = response
97
        requests_get.return_value = mock_json
98
        resp = app.get('/ajax/search/%s/search1/?q=foo' % cell.pk, status=200)
99
        assert requests_get.call_args[0][0] == 'http://www.example.net/search/?q=foo'
100
        assert '<li>' not in resp.text
101
        assert 'no result found' in resp.text
102

  
103
        resp = app.get('/ajax/search/%s/search1/?q=foo%%23bar' % cell.pk, status=200)
104
        assert requests_get.call_args[0][0] == 'http://www.example.net/search/?q=foo%23bar'
105
        assert '<li>' not in resp.text
106
        assert 'no result found' in resp.text
107

  
108
        response['data'] = [{'url': 'http://test', 'text': 'barbarbar'}]
109
        resp = app.get('/ajax/search/%s/search1/?q=foo' % cell.pk, status=200)
110
        assert resp.text.count('<li>') == 1
111
        assert '<li><a href="http://test">barbarbar</a>' in resp.text
112
        assert 'no result found' not in resp.text
110 113

  
111
            response['data'] = [
112
                {'url': 'http://test', 'text': 'barbarbar', 'description': 'this is <b>html</b>'}
113
            ]
114
            resp = app.get('/ajax/search/%s/search1/?q=foo' % cell.pk, status=200)
115
            assert resp.text.count('<li>') == 1
116
            assert '<li><a href="http://test">barbarbar</a>' in resp.text
117
            assert 'this is <b>html</b>' in resp.text
118
            assert 'no result found' not in resp.text
114
        response['data'] = [{'url': 'http://test', 'text': 'barbarbar', 'description': 'this is <b>html</b>'}]
115
        resp = app.get('/ajax/search/%s/search1/?q=foo' % cell.pk, status=200)
116
        assert resp.text.count('<li>') == 1
117
        assert '<li><a href="http://test">barbarbar</a>' in resp.text
118
        assert 'this is <b>html</b>' in resp.text
119
        assert 'no result found' not in resp.text
119 120

  
120
            resp = app.get('/ajax/search/%s/search1/?q=' % cell.pk, status=200)
121
            assert '<li>' not in resp.text
122
            assert 'no result found' not in resp.text
121
        resp = app.get('/ajax/search/%s/search1/?q=' % cell.pk, status=200)
122
        assert '<li>' not in resp.text
123
        assert 'no result found' not in resp.text
123 124

  
124
            cell._search_services = {'data': ['search_alternate_key']}
125
            cell.save()
126
            response = {'results': [{'url': 'http://test', 'text': 'barbarbar'}]}
127
            mock_json.json.return_value = response
128
            resp = app.get('/ajax/search/%s/search_alternate_key/?q=foo' % cell.pk, status=200)
129
            assert resp.text.count('<li>') == 1
130
            assert '<li><a href="http://test">barbarbar</a>' in resp.text
131

  
132
            # search engine does not return valid JSON
133
            class FakedResponse(mock.Mock):
134
                def json(self):
135
                    return json.loads(self.content)
136

  
137
            requests_get.return_value = FakedResponse(content='notjson', status_code=200)
138
            resp = app.get('/ajax/search/%s/search_alternate_key/?q=bar' % cell.pk, status=200)
139
            assert requests_get.call_args[0][0] == 'http://www.example.net/search/?q=bar'
140
            assert '<li>' not in resp.text
141
            assert 'no result found' in resp.text
142
            requests_get.return_value = FakedResponse(content='500withbadjson', status_code=500)
143
            resp = app.get('/ajax/search/%s/search_alternate_key/?q=foo' % cell.pk, status=200)
144
            assert requests_get.call_args[0][0] == 'http://www.example.net/search/?q=foo'
145
            assert '<li>' not in resp.text
146
            assert 'no result found' in resp.text
147

  
148
        with override_settings(TEMPLATE_VARS=TEMPLATE_VARS):
149
            cell._search_services = {'data': ['search_tmpl']}
150
            cell.save()
151
            with mock.patch('combo.apps.search.models.requests.get') as requests_get:
152
                response = {'err': 0, 'data': []}
153
                mock_json = mock.Mock()
154
                mock_json.json.return_value = response
155
                requests_get.return_value = mock_json
156
                resp = app.get('/ajax/search/%s/search_tmpl/?q=foo' % cell.pk, status=200)
157
                assert requests_get.call_args[0][0] == 'http://search.example.net/?q=foo'
158

  
159
                # TEMPLATE_VARS are accessible in template
160
                cell.slug = 'searchfoo'
161
                cell.save()
162
                templates_settings = [settings.TEMPLATES[0].copy()]
163
                templates_settings[0]['DIRS'] = [
164
                    '%s/templates-1' % os.path.abspath(os.path.dirname(__file__))
165
                ]
166
                with override_settings(TEMPLATES=templates_settings):
167
                    resp = app.get('/ajax/search/%s/search_tmpl/?q=bar' % cell.pk, status=200)
168
                    assert requests_get.call_args[0][0] == 'http://search.example.net/?q=bar'
169
                    assert 'searchfoo results.data=[]' in resp.text
170
                    assert 'search_url=http://search.example.net/' in resp.text
125
        cell._search_services = {'data': ['search_alternate_key']}
126
        cell.save()
127
        response = {'results': [{'url': 'http://test', 'text': 'barbarbar'}]}
128
        mock_json.json.return_value = response
129
        resp = app.get('/ajax/search/%s/search_alternate_key/?q=foo' % cell.pk, status=200)
130
        assert resp.text.count('<li>') == 1
131
        assert '<li><a href="http://test">barbarbar</a>' in resp.text
132

  
133
        # search engine does not return valid JSON
134
        class FakedResponse(mock.Mock):
135
            def json(self):
136
                return json.loads(self.content)
137

  
138
        requests_get.return_value = FakedResponse(content='notjson', status_code=200)
139
        resp = app.get('/ajax/search/%s/search_alternate_key/?q=bar' % cell.pk, status=200)
140
        assert requests_get.call_args[0][0] == 'http://www.example.net/search/?q=bar'
141
        assert '<li>' not in resp.text
142
        assert 'no result found' in resp.text
143
        requests_get.return_value = FakedResponse(content='500withbadjson', status_code=500)
144
        resp = app.get('/ajax/search/%s/search_alternate_key/?q=foo' % cell.pk, status=200)
145
        assert requests_get.call_args[0][0] == 'http://www.example.net/search/?q=foo'
146
        assert '<li>' not in resp.text
147
        assert 'no result found' in resp.text
148

  
149
    settings.TEMPLATE_VARS = TEMPLATE_VARS
150
    cell._search_services = {'data': ['search_tmpl']}
151
    cell.save()
152
    with mock.patch('combo.apps.search.models.requests.get') as requests_get:
153
        response = {'err': 0, 'data': []}
154
        mock_json = mock.Mock()
155
        mock_json.json.return_value = response
156
        requests_get.return_value = mock_json
157
        resp = app.get('/ajax/search/%s/search_tmpl/?q=foo' % cell.pk, status=200)
158
        assert requests_get.call_args[0][0] == 'http://search.example.net/?q=foo'
159

  
160
        # TEMPLATE_VARS are accessible in template
161
        cell.slug = 'searchfoo'
162
        cell.save()
163
        templates_settings = [settings.TEMPLATES[0].copy()]
164
        templates_settings[0]['DIRS'] = ['%s/templates-1' % os.path.abspath(os.path.dirname(__file__))]
165
        with override_settings(TEMPLATES=templates_settings):
166
            resp = app.get('/ajax/search/%s/search_tmpl/?q=bar' % cell.pk, status=200)
167
            assert requests_get.call_args[0][0] == 'http://search.example.net/?q=bar'
168
            assert 'searchfoo results.data=[]' in resp.text
169
            assert 'search_url=http://search.example.net/' in resp.text
171 170

  
172 171

  
173 172
def test_search_cell_urlsplit(settings, app):
174
-