From bcf19e7a86da1170ea73d621a540c42afe1e2cae Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 9 Apr 2024 15:47:27 -0400 Subject: [PATCH] add other_title to works --- catalog/book/models.py | 28 ++++++++- catalog/book/tests.py | 7 +++ .../management/commands/mastodon_sites.py | 57 ------------------- 3 files changed, 34 insertions(+), 58 deletions(-) delete mode 100644 mastodon/management/commands/mastodon_sites.py diff --git a/catalog/book/models.py b/catalog/book/models.py index 0bc573b2..cce871bb 100644 --- a/catalog/book/models.py +++ b/catalog/book/models.py @@ -241,6 +241,26 @@ class Work(Item): douban_work = PrimaryLookupIdDescriptor(IdType.DoubanBook_Work) goodreads_work = PrimaryLookupIdDescriptor(IdType.Goodreads_Work) editions = models.ManyToManyField(Edition, related_name="works") + author = jsondata.ArrayField( + verbose_name=_("author"), + base_field=models.CharField(max_length=500), + null=True, + blank=True, + default=list, + ) + other_title = jsondata.ArrayField( + verbose_name=_("other title"), + base_field=models.CharField(blank=True, default="", max_length=200), + null=True, + blank=True, + default=list, + ) + METADATA_COPY_LIST = [ + "title", + "other_title", + "author", + "brief", + ] # TODO: we have many duplicates due to 302 # a lazy fix is to remove smaller DoubanBook_Work ids # but ideally deal with 302 in scrape(). @@ -248,13 +268,19 @@ class Work(Item): @classmethod def lookup_id_type_choices(cls): id_types = [ + IdType.WikiData, IdType.DoubanBook_Work, IdType.Goodreads_Work, - IdType.WikiData, ] return [(i.value, i.label) for i in id_types] def merge_to(self, to_item): + if ( + to_item + and self.title != to_item.title + and self.title not in to_item.other_title + ): + to_item.other_title += [self.title] super().merge_to(to_item) for edition in self.editions.all(): to_item.editions.add(edition) diff --git a/catalog/book/tests.py b/catalog/book/tests.py index 70d63a4b..25bb9538 100644 --- a/catalog/book/tests.py +++ b/catalog/book/tests.py @@ -83,6 +83,13 @@ class WorkTestCase(TestCase): self.hyperion.editions.add(self.hyperion_print) self.assertFalse(self.hyperion_print.has_related_books()) + def test_merge(self): + w1 = Work.objects.create(title="title1") + w2 = Work.objects.create(title="title2") + w2.merge_to(w1) + self.assertEqual(w1.title, "title1") + self.assertEqual(w1.other_title, ["title2"]) + def test_link(self): self.hyperion_print.link_to_related_book(self.hyperion_ebook) self.assertTrue(self.hyperion_print.has_related_books()) diff --git a/mastodon/management/commands/mastodon_sites.py b/mastodon/management/commands/mastodon_sites.py deleted file mode 100644 index 44386e31..00000000 --- a/mastodon/management/commands/mastodon_sites.py +++ /dev/null @@ -1,57 +0,0 @@ -import pprint - -from django.core.management.base import BaseCommand - -from mastodon.api import create_app, detect_server_info -from mastodon.models import MastodonApplication - - -class Command(BaseCommand): - help = "Manage Mastodon sites" - - def add_arguments(self, parser): - # parser.add_argument("domain", type=str, help="mastodon domain") - parser.add_argument( - "--refresh", - action="store_true", - help="refresh app registration on all sites", - ) - - def handle(self, *args, **options): - if options["refresh"]: - for site in MastodonApplication.objects.exclude(disabled=True): - try: - _, _, server_version = detect_server_info(site.api_domain) - except Exception: - continue - allow_multiple_redir = True - if "; Pixelfed" in server_version or server_version.startswith("0."): - allow_multiple_redir = False - if allow_multiple_redir: - continue - try: - response = create_app(site.api_domain, allow_multiple_redir) - except Exception as e: - self.stdout.write(f"Error creating app on {site.api_domain}: {e}") - continue - if response.status_code != 200: - self.stdout.write( - f"Error creating app on {site.api_domain}: {response.status_code}" - ) - continue - try: - data = response.json() - except Exception: - self.stdout.write( - f"Error creating app on {site.api_domain}: unable to parse response" - ) - continue - site.app_id = data["id"] - site.client_id = data["client_id"] - site.client_secret = data["client_secret"] - site.vapid_key = data.get("vapid_key") - site.save( - update_fields=["app_id", "client_id", "client_secret", "vapid_key"] - ) - self.stdout.write(f"updated app on {site.api_domain}") - self.stdout.write(self.style.SUCCESS("Done."))