def test_backup_then_restore_from_zip(self): self._publish_transportation_form() initial_instance_count = Instance.objects.filter( xform=self.xform).count() # make submissions for i in range(len(self.surveys)): self._submit_transport_instance(i) instance_count = Instance.objects.filter(xform=self.xform).count() self.assertEqual(instance_count, initial_instance_count + len(self.surveys)) # make a backup temp_dir = tempfile.mkdtemp() zip_file = open(os.path.join(temp_dir, "backup.zip"), "wb") zip_file.close() create_zip_backup(zip_file.name, self.user, self.xform) # delete all the instances for instance in Instance.objects.filter(xform=self.xform): instance.delete() instance_count = Instance.objects.filter(xform=self.xform).count() # remove temp dir tree self.assertEqual(instance_count, 0) # restore instances self.assertTrue(os.path.exists(zip_file.name)) restore_backup_from_zip(zip_file.name, self.user.username) instance_count = Instance.objects.filter(xform=self.xform).count() # remove temp dir tree self.assertEqual(instance_count, len(self.surveys)) shutil.rmtree(temp_dir)
def test_backup_then_restore_from_zip(self): self._publish_transportation_form() initial_instance_count = Instance.objects.filter(xform=self.xform).count() # make submissions for i in range(len(self.surveys)): self._submit_transport_instance(i) instance_count = Instance.objects.filter(xform=self.xform).count() self.assertEqual(instance_count, initial_instance_count + len(self.surveys)) # make a backup temp_dir = tempfile.mkdtemp() zip_file = open(os.path.join(temp_dir, "backup.zip"), "wb") zip_file.close() create_zip_backup(zip_file.name, self.user, self.xform) # delete all the instances for instance in Instance.objects.filter(xform=self.xform): instance.delete() instance_count = Instance.objects.filter(xform=self.xform).count() # remove temp dir tree self.assertEqual(instance_count, 0) # restore instances self.assertTrue(os.path.exists(zip_file.name)) restore_backup_from_zip(zip_file.name, self.user.username) instance_count = Instance.objects.filter(xform=self.xform).count() # remove temp dir tree self.assertEqual(instance_count, len(self.surveys)) shutil.rmtree(temp_dir)
def handle(self, *args, **options): try: username = args[0] except IndexError: raise CommandError(_("You must provide the username to publish the" " form to.")) # make sure user exists try: user = User.objects.get(username=username) except User.DoesNotExist: raise CommandError(_("The user '%s' does not exist.") % username) try: input_file = args[1] except IndexError: raise CommandError(_("You must provide the path to restore from.")) else: input_file = os.path.realpath(input_file) num_instances, num_restored = restore_backup_from_zip( input_file, username) sys.stdout.write("Restored %d of %d submissions\n" % (num_restored, num_instances))