def handle(self, *args, **options): try: output_file = args[0] except IndexError: raise CommandError( _("Provide the path to the zip file to backup" " to")) else: output_file = os.path.realpath(output_file) try: username = args[1] 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: id_string = args[2] except IndexError: xform = None else: # make sure xform exists try: xform = XForm.objects.get(user=user, id_string=id_string) except XForm.DoesNotExist: raise CommandError( _("The id_string '%s' does not exist.") % id_string) create_zip_backup(output_file, user, xform)
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: id_string = args[1] except IndexError: raise CommandError(_( "You must provide the id_string of the form.")) # make sure user exists try: xform = XForm.objects.get(user=user, id_string=id_string) except XForm.DoesNotExist: raise CommandError(_("The id_string '%s' does not exist.") % id_string) try: output_file = args[2] except IndexError: raise CommandError(_("You must provide the path to save the" " restore file to.")) else: output_file = os.path.realpath(output_file) create_zip_backup(output_file, user, xform)
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)