Skip to content

Commit

Permalink
feat: new CustomIP4MaskedField field
Browse files Browse the repository at this point in the history
  • Loading branch information
francesco-filicetti committed Jun 27, 2024
1 parent ad18d56 commit 7522491
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
12 changes: 12 additions & 0 deletions django_form_builder/dynamic_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,18 @@ class CustomIPField(GenericIPAddressField, BaseCustomField):
"""
field_type = _("Indirizzo IP")


class CustomIP4MaskedField(BaseCustomField):
field_type = _("Indirizzo IPv4 con subnet opzionale")

def raise_error(self, name, cleaned_data, **kwargs):
if not cleaned_data: return []

# Only valid sequences
if not re.match('/^([01]?\d\d?|2[0-4]\d|25[0-5])(?:\.(?:[01]?\d\d?|2[0-4]\d|25[0-5])){3}(?:\/[0-2]\d|\/3[0-2])?$', str(cleaned_data)):
return [_("Formato non valido"),]


class CustomMACField(CustomCharField, BaseCustomField):
"""
GenericIPAddressField
Expand Down
7 changes: 7 additions & 0 deletions django_form_builder/tests/test_01_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ def test_ip_valid(self):
logger.info("Test valid CustomIPField")
assert form.is_valid()

def test_ipv4_valid(self):
"""
Test invalid values in CustomIP4MaskedField
"""
form = self.single_field_form('CustomIP4MaskedField', '192.168.0.1/20')
logger.info("Test valid CustomIP4MaskedField")
assert form.is_valid()

def test_ip_invalid(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

setup(
name='django-form-builder',
version='1.1.1',
version='1.2.0',
packages=find_packages(),
package_data={'': ['*.wav']},
data_files=[
Expand Down

0 comments on commit 7522491

Please sign in to comment.