Skip to content

Commit

Permalink
update model to link editions
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name authored and alphatownsman committed Apr 9, 2024
1 parent 9481bd4 commit fec56d1
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 41 deletions.
2 changes: 1 addition & 1 deletion boofilsic/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@
SECRET_KEY = env("NEODB_SECRET_KEY")
DEBUG = env("NEODB_DEBUG")
DATABASES = {
"default": env.db_url("NEODB_DB_URL"),
"takahe": env.db_url("TAKAHE_DB_URL"),
"default": env.db_url("NEODB_DB_URL"),
}
DATABASES["default"]["OPTIONS"] = {"client_encoding": "UTF8"}
DATABASES["default"]["TEST"] = {"DEPENDENCIES": ["takahe"]}
Expand Down
27 changes: 27 additions & 0 deletions catalog/book/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"""

from os.path import exists

from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -186,6 +188,31 @@ def get_related_books(self):
.order_by("title")
)

def has_related_books(self):
works = list(self.works.all())
if not works:
return False
return Edition.objects.filter(works__in=works).exclude(pk=self.pk).exists()

def link_to_related_book(self, target: "Edition") -> bool:
if target == self or target.is_deleted or target.merged_to_item:
return False
if target.works.all().exists():
for work in target.works.all():
self.works.add(work)
elif self.works.all().exists():
for work in self.works.all():
target.works.add(work)
else:
Work.objects.create(title=self.title).editions.add(self, target)
return True

def unlink_from_all_works(self):
self.works.clear()

def has_works(self):
return self.works.all().exists()


class Work(Item):
category = ItemCategory.Book
Expand Down
74 changes: 64 additions & 10 deletions catalog/book/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@


class BookTestCase(TestCase):
databases = "__all__"

def setUp(self):
hyperion = Edition.objects.create(title="Hyperion")
hyperion.pages = 500
Expand Down Expand Up @@ -55,20 +57,64 @@ def test_isbn(self):
self.assertEqual(hyperion.isbn, "9780575099432")
self.assertEqual(hyperion.isbn10, "0575099437")

def test_work(self):
hyperion_print = Edition.objects.get(title="Hyperion")
hyperion_ebook = Edition(title="Hyperion")
hyperion_ebook.save()
hyperion_ebook.asin = "B0043M6780"
hyperion = Work(title="Hyperion")
hyperion.save()
hyperion.editions.add(hyperion_print)
hyperion.editions.add(hyperion_ebook)
# andymion = Edition(title="Andymion", pages=42)

class WorkTestCase(TestCase):
databases = "__all__"

def setUp(self):
self.hyperion_hardcover = Edition.objects.create(title="Hyperion")
self.hyperion_hardcover.pages = 481
self.hyperion_hardcover.isbn = "9780385249492"
self.hyperion_hardcover.save()
self.hyperion_print = Edition.objects.create(title="Hyperion")
self.hyperion_print.pages = 500
self.hyperion_print.isbn = "9780553283686"
self.hyperion_print.save()
self.hyperion_ebook = Edition(title="Hyperion")
self.hyperion_ebook.asin = "B0043M6780"
self.hyperion_ebook.save()
self.andymion_print = Edition.objects.create(title="Andymion", pages=42)
# serie = Serie(title="Hyperion Cantos")
self.hyperion = Work(title="Hyperion")
self.hyperion.save()

def test_work(self):
self.assertFalse(self.hyperion_print.has_related_books())
self.hyperion.editions.add(self.hyperion_print)
self.assertFalse(self.hyperion_print.has_related_books())

def test_link(self):
self.hyperion_print.link_to_related_book(self.hyperion_ebook)
self.assertTrue(self.hyperion_print.has_related_books())
self.assertTrue(self.hyperion_ebook.has_related_books())
self.assertTrue(self.hyperion_print.has_works())
self.assertEqual(
self.hyperion_print.works.first().title, self.hyperion_print.title
)
self.hyperion_print.unlink_from_all_works()
self.assertFalse(self.hyperion_print.has_related_books())
self.assertFalse(self.hyperion_ebook.has_related_books())
self.hyperion_print.link_to_related_book(self.hyperion_ebook)
self.assertTrue(self.hyperion_print.has_related_books())
self.assertTrue(self.hyperion_ebook.has_related_books())
self.hyperion_ebook.unlink_from_all_works()
self.assertFalse(self.hyperion_print.has_related_books())
self.assertFalse(self.hyperion_ebook.has_related_books())

def test_link3(self):
self.hyperion_print.link_to_related_book(self.hyperion_ebook)
self.hyperion_ebook.link_to_related_book(self.hyperion_hardcover)
self.hyperion_print.link_to_related_book(self.hyperion_hardcover)
self.assertTrue(self.hyperion_print.has_works())
self.assertEqual(self.hyperion_print.works.all().count(), 1)
self.assertEqual(
self.hyperion_ebook.works.all().first().editions.all().count(), 3
)


class GoodreadsTestCase(TestCase):
databases = "__all__"

def setUp(self):
pass

Expand Down Expand Up @@ -148,6 +194,8 @@ def test_work(self):


class GoogleBooksTestCase(TestCase):
databases = "__all__"

def test_parse(self):
t_type = IdType.GoogleBooks
t_id = "hV--zQEACAAJ"
Expand Down Expand Up @@ -179,6 +227,8 @@ def test_scrape(self):


class BooksTWTestCase(TestCase):
databases = "__all__"

def test_parse(self):
t_type = IdType.BooksTW
t_id = "0010947886"
Expand Down Expand Up @@ -227,6 +277,8 @@ def test_scrape(self):


class DoubanBookTestCase(TestCase):
databases = "__all__"

def setUp(self):
pass

Expand Down Expand Up @@ -289,6 +341,8 @@ def test_work(self):


class MultiBookSitesTestCase(TestCase):
databases = "__all__"

@use_local_response
def test_editions(self):
# isbn = '9781847498571'
Expand Down
30 changes: 0 additions & 30 deletions common/tests.py

This file was deleted.

0 comments on commit fec56d1

Please sign in to comment.