Not tested, but something like this is what I mean:
class Router:
def __init__(self,device_type,ip,username,password,port):
self.device_type = device_type
self.ip = ip
self.username = username
self.password = password
self.port = port
# you can actually skip following 4 lines
self.number = None
self.vlanname = None
self.datavlan = None
self.voicevlan = None
def _connect(self):
self.net_connect = ConnectHandler(device_type=self.device_type, ip=self.ip,
username=self.username, password=self.password,
port=self.port)
self.net_connect.send_command('en')
self.net_connect.send_command('cisco')
def add_vlan(self, number,vlanname):
# not sure you need to keep these 2 as properties of the class
self.number = number
self.vlanname = vlanname
self._connect()
# if you can use f-strings instead
# config_commands = [f'vlan {self.number}', f'name {self.vlanname}']
config_commands = [
'vlan {number}'.format(number=self.number),
'name {vlanname}'.format(vlanname=self.vlanname)]
return self.net_connect.send_config_set(config_commands)
def add_interface(self, interface, datavlan, voicevlan):
# not sure you need to keep these 3 as properties of the class
self.interface = interface
self.datavlan = datavlan
self.voiceclan = voicevlan
self._connect()
# if you can use f-strings instead
# config_commands = [f'interface gi{self.interface}', 'switchport',
# f'switchport access vlan {self.datavlan}', f'switchport voice vlan {self.voicevlan}']
config_commands = [
'interface gi{interface}'.format(interface=self.interface),
'switchport',
'switchport access vlan {datavlan}'.format(datavlan=self.datavlan),
'switchport voice vlan {voicevlan}'.format(voicelan=self.vlan)]
return net_connect.send_config_set(config_commands)
But basically, it can be shortened further to:
class Router:
def __init__(self,device_type,ip,username,password,port):
self.device_type = device_type
self.ip = ip
self.username = username
self.password = password
self.port = port
def _connect(self):
self.net_connect = ConnectHandler(device_type=self.device_type, ip=self.ip,
username=self.username, password=self.password,
port=self.port)
self.net_connect.send_command('en')
self.net_connect.send_command('cisco')
def add_vlan(self, number,vlanname):
self._connect()
config_commands = [f'vlan {number}', f'name {vlanname}']
return self.net_connect.send_config_set(config_commands)
def add_interface(self, interface, datavlan, voicevlan):
self._connect()
config_commands = [f'interface gi{interface}', 'switchport',
f'switchport access vlan {datavlan}',
f'switchport voice vlan {voicevlan}']
return net_connect.send_config_set(config_commands)
Then you can have one Router class. And as I said your methods should return someting, not None, that you pass as result argument in render_template(). That is if you want to display the result, of course.
By the way, not sure why you have this line
data = request.form.to_dict('number')
. You only work with
result
.