search: configure user search result template (#52439)
This commit is contained in:
parent
9ddf332711
commit
6b2d3159a8
|
@ -18,6 +18,7 @@ from django import forms
|
|||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from combo.data.models import Page
|
||||
from combo.profile import default_description_template
|
||||
|
||||
from . import engines
|
||||
from .models import SearchCell
|
||||
|
@ -152,6 +153,12 @@ class CardsEngineSettingsForm(CardsEngineSettingsUpdateForm):
|
|||
|
||||
class UsersEngineSettingsUpdateForm(forms.ModelForm):
|
||||
title = forms.CharField(label=_('Custom Title'), required=False)
|
||||
description_template = forms.CharField(
|
||||
label=_('User search result template'),
|
||||
required=False,
|
||||
widget=forms.Textarea,
|
||||
initial=default_description_template,
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = SearchCell
|
||||
|
|
|
@ -29,6 +29,7 @@ from combo.apps.search.forms import (
|
|||
)
|
||||
from combo.apps.search.models import SearchCell
|
||||
from combo.data.models import PageSnapshot
|
||||
from combo.profile import default_description_template
|
||||
|
||||
|
||||
def page_search_cell_add_engine(request, page_pk, cell_reference, engine_slug):
|
||||
|
@ -64,6 +65,8 @@ def page_search_cell_add_engine(request, page_pk, cell_reference, engine_slug):
|
|||
kwargs = {
|
||||
'title': form.cleaned_data['title'],
|
||||
}
|
||||
if form.cleaned_data.get('description_template'):
|
||||
kwargs['description_template'] = form.cleaned_data['description_template']
|
||||
for key in ['without_user', 'with_description']:
|
||||
if form.cleaned_data.get(key):
|
||||
kwargs[key] = True
|
||||
|
@ -92,6 +95,8 @@ def page_search_cell_update_engine(request, page_pk, cell_reference, engine_slug
|
|||
kwargs = {
|
||||
'title': form.cleaned_data['title'],
|
||||
}
|
||||
if form.cleaned_data.get('description_template'):
|
||||
kwargs['description_template'] = form.cleaned_data['description_template']
|
||||
for key in ['without_user', 'with_description']:
|
||||
if form.cleaned_data.get(key):
|
||||
kwargs[key] = True
|
||||
|
@ -112,6 +117,10 @@ def page_search_cell_update_engine(request, page_pk, cell_reference, engine_slug
|
|||
'with_description': cell._search_services.get('options', {})
|
||||
.get(engine_slug, {})
|
||||
.get('with_description'),
|
||||
'description_template': cell._search_services.get('options', {})
|
||||
.get(engine_slug, {})
|
||||
.get('description_template')
|
||||
or default_description_template,
|
||||
}
|
||||
form = form_class(instance=cell, engine_slug=engine_slug, initial=initial)
|
||||
context = {
|
||||
|
|
|
@ -261,7 +261,9 @@ class SearchCell(CellBase):
|
|||
hit_templates['url'] = Template(service['hit_url_template'])
|
||||
if service.get('hit_label_template'):
|
||||
hit_templates['text'] = Template(service['hit_label_template'])
|
||||
if service.get('hit_description_template'):
|
||||
if (service.get('options') or {}).get('description_template'):
|
||||
hit_templates['description'] = Template(service['options']['description_template'])
|
||||
if not hit_templates.get('description') and service.get('hit_description_template'):
|
||||
hit_templates['description'] = Template(service['hit_description_template'])
|
||||
if hit_templates:
|
||||
for hit in results.get('data') or []:
|
||||
|
|
|
@ -31,6 +31,15 @@ def user_get_name_id(user):
|
|||
return user._name_id
|
||||
|
||||
|
||||
default_description_template = """{{ email|default:"" }}
|
||||
{% if phone %} 📞 {{ phone }}{% endif %}
|
||||
{% if mobile %} 📱 {{ mobile }}{% endif %}
|
||||
{% if address or zipcode or city %} 📨{% endif %}
|
||||
{% if address %} {{ address }}{% endif %}
|
||||
{% if zipcode %} {{ zipcode }}{% endif %}
|
||||
{% if city %} {{ city }}{% endif %}"""
|
||||
|
||||
|
||||
class AppConfig(django.apps.AppConfig):
|
||||
name = 'combo.profile'
|
||||
verbose_name = _('Profile')
|
||||
|
@ -65,13 +74,7 @@ class AppConfig(django.apps.AppConfig):
|
|||
'data_key': 'results',
|
||||
'hit_url_template': user_page_base_url + '{{uuid}}/',
|
||||
'hit_label_template': '{{first_name}} {{last_name}}',
|
||||
'hit_description_template': '{{ email|default:"" }}'
|
||||
'{% if phone %} 📞 {{ phone }}{% endif %}'
|
||||
'{% if mobile %} 📱 {{ mobile }}{% endif %}'
|
||||
'{% if address or zipcode or city %} 📨 {% endif %}'
|
||||
'{% if address %} {{ address }}{% endif %}'
|
||||
'{% if zipcode %} {{ zipcode }}{% endif %}'
|
||||
'{% if city %} {{ city }}{% endif %}',
|
||||
'hit_description_template': default_description_template,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1145,22 +1145,31 @@ def test_profile_add_search_engines(settings, app, admin_user):
|
|||
|
||||
resp = resp.click(href='.*/search_searchcell-%s/engine/users/add/' % cell.pk)
|
||||
resp.form['title'] = 'Custom Title'
|
||||
resp.form['description_template'] = 'Foo Bar'
|
||||
resp = resp.form.submit('submit')
|
||||
assert resp.status_int == 302
|
||||
assert resp.location.endswith('/manage/pages/%s/#cell-%s' % (page.pk, cell.get_reference()))
|
||||
cell.refresh_from_db()
|
||||
assert cell._search_services['data'] == ['users']
|
||||
assert cell._search_services['options']['users'] == {'title': 'Custom Title'}
|
||||
assert cell._search_services['options']['users'] == {
|
||||
'title': 'Custom Title',
|
||||
'description_template': 'Foo Bar',
|
||||
}
|
||||
|
||||
# edit engine
|
||||
resp = app.get('/manage/pages/%s/' % page.pk)
|
||||
resp = resp.click(href='.*/search_searchcell-%s/engine/users/edit/' % cell.pk)
|
||||
resp.form['title'] = 'Custom Title Foo Bar'
|
||||
resp.form['description_template'] = ''
|
||||
resp.form['description_template'] = 'Foo Bar Baz'
|
||||
resp = resp.form.submit('submit')
|
||||
assert resp.status_int == 302
|
||||
assert resp.location.endswith('/manage/pages/%s/#cell-%s' % (page.pk, cell.get_reference()))
|
||||
cell.refresh_from_db()
|
||||
assert cell._search_services['options']['users'] == {'title': 'Custom Title Foo Bar'}
|
||||
assert cell._search_services['options']['users'] == {
|
||||
'title': 'Custom Title Foo Bar',
|
||||
'description_template': 'Foo Bar Baz',
|
||||
}
|
||||
|
||||
# remove engine
|
||||
resp = resp.follow()
|
||||
|
@ -1171,6 +1180,51 @@ def test_profile_add_search_engines(settings, app, admin_user):
|
|||
assert cell._search_services['data'] == []
|
||||
|
||||
|
||||
def test_search_profile(settings, app):
|
||||
settings.KNOWN_SERVICES = {'authentic': {'default': {'title': 'authentic', 'url': 'https://authentic/'}}}
|
||||
Page.objects.create(slug='users', title='Users', sub_slug='(?P<name_id>[a-z0-9]+)')
|
||||
|
||||
page = Page.objects.create(title='One', slug='one', template_name='standard')
|
||||
cell = SearchCell.objects.create(page=page, placeholder='content', order=0)
|
||||
|
||||
cell._search_services = {'data': ['users']}
|
||||
cell.save()
|
||||
with mock.patch('combo.apps.search.models.requests.get') as requests_get:
|
||||
response = {
|
||||
'results': [
|
||||
{
|
||||
'id': '1',
|
||||
'uuid': '982a7c65f6c14a2c80307afd9c4bb368',
|
||||
'first_name': 'Foo',
|
||||
'last_name': 'Bar',
|
||||
'email': 'foo@bar.com',
|
||||
'phone': '0505050505',
|
||||
'mobile': '0606060606',
|
||||
'address': 'There',
|
||||
'zipcode': '75000',
|
||||
'city': 'Paris',
|
||||
}
|
||||
],
|
||||
}
|
||||
mock_json = mock.Mock()
|
||||
mock_json.json.return_value = response
|
||||
requests_get.return_value = mock_json
|
||||
|
||||
resp = app.get('/ajax/search/%s/users/?q=foo' % cell.pk, status=200)
|
||||
assert resp.text.count('<li>') == 1
|
||||
assert '<li><a href="/users/982a7c65f6c14a2c80307afd9c4bb368/">Foo Bar</a>' in resp.text
|
||||
assert '<div>foo@bar.com 📞 0505050505 📱 0606060606 📨 There 75000 Paris</div>' in resp.text.replace(
|
||||
'\n', ''
|
||||
)
|
||||
|
||||
cell._search_services['options'] = {'users': {'description_template': '{{ email }} Foo Bar'}}
|
||||
cell.save()
|
||||
resp = app.get('/ajax/search/%s/users/?q=foo' % cell.pk, status=200)
|
||||
assert resp.text.count('<li>') == 1
|
||||
assert '<li><a href="/users/982a7c65f6c14a2c80307afd9c4bb368/">Foo Bar</a>' in resp.text
|
||||
assert '<div>foo@bar.com Foo Bar</div>' in resp.text
|
||||
|
||||
|
||||
def test_private_search(app):
|
||||
page = Page(title='example page', slug='example-page')
|
||||
page.save()
|
||||
|
|
Loading…
Reference in New Issue