def test_two_place_holders02(self): # "2 int place holders" "2 int & 1 word place holders" # vname = 'creme_core__merge_entities' vname = 'creme_core__inner_edition' placeholder1 = '123456' final_value1 = '${id1}' placeholder2 = '789456' final_value2 = '${id2}' placeholder3 = 'fobbar' final_value3 = '${fname}' # tub = TemplateURLBuilder(entity1_id=(TemplateURLBuilder.Int, final_value1), # entity2_id=(TemplateURLBuilder.Int, final_value2), # ) tub = TemplateURLBuilder( ct_id=(TemplateURLBuilder.Int, final_value1), id=(TemplateURLBuilder.Int, final_value2), field_name=(TemplateURLBuilder.Word, final_value3), ) # self.assertEqual(reverse(vname, args=(placeholder1, placeholder2)).replace(placeholder1, final_value1) # .replace(placeholder2, final_value2), # tub.resolve(vname) # ) self.assertEqual( reverse(vname, args=(placeholder1, placeholder2, placeholder3)).replace( placeholder1, final_value1).replace( placeholder2, final_value2).replace(placeholder3, final_value3), tub.resolve(vname))
def test_one_place_holder01(self): "Word place holder" vname = 'creme_core__batch_process_ops' placeholder = 'XXXXXX' final_value = '${name}' # This string does not match with (?P<field>[\w]+) tub = TemplateURLBuilder(field=(TemplateURLBuilder.Word, final_value)) self.assertEqual(reverse(vname, args=(65, placeholder)).replace(placeholder, final_value), tub.resolve(vname, kwargs={'ct_id': 65}) )
def test_one_place_holder02(self): "Int place holder" vname = 'creme_core__batch_process_ops' placeholder = '123456' final_value = '${ct}' # This string does not match with (?P<ct_id>\d+) tub = TemplateURLBuilder(ct_id=(TemplateURLBuilder.Int, final_value)) self.assertEqual(reverse(vname, args=(placeholder, 'name')).replace(placeholder, final_value), tub.resolve(vname, kwargs={'field': 'name'}) )
def test_two_place_holders01(self): "1 word & 1 int place holders" vname = 'creme_core__batch_process_ops' placeholder1 = '123456'; final_value1 = '${ct}' placeholder2 = 'XXXXXX'; final_value2 = '${name}' tub = TemplateURLBuilder(ct_id=(TemplateURLBuilder.Int, final_value1), field=(TemplateURLBuilder.Word, final_value2), ) self.assertEqual(reverse(vname, args=(placeholder1, placeholder2)).replace(placeholder1, final_value1) .replace(placeholder2, final_value2), tub.resolve(vname) )
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 _build_valueinput(self, field_attrs): pinput = PolymorphicInput(key='${field.type}.${operator.id}', attrs={'auto': False}) pinput.add_input( '^enum(__null)?.({}|{})$'.format(operators.EQUALS, operators.EQUALS_NOT), widget=DynamicSelectMultiple, # TODO: use a GET arg instead of using a TemplateURLBuilder ? url=TemplateURLBuilder( cf_id=(TemplateURLBuilder.Int, '${field.id}'), ).resolve('creme_core__cfield_enums'), attrs=field_attrs, ) pinput.add_input( '^date(__null)?.{}$'.format(operators.RANGE), NullableDateRangeSelect, attrs={'auto': False}, ) pinput.add_input( '^boolean(__null)?.*', DynamicSelect, options=_BOOL_OPTIONS, attrs=field_attrs, ) pinput.add_input( '(string|.*__null)?.({})$'.format(operators.ISEMPTY), DynamicSelect, options=_BOOL_OPTIONS, attrs=field_attrs, ) pinput.set_default_input(widget=DynamicInput, attrs={'auto': False}) return pinput
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_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_two_place_holders02(self): "2 int & 1 word place holders" vname = 'creme_core__inner_edition' placeholder1 = '123456'; final_value1 = '${id1}' placeholder2 = '789456'; final_value2 = '${id2}' placeholder3 = 'fobbar'; final_value3 = '${fname}' tub = TemplateURLBuilder(ct_id=(TemplateURLBuilder.Int, final_value1), id=(TemplateURLBuilder.Int, final_value2), field_name=(TemplateURLBuilder.Word, final_value3), ) self.assertEqual(reverse(vname, args=(placeholder1, placeholder2, placeholder3)) .replace(placeholder1, final_value1) .replace(placeholder2, final_value2) .replace(placeholder3, final_value3), tub.resolve(vname) )
def get_context(self, name, value, attrs): add_dselect = partial(self.add_dselect, attrs={'auto': False}) add_dselect('type', options=self.types) add_dselect( 'sub_type', options=TemplateURLBuilder( type_id=(TemplateURLBuilder.Word, '${type}'), ).resolve('activities__get_types'), ) return super().get_context(name=name, value=value, attrs=attrs)
def get_context(self, name, value, attrs): self.selector = chained_input = ChainedInput() src_name = 'source' add = partial(chained_input.add_dselect, attrs={'auto': False}) add(src_name, options=self.sources) add('choice', options=TemplateURLBuilder( line_id=(TemplateURLBuilder.Int, '${%s}' % src_name)).resolve('polls__form_line_choices')) return super().get_context(name=name, value=value, attrs=attrs)
def get_context(self, name, value, attrs): selector = ChainedInput(self.attrs) add = partial(selector.add_dselect, attrs={'auto': False}) add('category', options=self.categories, label=_('Category')) add( 'subcategory', options=TemplateURLBuilder(category_id=( TemplateURLBuilder.Int, '${category}')).resolve('products__subcategories'), label=_('Sub-category'), ) self.delegate = selector self._build_actions(attrs) return super().get_context(name=name, value=value, attrs=attrs)
def get_selector(self, name, value, attrs): chained_input = ChainedInput() rtypes = [*self.rtypes] if not rtypes: return None attrs_json = {'auto': False, 'datatype': 'json'} rtype_name = 'rtype' ctype_name = 'ctype' add_dselect = chained_input.add_dselect add_dselect('has', options=_HAS_RELATION_OPTIONS.items(), attrs=attrs_json) add_dselect(rtype_name, options=rtypes, attrs={ 'auto': False, 'autocomplete': True }) add_dselect( ctype_name, attrs={ **attrs_json, 'autocomplete': True }, # TODO: use a GET arg instead of using a TemplateURLBuilder ? options=TemplateURLBuilder( rtype_id=(TemplateURLBuilder.Word, '${%s}' % rtype_name), ).resolve('creme_core__ctypes_compatible_with_rtype'), ) add_dselect( 'filter', options=reverse('creme_core__efilters') + '?ct_id=${%s}' % ctype_name, attrs={ 'auto': False, 'autocomplete': True, 'data-placeholder': _('(no filter)') }, ) return chained_input
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 get_selector(self, name, value, attrs): chained_input = ChainedInput() rtypes = [*self.rtypes] if not rtypes: return None # datatype = json => boolean are returned as json boolean, not strings attrs_json = {'auto': False, 'datatype': 'json'} rtype_name = 'rtype' # TODO: use a GET arg instead of using a TemplateURLBuilder ? ctype_url = TemplateURLBuilder( rtype_id=(TemplateURLBuilder.Word, '${%s}' % rtype_name), ).resolve('creme_core__ctypes_compatible_with_rtype_as_choices') add_dselect = chained_input.add_dselect add_dselect('has', options=_HAS_RELATION_OPTIONS.items(), attrs=attrs_json) add_dselect(rtype_name, options=rtypes, attrs={ 'auto': False, 'autocomplete': True }) add_dselect('ctype', options=ctype_url, attrs={ **attrs_json, 'autocomplete': True }) chained_input.add_input( 'entity', widget=RelationTargetInput, attrs={'auto': False}, key='${ctype}', multiple=True, ) return chained_input
def _build_valueinput(self, field_attrs): pinput = PolymorphicInput(key='${field.type}.${operator.id}', attrs={'auto': False}) EQUALS_OPS = '{}|{}'.format(operators.EQUALS, operators.EQUALS_NOT) add_input = pinput.add_input add_input( '^enum(__null)?.({})$'.format(EQUALS_OPS), widget=DynamicSelectMultiple, attrs=field_attrs, # TODO: use a GET arg instead of using a TemplateURLBuilder ? # TODO: remove "field.ctype" ? url=TemplateURLBuilder( field=(TemplateURLBuilder.Word, '${field.name}'), ).resolve( 'creme_core__enumerable_choices', kwargs={ 'ct_id': ContentType.objects.get_for_model(self.model).id }, ), ) pinput.add_dselect( '^user(__null)?.({})$'.format(EQUALS_OPS), '{}?filter_type={}'.format( reverse('creme_core__efilter_user_choices'), self.filter_type, ), attrs=field_attrs, ) add_input( '^fk(__null)?.({})$'.format(EQUALS_OPS), widget=EntitySelector, attrs={'auto': False}, content_type='${field.ctype}', ) add_input( '^date(__null)?.{}$'.format(operators.RANGE), widget=NullableDateRangeSelect, attrs={'auto': False}, ) add_input( '^date(__null)?.({})$'.format(EQUALS_OPS), widget=DynamicInput, type='date', attrs={'auto': False}, ) add_input( '^boolean(__null)?.*', widget=DynamicSelect, options=_BOOL_OPTIONS, attrs=field_attrs, ) add_input( '(string|.*__null).({})$'.format(operators.ISEMPTY), widget=DynamicSelect, options=_BOOL_OPTIONS, attrs=field_attrs, ) pinput.set_default_input(widget=DynamicInput, attrs={'auto': False}) return pinput
def test_place_holder02(self): "Int" ph = TemplateURLBuilder.Int('$ct_id', 'ctype_id') self.assertEqual('1234567890', ph.tmp_name(0, 0)) self.assertEqual('1234567891', ph.tmp_name(1, 0)) self.assertEqual('9876543210', ph.tmp_name(0, 1))
def test_place_holder01(self): "Word" ph = TemplateURLBuilder.Word('$name', 'name') self.assertEqual('__placeholder0__', ph.tmp_name(0, 0)) self.assertEqual('__placeholder1__', ph.tmp_name(1, 0)) self.assertEqual('__PLACEHOLDER0__', ph.tmp_name(0, 1))