def edit_devices(): error = None if request.method == 'POST': # check if this is remove vs update POST request action = request.form.getlist('action') if action[0] == 'remove': # read in the content of a present click list_value = request.form.getlist('id') # if list is not empty - content has gone through if len(list_value) > 0: value = int(list_value[0]) # find device and rules associated with it device = Devices.get(Devices.deviceid == value) # recursively - delete all dependencies incl. nullable device.delete_instance(recursive=True, delete_nullable=True) elif action[0] == 'update': # read in the content of a present click list_id_value = request.form.getlist('id') print list_id_value # if list is not empty - content has gone through if len(list_id_value) > 0: id_value = int(list_id_value[0]) print "value is", id_value # find device and rules associated with it device = Devices.get(Devices.deviceid == id_value) list_name_value = request.form.getlist('name') list_descr_value = request.form.getlist('descr') list_ver_value = request.form.getlist('ver') if list_name_value[0] != '': if list_name_value[0] != device.devicename: device.devicename = list_name_value[0] elif list_descr_value[0] != device.description: device.description = list_descr_value[0] elif list_ver_value[0] != device.version: device.version = list_ver_value[0] device.save() else: error = 'Name cannot be empty' else: # list devices entries = [ dict(did=device.deviceid, dname=device.devicename, ddescr=device.description, dver=device.version) for device in Devices.select() ] return render_template('edit_devices.html', error=error, entries=entries)
def add_device(): error = None if request.method == 'POST': _devicename = request.form['devicename'].strip() _description = request.form['description'].strip() _version = request.form['version'].strip() # validate the recieved values if _devicename: # make sure device doesn't exist already try: device = Devices.get(Devices.devicename==_devicename) if device: error = 'Device "' + _devicename + '" already exists' except Devices.DoesNotExist: device = Devices(devicename=_devicename, description=_description, version=_version) device.save() global added_device added_device = {'object': device, 'deviceid': device.deviceid, 'devicename': _devicename, 'description': _description, 'version': _version} # add a successful completion mark flash('Device "' + _devicename + '" was successfully added.') return redirect(url_for('index')) else: error = 'Enter the required fields' return render_template('add_device.html', error=error)
def add_device(): error = None if request.method == 'POST': _devicename = request.form['devicename'].strip() _description = request.form['description'].strip() _version = request.form['version'].strip() # validate the recieved values if _devicename: # make sure device doesn't exist already try: device = Devices.get(Devices.devicename == _devicename) if device: error = 'Device "' + _devicename + '" already exists' except Devices.DoesNotExist: device = Devices(devicename=_devicename, description=_description, version=_version) device.save() global added_device added_device = { 'object': device, 'deviceid': device.deviceid, 'devicename': _devicename, 'description': _description, 'version': _version } # add a successful completion mark flash('Device "' + _devicename + '" was successfully added.') return redirect(url_for('index')) else: error = 'Enter the required fields' return render_template('add_device.html', error=error)
def edit_devices(): error = None if request.method == 'POST': # check if this is remove vs update POST request action = request.form.getlist('action') if action[0] == 'remove': # read in the content of a present click list_value = request.form.getlist('id') # if list is not empty - content has gone through if len(list_value) > 0: value = int(list_value[0]) # find device and rules associated with it device = Devices.get(Devices.deviceid == value) # recursively - delete all dependencies incl. nullable device.delete_instance(recursive=True,delete_nullable=True) elif action[0] == 'update': # read in the content of a present click list_id_value = request.form.getlist('id') print list_id_value # if list is not empty - content has gone through if len(list_id_value) > 0: id_value = int(list_id_value[0]) print "value is", id_value # find device and rules associated with it device = Devices.get(Devices.deviceid == id_value) list_name_value = request.form.getlist('name') list_descr_value = request.form.getlist('descr') list_ver_value = request.form.getlist('ver') if list_name_value[0] != '': if list_name_value[0] != device.devicename: device.devicename = list_name_value[0] elif list_descr_value[0] != device.description: device.description = list_descr_value[0] elif list_ver_value[0] != device.version: device.version = list_ver_value[0] device.save() else: error = 'Name cannot be empty' else: # list devices entries = [dict(did=device.deviceid, dname=device.devicename, ddescr=device.description, dver=device.version) for device in Devices.select()] return render_template('edit_devices.html', error=error, entries=entries)
def generate(): params_list = params.split(',') if int(params_list[0]) == 0 and int(params_list[1]) == 0: db_query = [dict(rid=drule.ruleid, did=Devices.get(Devices.deviceid==drule.deviceid.deviceid).devicename, tid=Tokens.get(Tokens.tokenid==drule.tokenid.tokenid).token, rule=drule.ruleline, condition=drule.cond, config=drule.configcommand) for drule in Devicerules.select()] elif int(params_list[0]) == 0: db_query = [dict(rid=drule.ruleid, did=Devices.get(Devices.deviceid==drule.deviceid.deviceid).devicename, tid=Tokens.get(Tokens.tokenid==drule.tokenid.tokenid).token, rule=drule.ruleline, condition=drule.cond, config=drule.configcommand) for drule in Devicerules.select().where(Devicerules.tokenid==params_list[1])] elif int(params_list[1]) == 0: db_query = [dict(rid=drule.ruleid, did=Devices.get(Devices.deviceid==drule.deviceid.deviceid).devicename, tid=Tokens.get(Tokens.tokenid==drule.tokenid.tokenid).token, rule=drule.ruleline, condition=drule.cond, config=drule.configcommand) for drule in Devicerules.select().where(Devicerules.deviceid==params_list[0])] else: db_query = [dict(rid=drule.ruleid, did=Devices.get(Devices.deviceid==drule.deviceid.deviceid).devicename, tid=Tokens.get(Tokens.tokenid==drule.tokenid.tokenid).token, rule=drule.ruleline, condition=drule.cond, config=drule.configcommand) for drule in Devicerules.select().where(Devicerules.deviceid==params_list[0], Devicerules.tokenid==params_list[1])] table_start = '<table class="table table-condensed"><tr><th>Device</th><th>Token</th><th>Rule line</th><th>Condition</th><th>Configuration command</th></tr>' q = '' for each in db_query: q += '<tr>' q += '<td>' + str(each.get('did')) + '</td>' q += '<td>' + str(each.get('tid')) + '</td>' q += '<td>' + str(each.get('rule')) + '</td>' q += '<td>' + str(each.get('condition')) + '</td>' q += '<td>' + str(each.get('config')) + '</td>' q += '</tr>' table_end = '</table>' res = table_start + q + table_end yield res
def generate(): params_list = params.split(',') if int(params_list[0]) == 0 and int(params_list[1]) == 0: db_query = [ dict(rid=drule.ruleid, did=Devices.get(Devices.deviceid == drule.deviceid.deviceid).devicename, tid=Tokens.get( Tokens.tokenid == drule.tokenid.tokenid).token, rule=drule.ruleline, condition=drule.cond, config=drule.configcommand) for drule in Devicerules.select() ] elif int(params_list[0]) == 0: db_query = [ dict(rid=drule.ruleid, did=Devices.get(Devices.deviceid == drule.deviceid.deviceid).devicename, tid=Tokens.get( Tokens.tokenid == drule.tokenid.tokenid).token, rule=drule.ruleline, condition=drule.cond, config=drule.configcommand) for drule in Devicerules.select().where( Devicerules.tokenid == params_list[1]) ] elif int(params_list[1]) == 0: db_query = [ dict(rid=drule.ruleid, did=Devices.get(Devices.deviceid == drule.deviceid.deviceid).devicename, tid=Tokens.get( Tokens.tokenid == drule.tokenid.tokenid).token, rule=drule.ruleline, condition=drule.cond, config=drule.configcommand) for drule in Devicerules.select().where( Devicerules.deviceid == params_list[0]) ] else: db_query = [ dict(rid=drule.ruleid, did=Devices.get(Devices.deviceid == drule.deviceid.deviceid).devicename, tid=Tokens.get( Tokens.tokenid == drule.tokenid.tokenid).token, rule=drule.ruleline, condition=drule.cond, config=drule.configcommand) for drule in Devicerules.select().where( Devicerules.deviceid == params_list[0], Devicerules.tokenid == params_list[1]) ] table_start = '<table class="table table-condensed"><tr><th>Device</th><th>Token</th><th>Rule line</th><th>Condition</th><th>Configuration command</th></tr>' q = '' for each in db_query: q += '<tr>' q += '<td>' + str(each.get('did')) + '</td>' q += '<td>' + str(each.get('tid')) + '</td>' q += '<td>' + str(each.get('rule')) + '</td>' q += '<td>' + str(each.get('condition')) + '</td>' q += '<td>' + str(each.get('config')) + '</td>' q += '</tr>' table_end = '</table>' res = table_start + q + table_end yield res