def inspect_device(device_id): """ Inspect and edit a device and all its related object. """ device = Device.query.filter_by(id=device_id).first_or_404() devicetype = device.devicetype device_form = DeviceForm(prefix="device", obj=device) devicetype_form = DeviceTypeForm(prefix="devicetype", obj=devicetype) lan_form = LanForm(prefix="lan", obj=device.lan) manufacturer_form = ManufacturerForm(prefix="manufacturer", obj=devicetype.manufacturer) new_lan_form = LanForm(prefix="new-lan") new_devicetype_form = DeviceTypeForm(prefix="new-devicetype") new_manufacturer_form = ManufacturerForm(prefix="new-manufacturer") if request.method == 'POST': device_form.chain_push_modified(request=request, model=Device, obj=device) devicetype_form.chain_push_modified(request=request, model=DeviceType, obj=devicetype) manufacturer_form.chain_push_modified(request=request, model=Manufacturer, obj=devicetype.manufacturer) lan_form.chain_push_modified(request=request, model=Lan, obj=device.lan) new_devicetype_form.chain_push_new(request=request, model=DeviceType) new_lan_form.chain_push_new(request=request, model=Lan) new_manufacturer_form.chain_push_new(request=request, model=Manufacturer) form_dict = dict(lan_form=lan_form, device_form=device_form, devicetype_form=devicetype_form, manufacturer_form=manufacturer_form, new_lan_form=new_lan_form, new_devicetype_form=new_devicetype_form, new_manufacturer_form=new_manufacturer_form) return render_template("inspect_device.html", device=device, active_page="devices", clear_pwd=device.decrypt_password(), clear_enapwd=device.decrypt_enapassword(), **form_dict)
def devices(): """ The devices view. Displays a list of available devices, as well as the forms to add a new one. As this view contains many forms, there is a mechanism to display them all and get only the one submitted. Example : In template : <button type="submit" class="btn btn-primary" name="btn" value="{{ device_form._prefix }}save-btn"> <span>Save</span> </button> The value will be : device-save-btn """ lan_form = LanForm(prefix="lan") device_form = DeviceForm(prefix="device") devicetype_form = DeviceTypeForm(prefix="devicetype") devicetypecategory_form = DeviceTypeCategoryForm(prefix="devicetypecategory") manufacturer_form = ManufacturerForm(prefix="manufacturer") form_dict = dict(lan_form=lan_form, device_form=device_form, devicetype_form=devicetype_form, devicetypecategory_form=devicetypecategory_form, manufacturer_form=manufacturer_form) if request.method == 'POST': device_form.chain_push_new(request=request, model=Device) devicetype_form.chain_push_new(request=request, model=DeviceType) devicetypecategory_form.chain_push_new(request=request, model=DeviceTypeCategory) lan_form.chain_push_new(request=request, model=Lan) manufacturer_form.chain_push_new(request=request, model=Manufacturer) return render_template('devices.html', devices=Device.query.all(), active_page="devices", **form_dict)
def add_device(): form_create = DeviceForm() form_create.status.choices = [('ON', 'ON'), ('OFF', 'OFF')] form_create.status.default = 'ON' form_create.role.choices = [('ENTRY', 'ENTRY'), ('EXIT', 'EXIT')] form_create.role.default = 'ENTRY' # device_names = obtain_device_names(device_container.get_list_of_devices()) form_update = UpdateDeviceForm() # UpdateDeviceForm(choices=device_names) form_update.status.choices = [('ON', 'ON'), ('OFF', 'OFF')] form_update.status.default = 'ON' form_update.role.choices = [('ENTRY', 'ENTRY'), ('EXIT', 'EXIT')] form_update.role.default = 'ENTRY' form_update.name.choices = [('', '')] for name in device_container.get_list_of_devices(): form_update.name.choices.append((name, name)) if request.method == 'POST': print("POST ") if form_create.validate_on_submit(): print('Adding new device') name = form_create.name.data new_status = form_create.status.data new_status_enum = DeviceStatus[new_status] video_source = form_create.video_source.data address = form_create.address.data listener_port = form_create.listener_port.data role = form_create.role.data role_enum = DeviceRole[role] capture_images = form_create.capture.data if address is None or any(local in address for local in ['localhost', '127.0.0.1']): location_enum = DeviceLocation.LOCAL else: location_enum = DeviceLocation.NONLOCAL response = handle_device_update(name, new_status_enum, video_source, location_enum, address, listener_port, role_enum, capture_images) flash(response) snapshot = device_container.get_devices_snapshot() print(snapshot is None) return render_template('device.html', title='WebServer', create_form=form_create, update_form=form_update, devices=snapshot)
def edit_device(id): form = DeviceForm() device = user_device.get_device_view_model(id) if form.validate_on_submit(): user_device.edit_device(id, form) else: form.device_name.default = device.device_name form.device_type.default = device.device_type form.process() return render_template('devices/edit_device.html', form=form, device=device)
def device_add(): form = DeviceForm() if form.validate_on_submit(): device = Device(ip=form.ip.data, name=form.name.data, devicetype=form.devicetype.data, location=form.location.data) db.session.add(device) db.session.commit() flash('Your device has been created!', 'success') return redirect(url_for('device_index')) return render_template('device_new.html', title='Add Device', form=form, legend='Add Device')
def index(): form=DeviceForm() if form.validate_on_submit(): device=Device.query.filter_by(device_id=form.device_id.data).first() # search the database for the user that has matching username if device is None: # if the username is not in the database or the password is does not match flash('Invalid Device ID.') return redirect(url_for('index')) else: if device.user_id is None: device.user_id=current_user.id db.session.commit() return redirect(url_for('device', device_id=str(device.device_id))) else: if device.user_id == current_user.id: return redirect(url_for('device', device_id=str(device.device_id))) elif device.user_id != current_user.id: flash('Invalid Device ID.') return redirect(url_for('index')) return render_template('index.html', title='Home', form=form) # returns the template with the now filled in variables
def inspect_device(device_id): """ Inspect and edit a device and all its related object. """ device = Device.query.filter_by(id=device_id).first_or_404() devicetype = device.devicetype device_form = DeviceForm(prefix="device", obj=device) devicetype_form = DeviceTypeForm(prefix="devicetype", obj=devicetype) devicetypecategory_form = DeviceTypeCategoryForm(prefix="devicetypecategory", obj=devicetype.devicetypecategory) lan_form = LanForm(prefix="lan", obj=device.lan) manufacturer_form = ManufacturerForm(prefix="manufacturer", obj=devicetype.manufacturer) new_lan_form = LanForm(prefix="new-lan") new_devicetype_form = DeviceTypeForm(prefix="new-devicetype") new_devicetypecategory_form = DeviceTypeCategoryForm(prefix="new-devicetypecategory") new_manufacturer_form = ManufacturerForm(prefix="new-manufacturer") if request.method == 'POST': device_form.chain_push_modified(request=request, model=Device, obj=device) devicetype_form.chain_push_modified(request=request, model=DeviceType, obj=devicetype) devicetypecategory_form.chain_push_modified(request=request, model=DeviceTypeCategory, obj=devicetype.devicetypecategory) manufacturer_form.chain_push_modified(request=request, model=Manufacturer, obj=devicetype.manufacturer) lan_form.chain_push_modified(request=request, model=Lan, obj=device.lan) new_devicetype_form.chain_push_new(request=request, model=DeviceType) new_devicetypecategory_form.chain_push_new(request=request, model=DeviceTypeCategory) new_lan_form.chain_push_new(request=request, model=Lan) new_manufacturer_form.chain_push_new(request=request, model=Manufacturer) form_dict = dict(lan_form=lan_form, device_form=device_form, devicetype_form=devicetype_form, devicetypecategory_form=devicetypecategory_form, manufacturer_form=manufacturer_form, new_lan_form=new_lan_form, new_devicetype_form=new_devicetype_form, new_manufacturer_form=new_manufacturer_form, new_devicetypecategory_form=new_devicetypecategory_form) return render_template("inspect_device.html", device=device, active_page="devices", clear_pwd=device.decrypt_password(),**form_dict)
def network(id): network = networks.query.filter_by(id=id).first() if network.net_type: servers_list = servers.query.filter_by(id_network=id).all() form = ServerForm() return render_template('servers.html', network=network, servers=servers_list, form=form) else: form = DeviceForm() devices_list = devices.query.filter_by(id_network=id).all() return render_template('network.html', network=network, devices=devices_list, form=form)
def update_device(): form_create = DeviceForm() form_create.status.choices = [('ON', 'ON'), ('OFF', 'OFF')] form_create.status.default = 'ON' form_create.role.choices = [('ENTRY', 'ENTRY'), ('EXIT', 'EXIT')] form_create.role.default = 'ENTRY' # device_names = obtain_device_names(device_container.get_list_of_devices()) form_update = UpdateDeviceForm() # UpdateDeviceForm(choices=device_names) form_update.status.choices = [('ON', 'ON'), ('OFF', 'OFF')] form_update.status.default = 'ON' form_update.role.choices = [('ENTRY', 'ENTRY'), ('EXIT', 'EXIT')] form_update.role.default = 'ENTRY' form_update.name.choices = [('', '')] for name in device_container.get_list_of_devices(): form_update.name.choices.append((name, name)) if form_update.validate_on_submit(): print('Updating existing device') name = form_update.name.data new_status = form_update.status.data new_status_enum = DeviceStatus[new_status] role = form_update.role.data role_enum = DeviceRole[role] video_source = form_create.video_source.data address = form_update.address.data listener_port = form_update.listener_port.data capture_images = form_update.capture.data response = handle_device_update(name, new_status_enum, video_source, None, address, listener_port, role_enum, capture_images) flash(response) return render_template('device.html', title='WebServer', create_form=form_create, update_form=form_update, devices=device_container.get_devices_snapshot())
def search(content): form = DeviceForm() #ПЕРЕПИСАТЬ! делаем запрос по каждому полю и добавляем в результирующий список results = devices.query.filter( devices.number.like('%' + str(content) + '%')).order_by("id").all() results.extend( devices.query.filter(devices.owner.like('%' + str(content) + '%')).order_by("id").all()) results.extend( devices.query.filter( devices.description.like('%' + str(content) + '%')).order_by("id").all()) #отбираем подсети только которые нашлись в поиске , чтобы вывод был красивым all_networks = { network for network in networks.query.all() for dev in results if dev.id_network == network.id } if len(results) == 0: flash('Ничего не найдено', 'warning') return render_template('search_dev_result.html', results=results, form=form, networks=all_networks)
def edit(obj, id): """ General page for editing items Args: obj: The type of the object to be edited id: ID of object being edited Returns: Confirmation of object edit """ # Check the object type if obj == "Bug": # Grab the bug from the database, and init the form with the bug's data # as default input bug = db.session.query(Bug).filter_by(id=id).first() form = BugForm(device_id=bug.device_id, tester_id=bug.tester_id) if form.validate_on_submit(): # Checking that parameters have been changed if bug.device_id == int(form.device_id.data) and bug.tester_id == int(form.tester_id.data): flash("Please change some parameters") return redirect(url_for("edit", obj="Bug", id=id)) # Decrement the bug from the experience it was originally part of exp = db.session.query(Experience).filter_by(device_id=int(bug.device_id)).filter(Experience.tester.id == bug.tester_id).first() exp.bugs -= 1 # Then increment the bug count of the experience it is now part of exp = db.session.query(Experience).filter_by(device_id=int(form.device_id.data)).filter(Experience.tester.id == int(form.tester_id.data)).first() if exp is None: exp = Experience(device_id = int(form.device_id.data), bugs=1) db.session.query(Tester).filter_by(id=int(form.tester_id.data)).first().experience.append(exp) else: exp.bugs += 1 # Make changes, commit, and redirect bug.device_id = int(form.device_id.data) bug.tester_id = int(form.tester_id.data) db.session.commit() flash(obj + " edited successfully") return redirect(url_for("devtools")) return render_template("edit.html", title="Edit Bug", form=form) elif obj == "Device": # Get the desired device from memory device = db.session.query(Device).filter_by(id=id).first() # Device does not exist error handling if device is None: flash("No " + obj + " with ID " + str(id)) return redirect(url_for("devtools")) # Init form with device default form = DeviceForm(device_name=device.device_name) if form.validate_on_submit(): # Change, commit, confirm, redirect device.device_name = form.device_name.data db.session.commit() flash(obj + " edited successfully") return redirect(url_for("devtools")) return render_template("edit.html", title="Edit Device", form=form) elif obj == "Tester": # Grab the tester and init the form with his.her defaults tester = db.session.query(Tester).filter_by(id=id).first() form = EditTesterForm(first_name=tester.first_name, last_name=tester.last_name, country=tester.country, devices=" ".join([str(d.id) for d in tester.devices])) if form.validate_on_submit(): # Change data, reset devices, commit, confirm, redirect tester.first_name = form.first_name.data tester.last_name = form.last_name.data tester.country = form.country.data for i in form.devices.data.split(" "): device = db.session.query(Device).filter_by(id=int(i)).first() if device not in tester.devices: tester.devices.append(device) db.session.commit() flash(obj + " edited successfully") return redirect(url_for("devtools")) return render_template("edit.html", title="Edit Tester", form=form) else: # Type unkown error handler flash("Object type not recognized") return redirect(url_for("devtools"))
def add(obj): """ General page for adding items Args: obj: The type of the object to be added Returns: Confirmation of object addition """ if obj == "Bug": # Initialize form that holds bug data form = BugForm() if form.validate_on_submit(): # Create an instance of the bug bug = Bug(device_id=int(form.device_id.data), tester_id=int(form.tester_id.data)) # Add to the database db.session.add(bug) # Check if the experience for this device/tester combo exists exp = db.session.query(Experience).filter_by(device_id=int(form.device_id.data)).filter(Experience.tester.id == int(form.tester_id.data)).first() # If it's new, make an instance of it, and append it to the # relevanet tester if exp is None: exp = Experience(device_id = int(form.device_id.data), bugs=1) db.session.query(Tester).filter_by(id=int(form.tester_id.data)).first().experience.append(exp) else: # If it's not, increment the number of bugs found for that # experience exp.bugs += 1 # Commit all changes db.session.commit() # Confirm addition and render flash(obj + " added successfully") return redirect(url_for("devtools")) return render_template("add.html", title="Add Bug", form=form) elif obj == "Device": # Initialize device data form form = DeviceForm() if form.validate_on_submit(): # Create a device instance and add it to the database device = Device(device_name=form.device_name.data) db.session.add(device) # Commit, confirm, and redirect db.session.commit() flash(obj + " added successfully") return redirect(url_for("devtools")) return render_template("add.html", title="Add Device", form=form) elif obj == "Tester": # Init the form, etc. etc. form = AddTesterForm() if form.validate_on_submit(): # Create the tester instance tester = Tester(first_name=form.first_name.data, last_name=form.last_name.data, country=form.country.data, last_login=datetime.strptime(form.last_login.data, '%Y-%m-%d %H:%M:%S')) # Add all devices to the devices relationship for i in form.devices.data.split(" "): device = db.session.query(Device).filter_by(id=int(i)).first() tester.devices.append(device) # Add, commit, confirm, redirect db.session.add(tester) db.session.commit() flash(obj + " added successfully") return redirect(url_for("devtools")) return render_template("add.html", title="Add Tester", form=form) else: # Object type error handling flash("Object type not recognized") return redirect(url_for("devtools"))