def setup_modelmultichoice_field(self, fieldname, formfield): """ Called by :meth:`.setup_field` if the ``formfield`` is a ModelMultiChoiceField. Sets up :class:`cradmin_legacy.viewhelpers.multiselect2.manytomanywidget.Widget` as the widget if the ``view``-argument is provided to the constructor of this form, and if ``view.request.cradmin_instance.get_manytomanyselectview_url` returns a view URL for the model in the ModelMultiChoiceField. Parameters: fieldname: The name of the field. formfield: The form field object. """ if not self.view or not hasattr(self.view.request, 'cradmin_instance'): return model_class = formfield.queryset.model cradmin_instance = self.view.request.cradmin_instance manytomanyselectview_url = cradmin_instance.get_manytomanyselectview_url(model_class=model_class) if not manytomanyselectview_url: return self.fields[fieldname].widget = manytomanywidget.Widget( queryset=formfield.queryset, selectview_url=manytomanyselectview_url, required=formfield.required)
def test_inputfield_value_empty_string(self): mockqueryset = mock.MagicMock() selector = htmls.S( manytomanywidget.Widget(queryset=mockqueryset, selectview_url='/test').render( name='testname', value='', attrs={'id': 'testid'})) self.assertEqual('[]', selector.one('input[name="testname"]')['value'])
def test_selectbutton_css_class(self): mockqueryset = mock.MagicMock() selector = htmls.S( manytomanywidget.Widget(queryset=mockqueryset, selectview_url='/test').render( name='testname', value=None, attrs={'id': 'testid'})) self.assertEqual('btn btn-default', selector.one('button')['class'])
def test_inputfield_type_input_field_visible_default(self): mockqueryset = mock.MagicMock() selector = htmls.S( manytomanywidget.Widget(queryset=mockqueryset, selectview_url='/test').render( name='testname', value=None, attrs={'id': 'testid'})) self.assertEqual('hidden', selector.one('input[name="testname"]')['type'])
def test_selectbutton_text_default(self): mockqueryset = mock.MagicMock() selector = htmls.S( manytomanywidget.Widget(queryset=mockqueryset, selectview_url='/test').render( name='testname', value=None, attrs={'id': 'testid'})) self.assertEqual('Select ...', selector.one('button').alltext_normalized)
def test_wrapper_class(self): mockqueryset = mock.MagicMock() selector = htmls.S( manytomanywidget.Widget(queryset=mockqueryset, selectview_url='/test').render( name='testname', value=None, attrs={'id': 'testid'})) self.assertTrue( selector.one('#id_cradmin_modelmultichoicefieldwrapper_testname'). hasclass('cradmin-legacy-modelmultichoicefield-wrapper'))
def test_selectbutton_angularjs_directive(self): mockqueryset = mock.MagicMock() selector = htmls.S( manytomanywidget.Widget(queryset=mockqueryset, selectview_url='/test').render( name='testname', value=None, attrs={'id': 'testid'})) self.assertTrue( selector.one('button').hasattribute( 'cradmin-legacy-model-choice-field-changebegin-button'))
def test_preview_list_without_value(self): mockqueryset = mock.MagicMock() mockqueryset.filter.return_value = [] selector = htmls.S( manytomanywidget.Widget(queryset=mockqueryset, selectview_url='/test').render( name='testname', value=None, attrs={'id': 'testid'})) self.assertEqual( 0, selector.count('.cradmin-legacy-multiselect2-preview-list li'))
def test_preview_list_angularjs_directive(self): mockqueryset = mock.MagicMock() mockqueryset.filter.return_value = [] selector = htmls.S( manytomanywidget.Widget(queryset=mockqueryset, selectview_url='/test').render( name='testname', value=None, attrs={'id': 'testid'})) self.assertTrue( selector.one('.cradmin-legacy-multiselect2-preview-list'). hasattribute('cradmin-legacy-model-choice-field-preview'))
def test_wrapper_angularjs_directive_value(self): mockqueryset = mock.MagicMock() selector = htmls.S( manytomanywidget.Widget(queryset=mockqueryset, selectview_url='/test').render( name='testname', value=[1, 2], attrs={'id': 'testid'})) directive_value = selector.one( '#id_cradmin_modelmultichoicefieldwrapper_testname' )['cradmin-legacy-model-choice-field-wrapper'] path, querydictstring = directive_value.split('?') querydict = QueryDict(querydictstring) self.assertEqual('/test', path) self.assertEqual( { 'manytomany_select_fieldid': 'testid', 'manytomany_select_current_value': '[1, 2]', 'manytomany_select_required': 'True' }, querydict.dict())
def test_preview_list_with_value(self): mockqueryset = mock.MagicMock() mockmodelobject = mock.MagicMock() mockmodelobject.pk = 1 mockmodelobject.__str__.return_value = 'testvalue' if python2_compatibility.is_python2(): mockmodelobject.__unicode__.return_value = 'testvalue' mockqueryset.filter.return_value = [mockmodelobject] selector = htmls.S( manytomanywidget.Widget(queryset=mockqueryset, selectview_url='/test').render( name='testname', value=None, attrs={'id': 'testid'})) self.assertEqual( 1, selector.count('.cradmin-legacy-multiselect2-preview-list li')) self.assertEqual( 'testvalue', selector.one('.cradmin-legacy-multiselect2-preview-list ' '.cradmin-legacy-multiselect2-preview-list-value'). alltext_normalized)