Пример #1
0
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)
Пример #2
0
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)
Пример #3
0
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)
Пример #4
0
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)
Пример #5
0
 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.'
         ]})
Пример #6
0
 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)
Пример #7
0
 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)
Пример #8
0
 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.'
         ]})
Пример #9
0
 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)
Пример #10
0
 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)
Пример #11
0
 def test_store_checkbox(self):
     reload_module(settings)
     form = ImportForm()
     self.assertIsInstance(form['store'].field, BooleanField)
Пример #12
0
 def test_valid_uppercase_file_extension(self):
     f = SimpleUploadedFile('valid.JSON', b'[]')
     form = ImportForm({}, {
         'uploads': f
     })
     self.assertTrue(form.is_valid())
Пример #13
0
 def test_requires_file(self):
     form = ImportForm({}, {})
     self.assertFalse(form.is_valid())
     self.assertEqual({'uploads': ["This field is required."]},
                      form.errors)
Пример #14
0
 def test_store_checkbox(self):
     form = ImportForm()
     self.assertIsInstance(form['store'].field, BooleanField)
Пример #15
0
 def test_picked_files(self):
     reload_module(settings)
     form = ImportForm()
     self.assertIsInstance(form['picked_files'].field, FilePathField)
Пример #16
0
 def test_picked_files(self):
     form = ImportForm()
     self.assertIsInstance(form['picked_files'].field, FilePathField)
Пример #17
0
 def test_valid_uppercase_file_extension(self):
     f = SimpleUploadedFile('valid.JSON', b'[]')
     form = ImportForm({}, {
         'uploads': f
     })
     self.assertTrue(form.is_valid())
Пример #18
0
 def test_requires_file(self):
     form = ImportForm({}, {})
     self.assertFalse(form.is_valid())
     self.assertEqual({'uploads': ["This field is required."]},
                      form.errors)