fields: add parameter to have item hint as a real hint (#22048) #258
|
@ -6,6 +6,7 @@ import time
|
|||
import pytest
|
||||
import responses
|
||||
from bs4 import BeautifulSoup
|
||||
from pyquery import PyQuery
|
||||
from quixote.http_request import Upload
|
||||
|
||||
from wcs import fields
|
||||
|
@ -620,6 +621,42 @@ def test_item_get_display_value(pub):
|
|||
assert field.get_display_value(None) is None
|
||||
|
||||
|
||||
def test_item_render_as_list_with_hint(pub):
|
||||
items = ['aa', 'ab', 'ac']
|
||||
field = fields.ItemField(id='1', label='Foobar', display_mode='list', items=items)
|
||||
form = Form(use_tokens=False)
|
||||
field.add_to_form(form)
|
||||
assert len(PyQuery(str(form.render())).find('.hint')) == 0
|
||||
assert len(PyQuery(str(form.render())).find('option')) == 3
|
||||
|
||||
field = fields.ItemField(id='1', label='Foobar', display_mode='list', items=items, hint='This is an hint')
|
||||
form = Form(use_tokens=False)
|
||||
field.add_to_form(form)
|
||||
assert len(PyQuery(str(form.render())).find('.hint')) == 0
|
||||
assert len(PyQuery(str(form.render())).find('option')) == 4
|
||||
|
||||
field = fields.ItemField(
|
||||
id='1', label='Foobar', display_mode='list', items=items, hint='This is a very long hint' + 'x ' * 50
|
||||
)
|
||||
form = Form(use_tokens=False)
|
||||
field.add_to_form(form)
|
||||
assert len(PyQuery(str(form.render())).find('.hint')) == 1
|
||||
assert len(PyQuery(str(form.render())).find('option')) == 3
|
||||
|
||||
field = fields.ItemField(
|
||||
id='1',
|
||||
label='Foobar',
|
||||
display_mode='list',
|
||||
items=items,
|
||||
hint='This is an hint',
|
||||
use_hint_as_first_option=False,
|
||||
)
|
||||
form = Form(use_tokens=False)
|
||||
field.add_to_form(form)
|
||||
assert len(PyQuery(str(form.render())).find('.hint')) == 1
|
||||
assert len(PyQuery(str(form.render())).find('option')) == 3
|
||||
|
||||
|
||||
def test_item_render_as_radio():
|
||||
items_kwargs = []
|
||||
items_kwargs.append({'items': ['aa', 'ab', 'ac']})
|
||||
|
|
|
@ -2273,6 +2273,11 @@ class ItemField(WidgetField, MapOptionsMixin, ItemFieldMixin):
|
|||
in_filters = False
|
||||
display_disabled_items = False
|
||||
display_mode = 'list'
|
||||
|
||||
# <select> option
|
||||
use_hint_as_first_option = True
|
||||
|
||||
# timetable option
|
||||
initial_date_alignment = None
|
||||
|
||||
# map options
|
||||
|
@ -2365,7 +2370,9 @@ class ItemField(WidgetField, MapOptionsMixin, ItemFieldMixin):
|
|||
kwargs['options'] = self.get_options()
|
||||
if not kwargs.get('options'):
|
||||
kwargs['options'] = [(None, '---', None)]
|
||||
if display_mode == 'radio':
|
||||
if display_mode == 'list':
|
||||
kwargs['use_hint_as_first_option'] = self.use_hint_as_first_option
|
||||
elif display_mode == 'radio':
|
||||
self.widget_class = RadiobuttonsWidget
|
||||
if isinstance(kwargs['options'][0], str):
|
||||
first_items = [x for x in kwargs['options'][:3]]
|
||||
|
@ -2586,6 +2593,16 @@ class ItemField(WidgetField, MapOptionsMixin, ItemFieldMixin):
|
|||
'data-dynamic-display-value': 'template',
|
||||
},
|
||||
)
|
||||
form.add(
|
||||
CheckboxWidget,
|
||||
'use_hint_as_first_option',
|
||||
title=_('Use hint as first option'),
|
||||
value=self.use_hint_as_first_option,
|
||||
attrs={
|
||||
'data-dynamic-display-child-of': 'display_mode',
|
||||
'data-dynamic-display-value': 'list',
|
||||
},
|
||||
)
|
||||
|
||||
def get_admin_attributes(self):
|
||||
return WidgetField.get_admin_attributes(self) + [
|
||||
|
@ -2601,6 +2618,7 @@ class ItemField(WidgetField, MapOptionsMixin, ItemFieldMixin):
|
|||
'initial_position',
|
||||
'position_template',
|
||||
'initial_date_alignment',
|
||||
'use_hint_as_first_option',
|
||||
]
|
||||
|
||||
def check_admin_form(self, form):
|
||||
|
|
|
@ -2618,16 +2618,19 @@ class SingleSelectHintWidget(SingleSelectWidget):
|
|||
def __init__(self, name, value=None, **kwargs):
|
||||
self.options_with_attributes = kwargs.pop('options_with_attributes', None)
|
||||
self.select2 = kwargs.pop('select2', None)
|
||||
self.use_hint_as_first_option = kwargs.pop('use_hint_as_first_option', True)
|
||||
if 'template-name' in kwargs:
|
||||
self.template_name = kwargs.pop('template-name')
|
||||
super().__init__(name, value=value, **kwargs)
|
||||
if self.use_hint_as_first_option and len(self.hint or '') > 80:
|
||||
self.use_hint_as_first_option = False
|
||||
|
||||
def add_media(self):
|
||||
if self.select2:
|
||||
get_response().add_javascript(['select2.js'])
|
||||
|
||||
def separate_hint(self):
|
||||
return self.hint and len(self.hint) > 80
|
||||
return not (self.use_hint_as_first_option)
|
||||
|
||||
def get_options(self):
|
||||
if self.options_with_attributes:
|
||||
|
|
Loading…
Reference in New Issue