mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2025-01-11 09:45:27 +00:00
minor pylint and mypy fixes
This commit is contained in:
parent
e29c93a1e9
commit
f30555be0f
4 changed files with 40 additions and 32 deletions
|
@ -1,4 +1,9 @@
|
||||||
"""Import data from Bookwyrm export files"""
|
"""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
|
from bookwyrm.models.bookwyrm_import_job import BookwyrmImportJob
|
||||||
|
|
||||||
|
|
||||||
|
@ -8,8 +13,8 @@ class BookwyrmImporter:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def process_import(
|
def process_import(
|
||||||
self, user, archive_file, settings
|
self, user: User, archive_file: bytes, settings: QueryDict
|
||||||
): # pylint: disable=no-self-use
|
) -> BookwyrmImportJob: # pylint: disable=no-self-use
|
||||||
"""import user data from a Bookwyrm export file"""
|
"""import user data from a Bookwyrm export file"""
|
||||||
|
|
||||||
required = [k for k in settings if settings.get(k) == "on"]
|
required = [k for k in settings if settings.get(k) == "on"]
|
||||||
|
|
|
@ -60,7 +60,7 @@ def tar_export(json_data: str, user, file):
|
||||||
if getattr(user, "avatar", False):
|
if getattr(user, "avatar", False):
|
||||||
tar.add_image(user.avatar, filename="avatar")
|
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:
|
for book in editions:
|
||||||
if getattr(book, "cover", False):
|
if getattr(book, "cover", False):
|
||||||
tar.add_image(book.cover)
|
tar.add_image(book.cover)
|
||||||
|
|
|
@ -13,7 +13,7 @@ from bookwyrm.utils.tar import BookwyrmTarFile
|
||||||
from bookwyrm.models import bookwyrm_import_job
|
from bookwyrm.models import bookwyrm_import_job
|
||||||
|
|
||||||
|
|
||||||
class BookwyrmImport(TestCase):
|
class BookwyrmImport(TestCase): # pylint: disable=too-many-public-methods
|
||||||
"""testing user import functions"""
|
"""testing user import functions"""
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
@ -66,8 +66,10 @@ class BookwyrmImport(TestCase):
|
||||||
"../data/bookwyrm_account_export.tar.gz"
|
"../data/bookwyrm_account_export.tar.gz"
|
||||||
)
|
)
|
||||||
with open(self.archive_file, "rb") as fileobj:
|
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:
|
||||||
self.import_data = json.loads(tarfile.read("archive.json").decode("utf-8"))
|
self.import_data = json.loads(
|
||||||
|
tarfile.read("archive.json").decode("utf-8")
|
||||||
|
)
|
||||||
|
|
||||||
def test_update_user_profile(self):
|
def test_update_user_profile(self):
|
||||||
"""Test update the user's profile from import data"""
|
"""Test update the user's profile from import data"""
|
||||||
|
@ -77,21 +79,21 @@ class BookwyrmImport(TestCase):
|
||||||
):
|
):
|
||||||
|
|
||||||
with open(self.archive_file, "rb") as fileobj:
|
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(
|
models.bookwyrm_import_job.update_user_profile(
|
||||||
self.local_user, tarfile, self.import_data.get("user")
|
self.local_user, tarfile, self.import_data.get("user")
|
||||||
)
|
)
|
||||||
self.local_user.refresh_from_db()
|
self.local_user.refresh_from_db()
|
||||||
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
self.local_user.username, "mouse"
|
self.local_user.username, "mouse"
|
||||||
) # username should not change
|
) # username should not change
|
||||||
self.assertEqual(self.local_user.name, "Rat")
|
self.assertEqual(self.local_user.name, "Rat")
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
self.local_user.summary,
|
self.local_user.summary,
|
||||||
"I love to make soup in Paris and eat pizza in New York",
|
"I love to make soup in Paris and eat pizza in New York",
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_update_user_settings(self):
|
def test_update_user_settings(self):
|
||||||
"""Test updating the user's settings from import data"""
|
"""Test updating the user's settings from import data"""
|
||||||
|
@ -253,13 +255,13 @@ class BookwyrmImport(TestCase):
|
||||||
self.assertEqual(models.Edition.objects.count(), 1)
|
self.assertEqual(models.Edition.objects.count(), 1)
|
||||||
|
|
||||||
with open(self.archive_file, "rb") as fileobj:
|
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(
|
bookwyrm_import_job.get_or_create_edition(
|
||||||
self.import_data["books"][1], tarfile
|
self.import_data["books"][1], tarfile
|
||||||
) # Sand Talk
|
) # 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):
|
def test_get_or_create_edition_not_existing(self):
|
||||||
"""Test take a JSON string of books and editions,
|
"""Test take a JSON string of books and editions,
|
||||||
|
@ -269,15 +271,15 @@ class BookwyrmImport(TestCase):
|
||||||
self.assertEqual(models.Edition.objects.count(), 1)
|
self.assertEqual(models.Edition.objects.count(), 1)
|
||||||
|
|
||||||
with open(self.archive_file, "rb") as fileobj:
|
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(
|
bookwyrm_import_job.get_or_create_edition(
|
||||||
self.import_data["books"][0], tarfile
|
self.import_data["books"][0], tarfile
|
||||||
) # Seeing like a state
|
) # Seeing like a state
|
||||||
|
|
||||||
self.assertTrue(
|
self.assertTrue(
|
||||||
models.Edition.objects.filter(isbn_13="9780300070163").exists()
|
models.Edition.objects.filter(isbn_13="9780300070163").exists()
|
||||||
)
|
)
|
||||||
self.assertEqual(models.Edition.objects.count(), 2)
|
self.assertEqual(models.Edition.objects.count(), 2)
|
||||||
|
|
||||||
def test_clean_values(self):
|
def test_clean_values(self):
|
||||||
"""test clean values we don't want when creating new instances"""
|
"""test clean values we don't want when creating new instances"""
|
||||||
|
|
|
@ -39,6 +39,7 @@ class BookwyrmTarFile(tarfile.TarFile):
|
||||||
"""read data from the tar"""
|
"""read data from the tar"""
|
||||||
if reader := self.extractfile(filename):
|
if reader := self.extractfile(filename):
|
||||||
return reader.read()
|
return reader.read()
|
||||||
|
return None
|
||||||
|
|
||||||
def write_image_to_file(self, filename: str, file_field: Any) -> None:
|
def write_image_to_file(self, filename: str, file_field: Any) -> None:
|
||||||
"""add an image to the tar"""
|
"""add an image to the tar"""
|
||||||
|
|
Loading…
Reference in a new issue