Merge pull request #3061 from hughrun/user-migrate

minor pylint and mypy fixes
This commit is contained in:
Hugh Rundle 2023-10-23 21:32:36 +11:00 committed by GitHub
commit 5592a8e08b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 32 deletions

View file

@ -1,4 +1,9 @@
"""Import data from Bookwyrm export files"""
from typing import Any
from django.http import QueryDict
from bookwyrm.models import User
from bookwyrm.models.bookwyrm_import_job import BookwyrmImportJob
@ -8,8 +13,8 @@ class BookwyrmImporter:
"""
def process_import(
self, user, archive_file, settings
): # pylint: disable=no-self-use
self, user: User, archive_file: bytes, settings: QueryDict
) -> BookwyrmImportJob: # pylint: disable=no-self-use
"""import user data from a Bookwyrm export file"""
required = [k for k in settings if settings.get(k) == "on"]

View file

@ -60,7 +60,7 @@ def tar_export(json_data: str, user, file):
if getattr(user, "avatar", False):
tar.add_image(user.avatar, filename="avatar")
editions, books = get_books_for_user(user) # pylint: disable=unused-argument
editions, books = get_books_for_user(user) # pylint: disable=unused-variable
for book in editions:
if getattr(book, "cover", False):
tar.add_image(book.cover)

View file

@ -13,7 +13,7 @@ from bookwyrm.utils.tar import BookwyrmTarFile
from bookwyrm.models import bookwyrm_import_job
class BookwyrmImport(TestCase):
class BookwyrmImport(TestCase): # pylint: disable=too-many-public-methods
"""testing user import functions"""
def setUp(self):
@ -66,8 +66,10 @@ class BookwyrmImport(TestCase):
"../data/bookwyrm_account_export.tar.gz"
)
with open(self.archive_file, "rb") as fileobj:
tarfile = BookwyrmTarFile.open(mode="r:gz", fileobj=fileobj)
self.import_data = json.loads(tarfile.read("archive.json").decode("utf-8"))
with BookwyrmTarFile.open(mode="r:gz", fileobj=fileobj) as tarfile:
self.import_data = json.loads(
tarfile.read("archive.json").decode("utf-8")
)
def test_update_user_profile(self):
"""Test update the user's profile from import data"""
@ -77,21 +79,21 @@ class BookwyrmImport(TestCase):
):
with open(self.archive_file, "rb") as fileobj:
tarfile = BookwyrmTarFile.open(mode="r:gz", fileobj=fileobj)
with BookwyrmTarFile.open(mode="r:gz", fileobj=fileobj) as tarfile:
models.bookwyrm_import_job.update_user_profile(
self.local_user, tarfile, self.import_data.get("user")
)
self.local_user.refresh_from_db()
models.bookwyrm_import_job.update_user_profile(
self.local_user, tarfile, self.import_data.get("user")
)
self.local_user.refresh_from_db()
self.assertEqual(
self.local_user.username, "mouse"
) # username should not change
self.assertEqual(self.local_user.name, "Rat")
self.assertEqual(
self.local_user.summary,
"I love to make soup in Paris and eat pizza in New York",
)
self.assertEqual(
self.local_user.username, "mouse"
) # username should not change
self.assertEqual(self.local_user.name, "Rat")
self.assertEqual(
self.local_user.summary,
"I love to make soup in Paris and eat pizza in New York",
)
def test_update_user_settings(self):
"""Test updating the user's settings from import data"""
@ -253,13 +255,13 @@ class BookwyrmImport(TestCase):
self.assertEqual(models.Edition.objects.count(), 1)
with open(self.archive_file, "rb") as fileobj:
tarfile = BookwyrmTarFile.open(mode="r:gz", fileobj=fileobj)
with BookwyrmTarFile.open(mode="r:gz", fileobj=fileobj) as tarfile:
bookwyrm_import_job.get_or_create_edition(
self.import_data["books"][1], tarfile
) # Sand Talk
bookwyrm_import_job.get_or_create_edition(
self.import_data["books"][1], tarfile
) # Sand Talk
self.assertEqual(models.Edition.objects.count(), 1)
self.assertEqual(models.Edition.objects.count(), 1)
def test_get_or_create_edition_not_existing(self):
"""Test take a JSON string of books and editions,
@ -269,15 +271,15 @@ class BookwyrmImport(TestCase):
self.assertEqual(models.Edition.objects.count(), 1)
with open(self.archive_file, "rb") as fileobj:
tarfile = BookwyrmTarFile.open(mode="r:gz", fileobj=fileobj)
bookwyrm_import_job.get_or_create_edition(
self.import_data["books"][0], tarfile
) # Seeing like a state
with BookwyrmTarFile.open(mode="r:gz", fileobj=fileobj) as tarfile:
bookwyrm_import_job.get_or_create_edition(
self.import_data["books"][0], tarfile
) # Seeing like a state
self.assertTrue(
models.Edition.objects.filter(isbn_13="9780300070163").exists()
)
self.assertEqual(models.Edition.objects.count(), 2)
self.assertTrue(
models.Edition.objects.filter(isbn_13="9780300070163").exists()
)
self.assertEqual(models.Edition.objects.count(), 2)
def test_clean_values(self):
"""test clean values we don't want when creating new instances"""

View file

@ -39,6 +39,7 @@ class BookwyrmTarFile(tarfile.TarFile):
"""read data from the tar"""
if reader := self.extractfile(filename):
return reader.read()
return None
def write_image_to_file(self, filename: str, file_field: Any) -> None:
"""add an image to the tar"""