def restore_backup_from_path(dir_path, username, status): """ Only restores xml submissions, media files are assumed to still be in storage and will be retrieved by the filename stored within the submission """ num_instances = 0 num_restored = 0 for dir_path, dir_names, file_names in os.walk(dir_path): for file_name in file_names: # check if its a valid xml instance xml_instance_path = os.path.join(dir_path, file_name) num_instances += 1 xml_file = django_file(xml_instance_path, field_name="xml_file", content_type="text/xml") media_files = [] date_created = None try: date_created = _date_created_from_filename(file_name) except ValueError as e: sys.stderr.write("Couldn't determine date created from filename: '%s'\n" % file_name) else: sys.stdout.write("Creating instance from '%s'\n" % file_name) try: instance = create_instance(username, xml_file, media_files, date_created_override=date_created) num_restored += 1 except Exception as e: sys.stderr.write("Couldn't restote %s, create instance said: %s\n" % (file_name, e)) return num_instances, num_restored
def test_date_created_override(self): """ Test that passing a date_created_override when creating and instance will set our date as the date_created """ xml_file_path = os.path.join( settings.PROJECT_ROOT, "odk_logger", "fixtures", "tutorial", "instances", "tutorial_2012-06-27_11-27-53.xml" ) xml_file = django_file(xml_file_path, field_name="xml_file", content_type="text/xml") media_files = [] date_created = datetime.strptime("2013-01-01 12:00:00", "%Y-%m-%d %H:%M:%S") instance = create_instance(self.user.username, xml_file, media_files, date_created_override=date_created) self.assertIsNotNone(instance) self.assertEqual( instance.date_created.strftime("%Y-%m-%d %H:%M:%S"), date_created.strftime("%Y-%m-%d %H:%M:%S") )
def test_date_created_override(self): """ Test that passing a date_created_override when creating and instance will set our date as the date_created """ xml_file_path = os.path.join(settings.PROJECT_ROOT, "odk_logger", "fixtures", "tutorial", "instances", "tutorial_2012-06-27_11-27-53.xml") xml_file = django_file(xml_file_path, field_name="xml_file", content_type="text/xml") media_files = [] date_created = datetime.strptime("2013-01-01 12:00:00", "%Y-%m-%d %H:%M:%S") instance = create_instance(self.user.username, xml_file, media_files, date_created_override=date_created) self.assertIsNotNone(instance) self.assertEqual(instance.date_created.strftime("%Y-%m-%d %H:%M:%S"), date_created.strftime("%Y-%m-%d %H:%M:%S"))
def restore_backup_from_path(dir_path, username, status): """ Only restores xml submissions, media files are assumed to still be in storage and will be retrieved by the filename stored within the submission """ num_instances = 0 num_restored = 0 for dir_path, dir_names, file_names in os.walk(dir_path): for file_name in file_names: # check if its a valid xml instance xml_instance_path = os.path.join(dir_path, file_name) num_instances += 1 xml_file = django_file(xml_instance_path, field_name="xml_file", content_type="text/xml") media_files = [] date_created = None try: date_created = _date_created_from_filename(file_name) except ValueError as e: sys.stderr.write( "Couldn't determine date created from filename: '%s'\n" % file_name) else: sys.stdout.write("Creating instance from '%s'\n" % file_name) try: instance = create_instance( username, xml_file, media_files, date_created_override=date_created) num_restored += 1 except Exception as e: sys.stderr.write( "Could not restore %s, create instance said: %s\n" % (file_name, e)) return num_instances, num_restored