def test_render_is_required(self): widget = EntitySelector(content_type=12) widget.is_required = True html = ''' <span class="ui-creme-widget widget-auto ui-creme-entityselector" widget="ui-creme-entityselector" labelURL="{text_url}" label="{label}" popupURL="{url}" popupSelection="multiple" popupAuto> <input name="field" type="text" value="1" class="ui-creme-input ui-creme-entityselector" required /> <button type="button">{label}</button> </span>'''.format( label=_('Select…'), text_url=TemplateURLBuilder( entity_id=(TemplateURLBuilder.Int, '${id}'), ).resolve('creme_core__entity_as_json'), url=(reverse('creme_core__listview_popup') + '?ct_id=12&selection=${selection}&q_filter=${qfilter}'), ) self.assertHTMLEqual( html, widget.render('field', '1', attrs={ 'multiple': True, 'autoselect': True }))
def test_render_qfilter02(self): "Q instance." widget = EntitySelector(content_type=13) name = 'my_field' value = '2' render_dom = assert_and_parse_html( self, widget.render(name, value, attrs={'qfilter': Q(name='foobar')}), user_msg=None, msg='Widget render is not valid !', ) qfilter_attr = self.assertQFilter(render_dom, {'val': [['name', 'foobar']], 'op': 'AND'}) html = \ '''<span class="ui-creme-widget widget-auto ui-creme-entityselector" widget="ui-creme-entityselector" labelURL="{text_url}" label="{label}" popupURL="{url}" popupSelection="single" qfilter="{q_filter}"> <input name="{name}" type="hidden" class="ui-creme-input ui-creme-entityselector" value="{value}"/> <button type="button">{label}</button> </span>'''.format( name=name, value=value, label=_('Select…'), text_url=TemplateURLBuilder(entity_id=(TemplateURLBuilder.Int, '${id}')) .resolve('creme_core__entity_as_json'), url=reverse('creme_core__listview_popup') + '?ct_id=13&selection=${selection}&q_filter=${qfilter}', q_filter=escape(qfilter_attr), ) self.assertDOMEqual(assert_and_parse_html(self, html, None, 'Expected HTML is not valid !'), render_dom)
def test_listview_url(self): widget = EntitySelector() self.assertEqual( reverse('creme_core__listview_popup') + '?ct_id=${ctype}&selection=${selection}&q_filter=${qfilter}', widget.url) widget = EntitySelector(content_type=12) self.assertEqual( reverse('creme_core__listview_popup') + '?ct_id=12&selection=${selection}&q_filter=${qfilter}', widget.url)
def test_render_empty_value(self): "Empty." ct_id = ContentType.objects.get_for_model(FakeContact).id creation_url = reverse('creme_core__quick_form', args=(ct_id, )) widget = EntityCreatorWidget( FakeContact, creation_url=creation_url, creation_allowed=False, ) self.assertIsNone(widget.creation_label) reset_label = _('Clear') create_label = FakeContact.creation_label html = ''' <ul class="hbox ui-creme-widget ui-layout widget-auto ui-creme-actionbuttonlist" widget="ui-creme-actionbuttonlist"> <li class="delegate"> <span class="ui-creme-widget ui-creme-entityselector" widget="ui-creme-entityselector" labelURL="{select_label_url}" label="{select_label}" popupURL="{select_url}" popupSelection="single"> <input name="field" type="text" class="ui-creme-input ui-creme-entityselector"/> <button type="button">{select_label}</button> </span> </li> <li> <button class="ui-creme-actionbutton with-icon" action="reset" name="reset" title="{reset_label}" type="button" value=""> {reset_icon}<span>{reset_label}</span> </button> </li> <li> <button class="ui-creme-actionbutton with-icon" name="create" title="{create_title}" type="button" disabled popupUrl="{create_url}"> {create_icon}<span>{create_label}</span> </button> </li> </ul>'''.format( select_label=_('Select…'), select_label_url=EntitySelector(content_type=ct_id).text_url, select_url=EntitySelector(content_type=ct_id).url, reset_label=reset_label, reset_icon=self.get_icon('delete', size='form-widget', label=reset_label).render(), create_title=_("Can't create"), create_label=create_label, create_url=creation_url, create_icon=self.get_icon('add', size='form-widget', label=create_label).render(), ) self.assertHTMLEqual(html, widget.render('field', ''))
def test_render02(self): "Initialized, creation_label." user = self.login() contact = FakeContact.objects.create(last_name='Doe', first_name='John', user=user) ct_id = contact.entity_type_id creation_url = reverse('creme_core__quick_form', args=(ct_id, )) creation_label = 'Create a agent' widget = EntityCreatorWidget( FakeContact, creation_url=creation_url, creation_allowed=False, creation_label=creation_label, ) self.assertEqual(creation_label, widget.creation_label) widget.from_python = lambda value: value.id html = '''<ul class="hbox ui-creme-widget ui-layout widget-auto ui-creme-actionbuttonlist" widget="ui-creme-actionbuttonlist"> <li class="delegate"> <span class="ui-creme-widget ui-creme-entityselector" widget="ui-creme-entityselector" labelURL="{select_label_url}" label="{select_label}" popupURL="{select_url}" popupSelection="single"> <input name="field" type="text" class="ui-creme-input ui-creme-entityselector" value="{value}"/> <button type="button">{select_label}</button> </span> </li> <li> <button class="ui-creme-actionbutton" action="reset" name="reset" title="{reset_title}" type="button" value=""> {reset_label} </button> </li> <li> <button class="ui-creme-actionbutton" name="create" title="{create_title}" type="button" disabled popupUrl="{create_url}"> {create_label} </button> </li> </ul>'''.format( select_label=_('Select…'), select_label_url=EntitySelector(content_type=ct_id).text_url, select_url=EntitySelector(content_type=ct_id).url, reset_title=_('Clear'), reset_label=_('Clear'), create_title=_("Can't create"), create_label=creation_label, create_url=creation_url, value=contact.id, ) self.assertHTMLEqual(html, widget.render('field', value=contact))
def test_text_url(self): widget = EntitySelector(content_type=12) self.assertEqual( TemplateURLBuilder( entity_id=(TemplateURLBuilder.Int, '${id}'), ).resolve('creme_core__entity_as_json'), widget.text_url, )
def test_render(self): text_url = TemplateURLBuilder(entity_id=(TemplateURLBuilder.Int, '${id}')).resolve('creme_core__entity_as_json') widget = EntitySelector(content_type=12) name = 'field-1' value = '1' html = \ '''<span class="ui-creme-widget widget-auto ui-creme-entityselector" widget="ui-creme-entityselector" labelURL="{text_url}" label="{label}" popupURL="{url}" popupSelection="single"> <input name="{name}" type="hidden" class="ui-creme-input ui-creme-entityselector" value="{value}"/> <button type="button">{label}</button> </span>'''.format( name=name, value=value, label=_('Select…'), text_url=text_url, url=reverse('creme_core__listview_popup') + '?ct_id=12&selection=${selection}&q_filter=${qfilter}', ) self.assertHTMLEqual(html, widget.render(name, value))
def test_render01(self): "Empty" ct_id = ContentType.objects.get_for_model(FakeContact).id creation_url = reverse('creme_core__quick_form', args=(ct_id,)) widget = EntityCreatorWidget(FakeContact, creation_url=creation_url, creation_allowed=False) html = '''<ul class="hbox ui-creme-widget ui-layout widget-auto ui-creme-actionbuttonlist" widget="ui-creme-actionbuttonlist"> <li class="delegate"> <span class="ui-creme-widget ui-creme-entityselector" widget="ui-creme-entityselector" labelURL="{select_label_url}" label="{select_label}" popupURL="{select_url}" popupSelection="single"> <input name="field" type="hidden" class="ui-creme-input ui-creme-entityselector"/> <button type="button">{select_label}</button> </span> </li> <li> <button class="ui-creme-actionbutton" action="reset" name="reset" title="{reset_title}" type="button" value=""> {reset_label} </button> </li> <li> <button class="ui-creme-actionbutton" name="create" title="{create_title}" type="button" disabled popupUrl="{create_url}"> {create_label} </button> </li> </ul>'''.format( select_label=_('Select…'), select_label_url=EntitySelector(content_type=ct_id).text_url, select_url=EntitySelector(content_type=ct_id).url, # 'select_q_filter': '', # qfilter="%(select_q_filter)s"> reset_title=_('Clear'), reset_label=_('Clear'), create_title=_("Can't create"), create_label=FakeContact.creation_label, create_url=creation_url, ) self.assertHTMLEqual(html, widget.render('field', ''))