search: title for search cell (#57323)
gitea-wip/combo/pipeline/head There was a failure building this commit Details
gitea/combo/pipeline/head Build started... Details

This commit is contained in:
Lauréline Guérin 2021-10-01 16:20:13 +02:00
parent 8e577d006f
commit bee691aa15
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
5 changed files with 114 additions and 95 deletions

View File

@ -27,7 +27,7 @@ from .models import SearchCell
class SearchCellForm(forms.ModelForm):
class Meta:
model = SearchCell
fields = ('autofocus', 'input_placeholder')
fields = ('title', 'autofocus', 'input_placeholder')
class SelectWithDisabled(forms.Select):

View File

@ -0,0 +1,16 @@
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('search', '0010_searchcell_template_name'),
]
operations = [
migrations.AddField(
model_name='searchcell',
name='title',
field=models.CharField(blank=True, max_length=150, verbose_name='Title'),
),
]

View File

@ -53,6 +53,7 @@ class SearchCell(CellBase):
exclude_from_search = True
_search_services = JSONField(_('Search Services'), default=dict, blank=True)
title = models.CharField(_('Title'), max_length=150, blank=True)
autofocus = models.BooleanField(_('Autofocus'), default=False)
input_placeholder = models.CharField(_('Placeholder'), max_length=64, default="", blank=True)

View File

@ -1,5 +1,8 @@
{% load i18n %}
{% block cell-content %}
{% if cell.title %}
<h2>{{ cell.title }}</h2>
{% endif %}
{% block search-form %}
<form id="combo-search-form-{{ cell.pk }}" class="combo-search-form">

View File

@ -57,7 +57,8 @@ class SearchServices:
settings.COMBO_SEARCH_SERVICES = {}
def test_search_cell(app):
def test_search_cell(settings, app):
settings.COMBO_SEARCH_SERVICES = SEARCH_SERVICES
page = Page(title='Search', slug='search_page', template_name='standard')
page.save()
@ -69,7 +70,6 @@ def test_search_cell(app):
# unknown cell pk
resp = app.get('/ajax/search/0/search1/?q=foo', status=404)
with SearchServices(SEARCH_SERVICES):
resp = cell.render({})
assert 'input' in resp
assert 'id="combo-search-input-%s"' % cell.pk in resp
@ -79,11 +79,14 @@ def test_search_cell(app):
cell.autofocus = True
cell.save()
resp = cell.render({})
assert 'h2' not in resp
assert 'autofocus' in resp
cell.slug = 'var-name'
cell.title = 'Custom Title'
context = {'request': RequestFactory().get('/?q_var_name=searchme')}
resp = cell.render(context)
assert '<h2>Custom Title</h2>' in resp
assert "$input.val('searchme');" in resp
with mock.patch('combo.apps.search.models.requests.get') as requests_get:
@ -108,9 +111,7 @@ def test_search_cell(app):
assert '<li><a href="http://test">barbarbar</a>' in resp.text
assert 'no result found' not in resp.text
response['data'] = [
{'url': 'http://test', 'text': 'barbarbar', 'description': 'this is <b>html</b>'}
]
response['data'] = [{'url': 'http://test', 'text': 'barbarbar', 'description': 'this is <b>html</b>'}]
resp = app.get('/ajax/search/%s/search1/?q=foo' % cell.pk, status=200)
assert resp.text.count('<li>') == 1
assert '<li><a href="http://test">barbarbar</a>' in resp.text
@ -145,7 +146,7 @@ def test_search_cell(app):
assert '<li>' not in resp.text
assert 'no result found' in resp.text
with override_settings(TEMPLATE_VARS=TEMPLATE_VARS):
settings.TEMPLATE_VARS = TEMPLATE_VARS
cell._search_services = {'data': ['search_tmpl']}
cell.save()
with mock.patch('combo.apps.search.models.requests.get') as requests_get:
@ -160,9 +161,7 @@ def test_search_cell(app):
cell.slug = 'searchfoo'
cell.save()
templates_settings = [settings.TEMPLATES[0].copy()]
templates_settings[0]['DIRS'] = [
'%s/templates-1' % os.path.abspath(os.path.dirname(__file__))
]
templates_settings[0]['DIRS'] = ['%s/templates-1' % os.path.abspath(os.path.dirname(__file__))]
with override_settings(TEMPLATES=templates_settings):
resp = app.get('/ajax/search/%s/search_tmpl/?q=bar' % cell.pk, status=200)
assert requests_get.call_args[0][0] == 'http://search.example.net/?q=bar'