add other_title to works

This commit is contained in:
Your Name 2024-04-09 15:47:27 -04:00 committed by Henri Dickson
parent c796072f9a
commit bcf19e7a86
3 changed files with 34 additions and 58 deletions

View file

@ -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)

View file

@ -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())

View file

@ -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."))