diff --git a/django_adminlte_2/templatetags/adminlte_filters.py b/django_adminlte_2/templatetags/adminlte_filters.py index 4ddc8f7492ea79026d40fa81a5a7f7969042b54a..7d04b4221b7d85e71c8675ab561d50e11c128923 100644 --- a/django_adminlte_2/templatetags/adminlte_filters.py +++ b/django_adminlte_2/templatetags/adminlte_filters.py @@ -173,7 +173,7 @@ def with_list(field, name=None): :param field: Form field to add attributes to. :param name: The datalist name. - Defaults to None. + Defaults to '_list' appended to end of field name. :return: Field that was passed in with list attribute added. Example:: @@ -189,10 +189,12 @@ def with_list(field, name=None): <input type="text" name="field" list="my_awesome_list" id="id_field" /> """ - if name is not None: - attrs = field.field.widget.attrs - attrs['list'] = name - field.field.widget.attrs = {**field.field.widget.attrs, **attrs} + if name is None: + name = f'{field.name}_list' + + attrs = field.field.widget.attrs + attrs['list'] = name + field.field.widget.attrs = {**field.field.widget.attrs, **attrs} return field @@ -227,7 +229,7 @@ def with_pattern(field, pattern=None): <input type="tel" name="field" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}" id="id_field" /> """ if pattern is None: - pattern = r"\([0-9]{3}\) [0-9]{3}-[0-9]{4}", + pattern = r"\([0-9]{3}\) [0-9]{3}-[0-9]{4}" attrs = field.field.widget.attrs attrs['pattern'] = pattern diff --git a/tests/test_templatetags.py b/tests/test_templatetags.py index 7c623e5692ccb304819e899eceb751529bcbf3e6..703aa14b066948e7bae0c2fc1e6790fd1b877079 100644 --- a/tests/test_templatetags.py +++ b/tests/test_templatetags.py @@ -226,6 +226,168 @@ class TemplateTagTestCase(TestCase): str(result) ) + # |------------------------------------------------------------------------- + # | Test with_list + # |------------------------------------------------------------------------- + + def test_with_list_adds_a_default_list_attribute_to_an_existing_form_field(self): + """Test with list adds a default list attribute to an existing form field""" + test_form = self.TestForm() + result = adminlte_filters.with_list( + test_form['test_text'] + ) + + self.assertIn( + 'test_text_list', + str(result) + ) + + def test_with_list_adds_a_specific_list_attribute_to_an_existing_form_field(self): + """Test with list adds a specific list attribute to an existing form field""" + test_form = self.TestForm() + result = adminlte_filters.with_list( + test_form['test_text'], + name='my_fancy_list' + ) + + self.assertIn( + 'my_fancy_list', + str(result) + ) + + # |------------------------------------------------------------------------- + # | Test with_pattern + # |------------------------------------------------------------------------- + + def test_with_pattern_adds_a_default_pattern_attribute_to_an_existing_form_field(self): + """Test with pattern adds a default pattern attribute to an existing form field""" + test_form = self.TestForm() + result = adminlte_filters.with_pattern( + test_form['test_text'] + ) + + self.assertIn( + r'\([0-9]{3}\) [0-9]{3}-[0-9]{4}', + str(result) + ) + + def test_with_pattern_adds_a_specific_pattern_attribute_to_an_existing_form_field(self): + """Test with pattern adds a specific pattern attribute to an existing form field""" + test_form = self.TestForm() + result = adminlte_filters.with_pattern( + test_form['test_text'], + pattern=r'[0-9]{3}-[0-9]{4}' + ) + + self.assertIn( + 'pattern="[0-9]{3}-[0-9]{4}"', + str(result) + ) + + # |------------------------------------------------------------------------- + # | Test with_inputmask + # |------------------------------------------------------------------------- + + def test_with_inputmask_adds_a_default_inputmask_attribute_to_an_existing_form_field(self): + """Test with inputmask adds a default inputmask attribute to an existing form field""" + test_form = self.TestForm() + result = adminlte_filters.with_inputmask( + test_form['test_text'] + ) + + self.assertIn( + '(999) 999-9999', + str(result) + ) + + def test_with_inputmask_adds_a_specific_inputmask_attribute_to_an_existing_form_field(self): + """Test with inputmask adds a specific inputmask attribute to an existing form field""" + test_form = self.TestForm() + result = adminlte_filters.with_inputmask( + test_form['test_text'], + inputmask='999-9999' + ) + + self.assertIn( + '999-9999', + str(result) + ) + + # |------------------------------------------------------------------------- + # | Test with_min + # |------------------------------------------------------------------------- + + def test_with_min_adds_a_default_min_attribute_to_an_existing_form_field(self): + """Test with min adds a default min attribute to an existing form field""" + test_form = self.TestForm() + result = adminlte_filters.with_min( + test_form['test_text'] + ) + + self.assertIn( + 'min="0"', + str(result) + ) + + def test_with_min_adds_a_specific_min_attribute_to_an_existing_form_field(self): + """Test with min adds a specific min attribute to an existing form field""" + test_form = self.TestForm() + result = adminlte_filters.with_min( + test_form['test_text'], + min_val=10 + ) + + self.assertIn( + 'min="10"', + str(result) + ) + + # |------------------------------------------------------------------------- + # | Test with_max + # |------------------------------------------------------------------------- + + def test_with_max_adds_a_default_max_attribute_to_an_existing_form_field(self): + """Test with max adds a default max attribute to an existing form field""" + test_form = self.TestForm() + result = adminlte_filters.with_max( + test_form['test_text'] + ) + + self.assertIn( + 'max="100"', + str(result) + ) + + def test_with_max_adds_a_specific_max_attribute_to_an_existing_form_field(self): + """Test with max adds a specific max attribute to an existing form field""" + test_form = self.TestForm() + result = adminlte_filters.with_max( + test_form['test_text'], + max_val=90 + ) + + self.assertIn( + 'max="90"', + str(result) + ) + + # |------------------------------------------------------------------------- + # | Test with_input_type + # |------------------------------------------------------------------------- + + def test_with_input_type_changes_the_input_type_of_an_existing_form_field(self): + """Test with input type changes the input_type of an existing form field""" + test_form = self.TestForm() + result = adminlte_filters.with_input_type( + test_form['test_text'], + new_type='url' + ) + + self.assertIn( + 'type="url"', + str(result) + ) + # |------------------------------------------------------------------------- # | Test dir # |-------------------------------------------------------------------------