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 get_context(self, name, value, attrs): add = partial(self.add_dselect, attrs={'auto': False}) add('type', options=self.types) add( 'sub_type', options=TemplateURLBuilder( type_id=(TemplateURLBuilder.Word, '${type}')).resolve('activities__get_types'), ) # return super(ActivityTypeWidget, self).get_context(name=name, value=value, attrs=attrs) return super().get_context(name=name, value=value, attrs=attrs)
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 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 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_popup_options(self): widget = EntitySelector(content_type=12) 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="hidden" class="ui-creme-input ui-creme-entityselector" value="1"/> <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 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 test_render_qfilter01(self): "Dictionary instance." widget = EntitySelector(content_type=12) name = 'field' value = '1' render_dom = assert_and_parse_html( self, widget.render('field', '1', attrs={'qfilter': { 'pk__in': [12, 13] }}), user_msg=None, msg='Widget render is not valid !', ) qfilter_attr = self.assertQFilter( render_dom, { 'val': [['pk__in', [12, 13]]], '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="text" value="{value}" class="ui-creme-input ui-creme-entityselector"/> <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=12&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_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 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 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 _build_valueinput(self, field_attrs): pinput = PolymorphicInput(key='${field.type}.${operator.id}', attrs={'auto': False}) # pinput.add_input('^enum(__null)?.({}|{})$'.format(EntityFilterCondition.EQUALS, # EntityFilterCondition.EQUALS_NOT, # ), 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(EntityFilterCondition.RANGE), pinput.add_input( '^date(__null)?.{}$'.format(operators.RANGE), NullableDateRangeSelect, attrs={'auto': False}, ) pinput.add_input( '^boolean(__null)?.*', # DynamicSelect, options=((TRUE, _('True')), (FALSE, _('False'))), attrs=field_attrs, DynamicSelect, options=_BOOL_OPTIONS, attrs=field_attrs, ) # pinput.add_input('(string|.*__null)?.({})$'.format(EntityFilterCondition.ISEMPTY), pinput.add_input( '(string|.*__null)?.({})$'.format(operators.ISEMPTY), # DynamicSelect, options=((TRUE, _('True')), (FALSE, _('False'))), attrs=field_attrs, DynamicSelect, options=_BOOL_OPTIONS, attrs=field_attrs, ) pinput.set_default_input(widget=DynamicInput, attrs={'auto': False}) return pinput
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 test_text_url(self): widget = EntitySelector(content_type=12) url = TemplateURLBuilder(entity_id=(TemplateURLBuilder.Int, '${id}')).resolve('creme_core__entity_as_json') self.assertEqual(url, widget.text_url)
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