def load_data(request): """ Load data from uploaded file or disk. Note: A uploaded file will be saved on `SMUGGLER_FIXTURE_DIR` if the submit button with name "_loadandsave" was pressed. """ form = ImportFileForm() if request.method == 'POST': data = [] if request.POST.has_key('_load') or request.POST.has_key('_loadandsave'): form = ImportFileForm(request.POST, request.FILES) if form.is_valid(): uploaded_file = request.FILES['file'] file_name = uploaded_file.name file_format = file_name.split('.')[-1] if request.POST.has_key('_loadandsave'): destination_path = os.path.join(SMUGGLER_FIXTURE_DIR, file_name) save_uploaded_file_on_disk(uploaded_file, destination_path) file_data = open(destination_path, 'r') elif uploaded_file.multiple_chunks(): file_data = open(uploaded_file.temporary_file_path(), 'r') else: file_data = uploaded_file.read() data.append((file_format, file_data)) elif request.POST.has_key('_loadfromdisk'): query_dict = request.POST.copy() del(query_dict['_loadfromdisk']) del(query_dict['csrfmiddlewaretoken']) selected_files = query_dict.values() for file_name in selected_files: file_path = os.path.join(SMUGGLER_FIXTURE_DIR, file_name) file_format = file_name.split('.')[-1] file_data = open(file_path, 'r') data.append((file_format, file_data)) counter = 0 for format, stream in data: objects = serializers.deserialize(format, stream) for obj in objects: counter += 1 obj.save() if data: user_msg = ('%(obj_count)d object(s) from %(file_count)d file(s) ' 'loaded with success.') # TODO: pluralize user_msg = _(user_msg) % { 'obj_count': counter, 'file_count': len(data) } messages.add_message(request, messages.INFO, user_msg) context = { 'files_available': get_file_list(SMUGGLER_FIXTURE_DIR) \ if SMUGGLER_FIXTURE_DIR else [], 'smuggler_fixture_dir': SMUGGLER_FIXTURE_DIR, 'import_file_form': form, } return render_to_response('smuggler/load_data_form.html', context, context_instance=RequestContext(request))
def load_data(request): """ Load data from uploaded file or disk. Note: A uploaded file will be saved on `SMUGGLER_FIXTURE_DIR` if the submit button with name "_loadandsave" was pressed. """ form = ImportFileForm() if request.method == 'POST': data = [] if request.POST.has_key('_load') or request.POST.has_key( '_loadandsave'): form = ImportFileForm(request.POST, request.FILES) if form.is_valid(): uploaded_file = request.FILES['file'] file_name = uploaded_file.name file_format = file_name.split('.')[-1] if request.POST.has_key('_loadandsave'): destination_path = os.path.join(SMUGGLER_FIXTURE_DIR, file_name) save_uploaded_file_on_disk(uploaded_file, destination_path) file_data = open(destination_path, 'r') elif uploaded_file.multiple_chunks(): file_data = open(uploaded_file.temporary_file_path(), 'r') else: file_data = uploaded_file.read() data.append((file_format, file_data)) elif request.POST.has_key('_loadfromdisk'): query_dict = request.POST.copy() del (query_dict['_loadfromdisk']) del (query_dict['csrfmiddlewaretoken']) selected_files = query_dict.values() for file_name in selected_files: file_path = os.path.join(SMUGGLER_FIXTURE_DIR, file_name) file_format = file_name.split('.')[-1] file_data = open(file_path, 'r') data.append((file_format, file_data)) if data: try: obj_count = load_requested_data(data) user_msg = ( '%(obj_count)d object(s) from %(file_count)d file(s) ' 'loaded with success.') # TODO: pluralize user_msg = _(user_msg) % { 'obj_count': obj_count, 'file_count': len(data) } messages.add_message(request, messages.INFO, user_msg) except (IntegrityError, ObjectDoesNotExist), e: messages.add_message( request, messages.ERROR, _(u'An exception occurred while loading data: %s') % unicode(e))
def load_data(request): """ Load data from uploaded file or disk. Note: A uploaded file will be saved on `SMUGGLER_FIXTURE_DIR` if the submit button with name "_loadandsave" was pressed. """ form = ImportFileForm() if request.method == "POST": data = [] if request.POST.has_key("_load") or request.POST.has_key("_loadandsave"): form = ImportFileForm(request.POST, request.FILES) if form.is_valid(): uploaded_file = request.FILES["file"] file_name = uploaded_file.name file_format = file_name.split(".")[-1] if request.POST.has_key("_loadandsave"): destination_path = os.path.join(SMUGGLER_FIXTURE_DIR, file_name) save_uploaded_file_on_disk(uploaded_file, destination_path) file_data = open(destination_path, "r") elif uploaded_file.multiple_chunks(): file_data = open(uploaded_file.temporary_file_path(), "r") else: file_data = uploaded_file.read() data.append((file_format, file_data)) elif request.POST.has_key("_loadfromdisk"): query_dict = request.POST.copy() del (query_dict["_loadfromdisk"]) del (query_dict["csrfmiddlewaretoken"]) selected_files = query_dict.values() for file_name in selected_files: file_path = os.path.join(SMUGGLER_FIXTURE_DIR, file_name) file_format = file_name.split(".")[-1] file_data = open(file_path, "r") data.append((file_format, file_data)) counter = 0 for format, stream in data: objects = serializers.deserialize(format, stream) for obj in objects: counter += 1 obj.save() if data: user_msg = "%(obj_count)d object(s) from %(file_count)d file(s) " "loaded with success." # TODO: pluralize user_msg = _(user_msg) % {"obj_count": counter, "file_count": len(data)} request.user.message_set.create(message=user_msg) context = { "files_available": get_file_list(SMUGGLER_FIXTURE_DIR) if SMUGGLER_FIXTURE_DIR else [], "smuggler_fixture_dir": SMUGGLER_FIXTURE_DIR, "import_file_form": form, } return render_to_response("smuggler/load_data_form.html", context, context_instance=RequestContext(request))
def load_data(request): """ Load data from uploaded file or disk. Note: A uploaded file will be saved on `SMUGGLER_FIXTURE_DIR` if the submit button with name "_loadandsave" was pressed. """ form = ImportFileForm() if request.method == 'POST': data = [] if request.POST.has_key('_load') or request.POST.has_key('_loadandsave'): form = ImportFileForm(request.POST, request.FILES) if form.is_valid(): uploaded_file = request.FILES['file'] file_name = uploaded_file.name file_format = file_name.split('.')[-1] if request.POST.has_key('_loadandsave'): destination_path = os.path.join(SMUGGLER_FIXTURE_DIR, file_name) save_uploaded_file_on_disk(uploaded_file, destination_path) file_data = open(destination_path, 'r') elif uploaded_file.multiple_chunks(): file_data = open(uploaded_file.temporary_file_path(), 'r') else: file_data = uploaded_file.read() data.append((file_format, file_data)) elif request.POST.has_key('_loadfromdisk'): query_dict = request.POST.copy() del(query_dict['_loadfromdisk']) del(query_dict['csrfmiddlewaretoken']) selected_files = query_dict.values() for file_name in selected_files: file_path = os.path.join(SMUGGLER_FIXTURE_DIR, file_name) file_format = file_name.split('.')[-1] file_data = open(file_path, 'r') data.append((file_format, file_data)) if data: try: obj_count = load_requested_data(data) user_msg = ('%(obj_count)d object(s) from %(file_count)d file(s) ' 'loaded with success.') # TODO: pluralize user_msg = _(user_msg) % { 'obj_count': obj_count, 'file_count': len(data) } messages.add_message(request, messages.INFO, user_msg) except (IntegrityError, ObjectDoesNotExist), e: messages.add_message(request, messages.ERROR, _(u'An exception occurred while loading data: %s') % unicode(e))
def form_valid(self, form): uploads = form.cleaned_data.get('uploads', []) store = form.cleaned_data.get('store', False) picked_files = form.cleaned_data.get('picked_files', []) fixtures = [] tmp_fixtures = [] for upload in uploads: file_name = upload.name if store: # Store the file in SMUGGLER_FIXTURE_DIR destination_path = os.path.join( settings.SMUGGLER_FIXTURE_DIR, file_name) save_uploaded_file_on_disk(upload, destination_path) else: # Store the file in a tmp file prefix, suffix = os.path.splitext(file_name) destination_path = tempfile.mkstemp( suffix=suffix, prefix=prefix + '_')[1] save_uploaded_file_on_disk(upload, destination_path) tmp_fixtures.append(destination_path) fixtures.append(destination_path) for file_name in picked_files: fixtures.append(file_name) try: obj_count = load_fixtures(fixtures) user_msg = ' '.join([ ungettext_lazy( 'Successfully imported %(count)d file.', 'Successfully imported %(count)d files.', len(fixtures) ) % {'count': len(fixtures)}, ungettext_lazy( 'Loaded %(count)d object.', 'Loaded %(count)d objects.', obj_count ) % {'count': obj_count}]) messages.info(self.request, user_msg) except (IntegrityError, ObjectDoesNotExist, DeserializationError, CommandError) as e: messages.error( self.request, _('An exception occurred while loading data: %s') % str(e)) finally: # Remove our tmp files for tmp_file in tmp_fixtures: os.unlink(tmp_file) return super(LoadDataView, self).form_valid(form)
def form_valid(self, form): uploads = form.cleaned_data.get('uploads', []) store = form.cleaned_data.get('store', False) picked_files = form.cleaned_data.get('picked_files', []) fixtures = [] tmp_fixtures = [] for upload in uploads: file_name = upload.name if store: # Store the file in SMUGGLER_FIXTURE_DIR destination_path = os.path.join( smuggler_settings.SMUGGLER_FIXTURE_DIR, file_name) save_uploaded_file_on_disk(upload, destination_path) else: # Store the file in a tmp file prefix, suffix = os.path.splitext(file_name) destination_path = tempfile.mkstemp(suffix=suffix, prefix=prefix + '_')[1] save_uploaded_file_on_disk(upload, destination_path) tmp_fixtures.append(destination_path) fixtures.append(destination_path) for file_name in picked_files: fixtures.append(file_name) try: prepare_time = datetime.timedelta( minutes=settings.BACKUP_RESTORE_PREPARE_TIME) eta = datetime.datetime.utcnow() + prepare_time local_time = localtime(now()) + prepare_time load_dump.apply_async((fixtures, tmp_fixtures), eta=eta) cache.add('BACKUP_RESTORE_TIME', local_time, prepare_time.total_seconds()) cache.add('BACKUP_FIXTURES', fixtures, prepare_time.total_seconds()) messages.success(self.request, 'Восстановление из бекапа было запущено') except: async_mail_admins( subject="Бекап системы", message="Не удалось запустить восстановление из бекапа.", fail_silently=False, html_message=None) messages.error(self.request, 'Не удалось запустить восстановление из бекапа.') return super(LoadBackupView, self).form_valid(form)
def test_save_uploaded_file_on_disk(self): path = p('..', 'smuggler_fixtures', 'test.json') save_uploaded_file_on_disk(SimpleUploadedFile('test.json', b'[]'), path) self.assertTrue(os.path.exists(path)) os.unlink(path)
def test_save_uploaded_file_on_disk(self): path = p("..", "smuggler_fixtures", "test.json") save_uploaded_file_on_disk(SimpleUploadedFile("test.json", b"[]"), path) self.assertTrue(os.path.exists(path)) os.unlink(path)
def test_save_uploaded_file_on_disk(self): path = p('..', 'smuggler_fixtures', 'test.json') save_uploaded_file_on_disk( SimpleUploadedFile('test.json', b'[]'), path) self.assertTrue(os.path.exists(path)) os.unlink(path)