

from flask_wtf import Form
from wtforms import TextField
from wtforms.validators import Required, IPAddress


class SnatForm(Form):
    source = TextField(u'SNAT源IP（段）',
                       validators=[Required(message=u'这是一个必选项！')])
    gateway = TextField(u'SNAT转发IP',
                        validators=[Required(message=u'这是一个必选项！'),
                                    IPAddress(message=u'无效的ip 地址！')])



from flask_wtf import Form
from wtforms import TextField, ValidationError
from wtforms.validators import Required, IPAddress


def IPorNet(message=u"无效的IP 或网段！"):
    def _ipornet(form, field):
        value = field.data
        ip = value.split('/')[0]
        if '/' in value:
            try:
                mask = int(value.split('/')[1])
            except:
                raise ValidationError(message)
            if mask < 0 or mask > 32:
                    raise ValidationError(message)
        parts = ip.split('.')
        if len(parts) == 4 and all(x.isdigit() for x in parts):
            numbers = list(int(x) for x in parts)
            if not all(num >= 0 and num < 256 for num in numbers):
                raise ValidationError(message)
            return True
        raise ValidationError(message)
    return _ipornet


class SnatForm(Form):
    source = TextField(u'SNAT源IP（段）',
                       validators=[Required(message=u'这是一个必选项！'),
                                   IPorNet(message=u"无效的IP 或网段！")])
    gateway = TextField(u'SNAT转发IP',
                        validators=[Required(message=u'这是一个必选项！'),
                                    IPAddress(message=u'无效的IP 地址！')])

Add snat ip or net validator.
Add snat ip or net validator.

  
  
  from flask_wtf import Form
- from wtforms import TextField
+ from wtforms import TextField, ValidationError
  from wtforms.validators import Required, IPAddress
+ 
+ 
+ def IPorNet(message=u"无效的IP 或网段！"):
+     def _ipornet(form, field):
+         value = field.data
+         ip = value.split('/')[0]
+         if '/' in value:
+             try:
+                 mask = int(value.split('/')[1])
+             except:
+                 raise ValidationError(message)
+             if mask < 0 or mask > 32:
+                     raise ValidationError(message)
+         parts = ip.split('.')
+         if len(parts) == 4 and all(x.isdigit() for x in parts):
+             numbers = list(int(x) for x in parts)
+             if not all(num >= 0 and num < 256 for num in numbers):
+                 raise ValidationError(message)
+             return True
+         raise ValidationError(message)
+     return _ipornet
  
  
  class SnatForm(Form):
      source = TextField(u'SNAT源IP（段）',
-                        validators=[Required(message=u'这是一个必选项！')])
+                        validators=[Required(message=u'这是一个必选项！'),
+                                    IPorNet(message=u"无效的IP 或网段！")])
      gateway = TextField(u'SNAT转发IP',
                          validators=[Required(message=u'这是一个必选项！'),
-                                     IPAddress(message=u'无效的ip 地址！')])
+                                     IPAddress(message=u'无效的IP 地址！')])
  
Add snat ip or net validator.
## Code Before:


from flask_wtf import Form
from wtforms import TextField
from wtforms.validators import Required, IPAddress


class SnatForm(Form):
    source = TextField(u'SNAT源IP（段）',
                       validators=[Required(message=u'这是一个必选项！')])
    gateway = TextField(u'SNAT转发IP',
                        validators=[Required(message=u'这是一个必选项！'),
                                    IPAddress(message=u'无效的ip 地址！')])

## Instruction:
Add snat ip or net validator.
## Code After:


from flask_wtf import Form
from wtforms import TextField, ValidationError
from wtforms.validators import Required, IPAddress


def IPorNet(message=u"无效的IP 或网段！"):
    def _ipornet(form, field):
        value = field.data
        ip = value.split('/')[0]
        if '/' in value:
            try:
                mask = int(value.split('/')[1])
            except:
                raise ValidationError(message)
            if mask < 0 or mask > 32:
                    raise ValidationError(message)
        parts = ip.split('.')
        if len(parts) == 4 and all(x.isdigit() for x in parts):
            numbers = list(int(x) for x in parts)
            if not all(num >= 0 and num < 256 for num in numbers):
                raise ValidationError(message)
            return True
        raise ValidationError(message)
    return _ipornet


class SnatForm(Form):
    source = TextField(u'SNAT源IP（段）',
                       validators=[Required(message=u'这是一个必选项！'),
                                   IPorNet(message=u"无效的IP 或网段！")])
    gateway = TextField(u'SNAT转发IP',
                        validators=[Required(message=u'这是一个必选项！'),
                                    IPAddress(message=u'无效的IP 地址！')])
