def post(self, request): form = DeviceBatchForm(data=request.POST, files=request.FILES) if not form.is_valid(): return Response({'form': form}, template_name='device_batch.html') data = form.cleaned_data try: msg = StringIO() management.call_command('device', batch_file=data['batch_file'], stdout=msg) messages.add_message(request._request, messages.INFO, msg.getvalue()) except CommandError as e: form._errors['batch_file'] = form.error_class([e]) return Response({'form': form}, template_name='device_batch.html') DeviceManager.clear() return HttpResponseRedirect(reverse('device-list'))
def put(self, request, *args, **kwargs): """ Function to save changes to multiple devices once. This function is called only when the "Save Changes" button is clicked on /devices/ page. However, it only supports enable/disable device(s). The url sent out will only include 'enable' field. """ DeviceFormSet = modelformset_factory(Device, form=DeviceListForm, extra=0) formset = DeviceFormSet(request.DATA) if formset.is_valid(): formset.save() DeviceManager.clear() messages.success(request._request, 'Changes successfully saved') if '/devices' not in request.META['HTTP_REFERER']: return HttpResponseRedirect(request.META['HTTP_REFERER']) else: return HttpResponseRedirect(reverse('device-list')) else: data = {'formset': formset, 'auth': Auth} return Response(data, template_name='device_list.html')
def post(self, request, device_id=None): if device_id: device = get_object_or_404(Device, pk=device_id) form = DeviceDetailForm(request.DATA, instance=device) else: form = DeviceDetailForm(request.DATA) if form.is_valid(): form.save() DeviceManager.clear() return HttpResponseRedirect(reverse('device-list')) else: return Response({'form': form, 'auth': Auth}, template_name='device_detail.html')
def handle(self, *args, **options): self.stdout.write('Reloading report objects ... ') management.call_command('clean_pyc', path=settings.PROJECT_ROOT) self.importer = Importer(buf=self.stdout) if options['report_id']: # single report report_id = options['report_id'] pk = int(report_id) report = Report.objects.get(pk=pk) management.call_command('clean', applications=False, report_id=report_id, clear_cache=False, clear_logs=False) DeviceManager.clear() self.import_module(report.sourcefile) elif options['report_name']: name = options['report_name'] try: report = Report.objects.get(sourcefile__endswith=name) sourcefile = report.sourcefile management.call_command('clean', applications=False, report_id=report.id, clear_cache=False, clear_logs=False) self.import_module(sourcefile) except ObjectDoesNotExist: self.import_module(name) DeviceManager.clear() elif options['namespace']: reports = Report.objects.filter(namespace=options['namespace']) self.capture_enabled(reports) # clear all data from one namespace (module) for report in reports: management.call_command('clean', applications=False, report_id=report.id, clear_cache=False, clear_logs=False) # ignore all dirs besides the one we cleared, then import root_dir = settings.REPORTS_DIR target_dir = options['namespace'] ignore_list = [ os.path.basename(os.path.normpath(x)) for x in os.listdir(root_dir) if x != target_dir ] self.importer.import_directory(root_dir, ignore_list=ignore_list) self.apply_enabled() else: self.capture_enabled() # clear all data management.call_command('clean', applications=True, report_id=None, clear_cache=True, clear_logs=False) # start with fresh device instances DeviceManager.clear() report_dir = settings.REPORTS_DIR self.importer.import_directory(report_dir, report_name=None) self.apply_enabled()
def handle(self, *args, **options): self.stdout.write('Reloading report objects ... ') management.call_command('clean_pyc', path=settings.PROJECT_ROOT) management.call_command('syncdb', interactive=False) self.importer = Importer(buf=self.stdout) if options['report_id']: # single report report_id = options['report_id'] pk = int(report_id) report = Report.objects.get(pk=pk) management.call_command('clean', applications=False, report_id=report_id, clear_cache=False, clear_logs=False) DeviceManager.clear() self.import_module(report.sourcefile) elif options['report_name']: name = options['report_name'] try: report = Report.objects.get(sourcefile__endswith=name) sourcefile = report.sourcefile management.call_command('clean', applications=False, report_id=report.id, clear_cache=False, clear_logs=False) self.import_module(sourcefile) except ObjectDoesNotExist: self.import_module(name) DeviceManager.clear() elif options['namespace']: reports = Report.objects.filter(namespace=options['namespace']) self.capture_enabled(reports) # clear all data from one namespace (module) for report in reports: management.call_command('clean', applications=False, report_id=report.id, clear_cache=False, clear_logs=False) # ignore all dirs besides the one we cleared, then import root_dir = settings.REPORTS_DIR target_dir = options['namespace'] ignore_list = [os.path.basename(os.path.normpath(x)) for x in os.listdir(root_dir) if x != target_dir] self.importer.import_directory( root_dir, ignore_list=ignore_list) self.apply_enabled() else: self.capture_enabled() # clear all data management.call_command('clean', applications=True, report_id=None, clear_cache=True, clear_logs=False) # start with fresh device instances DeviceManager.clear() report_dir = settings.REPORTS_DIR self.importer.import_directory(report_dir, report_name=None) self.apply_enabled()
def test_clear_with_device_id(self): DeviceManager.clear(device_id=1) self.assertFalse(1 in DeviceManager.devices)
def test_clear(self): DeviceManager.clear() self.assertEqual(DeviceManager.devices, {})