def __func(): form = forms.ExportImportAppDevices(request.form) filename = form.filename.data if form.filename.data else core.config.paths.default_appdevice_export_path returned_json = {} apps = running_context.App.query.all() for app in apps: devices = [] for device in app.devices: device_json = device.as_json(with_apps=False) device_json.pop('app', None) device_json.pop('id', None) devices.append(device_json) returned_json[app.as_json()['name']] = devices try: with open(filename, 'w') as appdevice_file: appdevice_file.write( json.dumps(returned_json, indent=4, sort_keys=True)) except (OSError, IOError) as e: current_app.logger.error( 'Error importing devices from {0}: {1}'.format(filename, e)) return {"error": "Error writing file"}, IO_ERROR else: current_app.logger.debug( 'Exported devices to {0}'.format(filename)) return {}, SUCCESS
def __func(): form = forms.ExportImportAppDevices(request.form) filename = form.filename.data if form.filename.data else core.config.paths.default_appdevice_export_path try: with open(filename, 'r') as appdevice_file: read_file = appdevice_file.read() read_file = read_file.replace('\n', '') apps_devices = json.loads(read_file) except (OSError, IOError) as e: current_app.logger.error( 'Error importing devices from {0}: {1}'.format(filename, e)) return {"error": "Error reading file."}, IO_ERROR for app in apps_devices: for device in apps_devices[app]: extra_fields = {} for key in device: if key not in ['ip', 'name', 'port', 'username']: extra_fields[key] = device[key] extra_fields_str = json.dumps(extra_fields) running_context.Device.add_device( name=device['name'], username=device['username'], ip=device['ip'], port=device['port'], extra_fields=extra_fields_str, app_server=app, password=None) current_app.logger.debug('Imported devices from {0}'.format(filename)) return {}, SUCCESS
def export_devices(): form = forms.ExportImportAppDevices(request.form) filename = form.filename.data if form.filename.data else core.config.paths.default_appdevice_export_path returned_json = {} apps = running_context.App.query.all() for app in apps: devices = [] for device in app.devices: device_json = device.as_json(with_apps=False) device_json.pop('app', None) device_json.pop('id', None) devices.append(device_json) returned_json[app.as_json()['name']] = devices try: with open(filename, 'w') as appdevice_file: appdevice_file.write(json.dumps(returned_json, indent=4, sort_keys=True)) except (OSError, IOError): return json.dumps({"status": "error writing file"}) return json.dumps({"status": "success"})
def import_devices(): form = forms.ExportImportAppDevices(request.form) filename = form.filename.data if form.filename.data else core.config.paths.default_appdevice_export_path try: with open(filename, 'r') as appdevice_file: read_file = appdevice_file.read() read_file = read_file.replace('\n', '') apps_devices = json.loads(read_file) except (OSError, IOError): return json.dumps({"status": "error reading file"}) for app in apps_devices: for device in apps_devices[app]: extra_fields = {} for key in device: if key not in ['ip', 'name', 'port', 'username']: extra_fields[key] = device[key] extra_fields_str = json.dumps(extra_fields) running_context.Device.add_device(name=device['name'], username=device['username'], ip=device['ip'], port=device['port'], extra_fields=extra_fields_str, app_server=app, password='') return json.dumps({"status": "success"})