def load_documents(request): """ Load documents from uploaded file. """ form = ImportForm() if request.method == 'POST': # Accept the uploaded document data. file_data = None form = ImportForm(request.POST, request.FILES) if form.is_valid(): uploaded_file = request.FILES['uploads'] if uploaded_file.multiple_chunks(): file_data = open(uploaded_file.temporary_file_path(), 'r') else: file_data = uploaded_file.read() if file_data: # Try to import the data, but report any error that occurs. try: counter = Document.objects.load_json(request.user, file_data) user_msg = (ugettext('%(obj_count)d object(s) loaded.') % {'obj_count': counter, }) messages.add_message(request, messages.INFO, user_msg) except Exception as e: err_msg = (ugettext('Failed to import data: %(error)s') % {'error': '{0!s}'.format(e), }) messages.add_message(request, messages.ERROR, err_msg) context = {'import_file_form': form} return render(request, 'admin/wiki/document/load_data_form.html', context)
def load_documents(request): """ Load documents from uploaded file. """ form = ImportForm() if request.method == 'POST': # Accept the uploaded document data. file_data = None form = ImportForm(request.POST, request.FILES) if form.is_valid(): uploaded_file = request.FILES['file'] if uploaded_file.multiple_chunks(): file_data = open(uploaded_file.temporary_file_path(), 'r') else: file_data = uploaded_file.read() if file_data: # Try to import the data, but report any error that occurs. try: counter = Document.objects.load_json(request.user, file_data) user_msg = (_('%(obj_count)d object(s) loaded.') % { 'obj_count': counter, }) messages.add_message(request, messages.INFO, user_msg) except Exception, e: err_msg = (_('Failed to import data: %(error)s') % { 'error': '%s' % e, }) messages.add_message(request, messages.ERROR, err_msg)
def load_documents(request): """ Load documents from uploaded file. """ form = ImportForm() if request.method == 'POST': # Accept the uploaded document data. file_data = None form = ImportForm(request.POST, request.FILES) if form.is_valid(): uploaded_file = request.FILES['file'] if uploaded_file.multiple_chunks(): file_data = open(uploaded_file.temporary_file_path(), 'r') else: file_data = uploaded_file.read() if file_data: # Try to import the data, but report any error that occurs. try: counter = Document.objects.load_json(request.user, file_data) user_msg = (_('%(obj_count)d object(s) loaded.') % {'obj_count': counter, }) messages.add_message(request, messages.INFO, user_msg) except Exception, e: err_msg = (_('Failed to import data: %(error)s') % {'error': '%s' % e, }) messages.add_message(request, messages.ERROR, err_msg)
def load_documents(request): """ Load documents from uploaded file. """ form = ImportForm() if request.method == 'POST': # Accept the uploaded document data. file_data = None form = ImportForm(request.POST, request.FILES) if form.is_valid(): uploaded_file = request.FILES['uploads'] if uploaded_file.multiple_chunks(): file_data = open(uploaded_file.temporary_file_path(), 'r') else: file_data = uploaded_file.read() if file_data: # Try to import the data, but report any error that occurs. try: counter = Document.objects.load_json(request.user, file_data) user_msg = (ugettext('%(obj_count)d object(s) loaded.') % { 'obj_count': counter, }) messages.add_message(request, messages.INFO, user_msg) except Exception as e: err_msg = (ugettext('Failed to import data: %(error)s') % { 'error': '{0!s}'.format(e), }) messages.add_message(request, messages.ERROR, err_msg) context = {'import_file_form': form} return render(request, 'admin/wiki/document/load_data_form.html', context)
def test_requires_at_least_one_field(self): reload_module(settings) form = ImportForm({}, {}) self.assertFalse(form.is_valid()) self.assertEqual(form.errors, { '__all__': [ 'At least one fixture file needs to be uploaded or selected.' ]})
def test_invalid_file_extension(self): f = SimpleUploadedFile('invalid.txt', b'invalid') form = ImportForm({}, { 'uploads': f }) self.assertFalse(form.is_valid()) self.assertEqual({'uploads': ["Invalid file extension: .txt."]}, form.errors)
def test_mix_valid_and_invalid(self): form = ImportForm({}, MultiValueDict({ 'uploads': [ SimpleUploadedFile('valid.json', b'[]'), SimpleUploadedFile('invalid.txt', b'invalid') ] })) self.assertFalse(form.is_valid()) self.assertEqual({'uploads': ["Invalid file extension: .txt."]}, form.errors)
def test_store_checkbox(self): reload_module(settings) form = ImportForm() self.assertIsInstance(form['store'].field, BooleanField)
def test_valid_uppercase_file_extension(self): f = SimpleUploadedFile('valid.JSON', b'[]') form = ImportForm({}, { 'uploads': f }) self.assertTrue(form.is_valid())
def test_requires_file(self): form = ImportForm({}, {}) self.assertFalse(form.is_valid()) self.assertEqual({'uploads': ["This field is required."]}, form.errors)
def test_store_checkbox(self): form = ImportForm() self.assertIsInstance(form['store'].field, BooleanField)
def test_picked_files(self): reload_module(settings) form = ImportForm() self.assertIsInstance(form['picked_files'].field, FilePathField)
def test_picked_files(self): form = ImportForm() self.assertIsInstance(form['picked_files'].field, FilePathField)