def handle(self, *args, **options): skip_ocr = options.get('skip_ocr') if options.get('location') is None: logger.error("Please add the path to your backup.tar") else: with open(options.get('location'), 'rb') as restore_file: if _can_restore(restore_file): if options.get('user') is None: username = input("Username:"******"Archive can be handled." "Please enter the user (username of) that should" " own the restored documents." ) if _is_valid_user(username): restore_documents( restore_file=restore_file, username=username, skip_ocr=skip_ocr ) else: logging.error("User %s was not valid", username) else: logging.error( "Archive cannot be restored because of" " version mismatch." )
def handle(self, *args, **options): username = options.get('user') user = None if username: try: user = User.objects.get(username=username) except User.DoesNotExist: logger.error( f"Username {username} not found." ) return if options.get('location') is None: logger.error("Please add the path to your backup.tar") else: with open(options.get('location'), 'rb') as restore_file: if _can_restore(restore_file): restore_documents( restore_file=restore_file, user=user, skip_ocr=True ) else: logging.error( "Archive cannot be restored because of" " version mismatch." )
def test_restore_backup(self): restore_path = os.path.join(BASE_DIR, "data", "testdata.tar") with open(restore_path, 'rb') as restore_archive: restore_documents(restore_archive, self.testcase_user) folder_1 = Folder.objects.filter(title='1', parent=None).first() self.assertIsNotNone(folder_1, 'Folder "1" was not restored') folder_2 = Folder.objects.filter(title='2', parent=None).first() self.assertIsNotNone(folder_2, 'Folder "2" was not restored') folder_3 = Folder.objects.filter(title='3', parent=folder_2).first() self.assertIsNotNone(folder_3, 'Folder "3" was not restored') document_berlin_1 = Document.objects.filter(title='berlin.pdf', parent=folder_1).first() self.assertIsNotNone( document_berlin_1, 'Document "berlin.pdf" in folder 1 was not restored') document_berlin_3 = Document.objects.filter(title='berlin.pdf', parent=folder_3).first() self.assertIsNotNone( document_berlin_3, 'Document "berlin.pdf" in folder 3 was not restored')
def test_restore_backup_documents_in_root(self): """ In case tar file contain documents in root (i.e. documents not part any folder) - restore_documents function should not throw an error (e.g. AttributeError: 'NoneType' object has no attribute 'id') """ restore_path = os.path.join(BASE_DIR, "data", "one-doc-in-root-testdata.tar") with open(restore_path, 'rb') as restore_archive: # should not throw an exception restore_documents(restore_archive, self.testcase_user)
def handle(self, *args, **options): if options.get('location') is None: logger.error("Please add the path to your backup.tar") else: with open(options.get('location'), 'rb') as restore_file: if _can_restore(restore_file): if options.get('user') is None: username = input("Username:"******"We can handle that archive. Please enter the user that should own the restored documents." ) if _is_valid_user(username): restore_documents(restore_file, username) else: logging.error("User %s was not valid", username) else: logging.error( "Looks like we can't understand your archive. Please make sure it is valid!" )