Browse Source

only check the port for validity if it has been set

Natenom/support-murmur-13-1446181288462
Michael Ziegler 15 years ago
parent
commit
248179ac5d
  1. 34
      pyweb/mumble/forms.py

34
pyweb/mumble/forms.py

@ -128,12 +128,11 @@ class MumbleAdminForm( MumbleForm ):
exclude = None
def clean_port( self ):
""" If portno == -1 autoassign, and check if the port number is valid. """
""" Check if the port number is valid. """
port = self.cleaned_data['port'];
if port == -1:
port = max( [ rec['port'] for rec in Mumble.objects.values('port') ] ) + 1;
if port is not None and port != '':
if port < 1 or port >= 2**16:
raise forms.ValidationError(
_("Port number %(portno)d is not within the allowed range %(minrange)d - %(maxrange)d") % {
@ -142,34 +141,7 @@ class MumbleAdminForm( MumbleForm ):
'maxrange': 2**16,
});
return port;
def clean( self ):
""" Try to bind to the addr and port to verify that they are available. """
if self.instance.id is not None or 'addr' not in self.cleaned_data or 'port' not in self.cleaned_data:
# Editing old instance or previous validation failed already, don't try to bind
return self.cleaned_data;
addr = socket.gethostbyname( self.cleaned_data['addr'] );
port = self.cleaned_data['port'];
try:
socktcp = socket.socket( socket.AF_INET, socket.SOCK_STREAM );
socktcp.bind( ( addr, port ) );
except socket.error, err:
raise forms.ValidationError( err.args[1] );
finally:
socktcp.close();
try:
sockudp = socket.socket( socket.AF_INET, socket.SOCK_DGRAM );
sockudp.bind( ( addr, port ) );
except socket.error, err:
raise forms.ValidationError( err.args[1] );
finally:
sockudp.close();
return self.cleaned_data;
return None
class MumbleUserForm( ModelForm ):

Loading…
Cancel
Save