def test_catalog_import_function_sheet_fails(self): path = 'sdiuyf' with patch('catalog.utils.SimpleSheet') as mock_sheet: mock_sheet.side_effect = ValueError with patch('catalog.utils._catalog_import_from_simple_sheet'): with self.assertRaises(CatalogImportFailure): catalog_import(path) mock_sheet.assertCalledWith(path)
def form_valid(self, form): f = self.request.FILES['file'] path = None try: if hasattr(f, 'temporary_file_path'): # Django already saved the uploaded file to our disk path = f.temporary_file_path() else: # We need to save it ourselves tmpf = NamedTemporaryFile(mode='wb', delete=False) path = tmpf.name for chunk in f.chunks(): tmpf.write(chunk) tmpf.close() try: num_new = catalog_import(path) except CatalogImportFailure as e: error_msg = "Something went wrong importing the catalog, nothing was imported." for err in e.errlist: error_msg = '%s\n %s' % (error_msg, err) form.errors['__all__'] = error_msg return self.form_invalid(form) messages.info( self.request, "Imported %d new item%s" % (num_new, '' if num_new == 1 else 's')) finally: if path and os.path.exists(path): os.remove(path) return super(CatalogImportView, self).form_valid(form)
def form_valid(self, form): f = self.request.FILES['file'] path = None try: if hasattr(f, 'temporary_file_path'): # Django already saved the uploaded file to our disk path = f.temporary_file_path() else: # We need to save it ourselves tmpf = NamedTemporaryFile(mode='wb', delete=False) path = tmpf.name for chunk in f.chunks(): tmpf.write(chunk) tmpf.close() try: num_new = catalog_import(path) except CatalogImportFailure as e: error_msg = "Something went wrong importing the catalog, nothing was imported." for err in e.errlist: error_msg = '%s\n %s' % (error_msg, err) form.errors['__all__'] = error_msg return self.form_invalid(form) messages.info(self.request, "Imported %d new item%s" % (num_new, '' if num_new == 1 else 's')) finally: if path and os.path.exists(path): os.remove(path) return super(CatalogImportView, self).form_valid(form)
def test_catalog_import_function(self): path = 'sdiuyf' with patch('catalog.utils.SimpleSheet') as mock_sheet: with patch('catalog.utils._catalog_import_from_simple_sheet') as mock_import: mock_import.return_value = 666 retval = catalog_import(path) self.assertEqual(666, retval) mock_sheet.assertCalledWith(path)
def test_catalog_import_function(self): path = 'sdiuyf' with patch('catalog.utils.SimpleSheet') as mock_sheet: with patch('catalog.utils._catalog_import_from_simple_sheet' ) as mock_import: mock_import.return_value = 666 retval = catalog_import(path) self.assertEqual(666, retval) mock_sheet.assertCalledWith(path)