2021-03-08 16:49:10 +00:00
|
|
|
""" database schema for info about authors """
|
2024-02-22 09:27:38 +00:00
|
|
|
|
2021-12-05 20:28:17 +00:00
|
|
|
import re
|
2023-07-23 18:50:44 +00:00
|
|
|
from typing import Tuple, Any
|
|
|
|
|
2020-11-27 22:54:08 +00:00
|
|
|
from django.db import models
|
2024-03-20 15:14:26 +00:00
|
|
|
from django.contrib.postgres.indexes import GinIndex
|
2023-11-25 01:26:13 +00:00
|
|
|
import pgtrigger
|
2020-11-27 22:54:08 +00:00
|
|
|
|
|
|
|
from bookwyrm import activitypub
|
2020-11-28 21:40:09 +00:00
|
|
|
from bookwyrm.settings import DOMAIN
|
2023-11-25 01:26:13 +00:00
|
|
|
from bookwyrm.utils.db import format_trigger
|
2020-11-27 22:54:08 +00:00
|
|
|
|
2024-02-22 09:27:38 +00:00
|
|
|
from .book import BookDataModel, MergedAuthor
|
2020-11-30 22:40:26 +00:00
|
|
|
from . import fields
|
2020-11-27 22:54:08 +00:00
|
|
|
|
|
|
|
|
2020-12-21 20:17:18 +00:00
|
|
|
class Author(BookDataModel):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""basic biographic info"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2024-02-22 09:27:38 +00:00
|
|
|
merged_model = MergedAuthor
|
|
|
|
|
2020-12-13 02:06:48 +00:00
|
|
|
wikipedia_link = fields.CharField(
|
2021-03-08 16:49:10 +00:00
|
|
|
max_length=255, blank=True, null=True, deduplication_field=True
|
|
|
|
)
|
2021-04-07 00:46:06 +00:00
|
|
|
isni = fields.CharField(
|
2023-01-12 03:21:40 +00:00
|
|
|
max_length=255, blank=True, null=True, deduplication_field=True
|
2021-04-07 00:46:06 +00:00
|
|
|
)
|
|
|
|
gutenberg_id = fields.CharField(
|
|
|
|
max_length=255, blank=True, null=True, deduplication_field=True
|
|
|
|
)
|
2022-12-11 19:33:33 +00:00
|
|
|
isfdb = fields.CharField(
|
|
|
|
max_length=255, blank=True, null=True, deduplication_field=True
|
|
|
|
)
|
2023-01-15 08:41:39 +00:00
|
|
|
|
|
|
|
website = fields.CharField(
|
|
|
|
max_length=255, blank=True, null=True, deduplication_field=True
|
|
|
|
)
|
2020-11-27 22:54:08 +00:00
|
|
|
# idk probably other keys would be useful here?
|
2020-11-30 22:40:26 +00:00
|
|
|
born = fields.DateTimeField(blank=True, null=True)
|
|
|
|
died = fields.DateTimeField(blank=True, null=True)
|
2021-11-21 21:47:12 +00:00
|
|
|
name = fields.CharField(max_length=255)
|
2020-11-30 22:40:26 +00:00
|
|
|
aliases = fields.ArrayField(
|
2020-11-27 22:54:08 +00:00
|
|
|
models.CharField(max_length=255), blank=True, default=list
|
|
|
|
)
|
2020-12-17 00:47:05 +00:00
|
|
|
bio = fields.HtmlField(null=True, blank=True)
|
2020-11-27 22:54:08 +00:00
|
|
|
|
2023-07-23 18:50:44 +00:00
|
|
|
def save(self, *args: Tuple[Any, ...], **kwargs: dict[str, Any]) -> None:
|
2022-12-20 20:48:14 +00:00
|
|
|
"""normalize isni format"""
|
2022-10-09 23:36:24 +00:00
|
|
|
if self.isni:
|
|
|
|
self.isni = re.sub(r"\s", "", self.isni)
|
|
|
|
|
2022-01-05 22:33:10 +00:00
|
|
|
return super().save(*args, **kwargs)
|
|
|
|
|
2021-12-05 20:28:17 +00:00
|
|
|
@property
|
|
|
|
def isni_link(self):
|
|
|
|
"""generate the url from the isni id"""
|
|
|
|
clean_isni = re.sub(r"\s", "", self.isni)
|
2021-12-05 21:24:40 +00:00
|
|
|
return f"https://isni.org/isni/{clean_isni}"
|
2021-12-05 20:28:17 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def openlibrary_link(self):
|
|
|
|
"""generate the url from the openlibrary id"""
|
|
|
|
return f"https://openlibrary.org/authors/{self.openlibrary_key}"
|
|
|
|
|
2022-12-11 19:33:33 +00:00
|
|
|
@property
|
|
|
|
def isfdb_link(self):
|
|
|
|
"""generate the url from the isni id"""
|
|
|
|
return f"https://www.isfdb.org/cgi-bin/ea.cgi?{self.isfdb}"
|
|
|
|
|
2020-11-28 21:40:09 +00:00
|
|
|
def get_remote_id(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""editions and works both use "book" instead of model_name"""
|
2021-09-18 18:32:00 +00:00
|
|
|
return f"https://{DOMAIN}/author/{self.id}"
|
2020-11-28 21:40:09 +00:00
|
|
|
|
2023-11-25 01:26:13 +00:00
|
|
|
class Meta:
|
|
|
|
"""sets up indexes and triggers"""
|
|
|
|
|
2024-03-20 15:14:26 +00:00
|
|
|
# pylint: disable=line-too-long
|
|
|
|
|
|
|
|
indexes = (GinIndex(fields=["search_vector"]),)
|
2023-11-25 01:26:13 +00:00
|
|
|
triggers = [
|
2024-03-20 15:14:26 +00:00
|
|
|
pgtrigger.Trigger(
|
|
|
|
name="update_search_vector_on_author_edit",
|
|
|
|
when=pgtrigger.Before,
|
|
|
|
operation=pgtrigger.Insert
|
|
|
|
| pgtrigger.UpdateOf("name", "aliases", "search_vector"),
|
|
|
|
func=format_trigger(
|
|
|
|
"""new.search_vector :=
|
|
|
|
-- author name, with priority A
|
|
|
|
setweight(to_tsvector('simple', new.name), 'A') ||
|
|
|
|
-- author aliases, with priority B
|
|
|
|
setweight(to_tsvector('simple', coalesce(array_to_string(new.aliases, ' '), '')), 'B');
|
|
|
|
RETURN new;
|
|
|
|
"""
|
|
|
|
),
|
|
|
|
),
|
2023-11-25 01:26:13 +00:00
|
|
|
pgtrigger.Trigger(
|
2024-03-20 15:18:55 +00:00
|
|
|
name="reset_book_search_vector_on_author_edit",
|
2023-11-25 01:26:13 +00:00
|
|
|
when=pgtrigger.After,
|
2024-03-20 15:18:55 +00:00
|
|
|
operation=pgtrigger.UpdateOf("name", "aliases"),
|
2023-11-25 01:26:13 +00:00
|
|
|
func=format_trigger(
|
2023-11-25 21:13:05 +00:00
|
|
|
"""WITH updated_books AS (
|
|
|
|
SELECT book_id
|
|
|
|
FROM bookwyrm_book_authors
|
|
|
|
WHERE author_id = new.id
|
2023-11-25 01:26:13 +00:00
|
|
|
)
|
|
|
|
UPDATE bookwyrm_book
|
|
|
|
SET search_vector = ''
|
2023-11-25 21:13:05 +00:00
|
|
|
FROM updated_books
|
|
|
|
WHERE id = updated_books.book_id;
|
2023-11-25 01:26:13 +00:00
|
|
|
RETURN new;
|
|
|
|
"""
|
|
|
|
),
|
2024-03-20 15:14:26 +00:00
|
|
|
),
|
2023-11-25 01:26:13 +00:00
|
|
|
]
|
|
|
|
|
2020-11-27 22:54:08 +00:00
|
|
|
activity_serializer = activitypub.Author
|