Updates models

This commit is contained in:
Mouse Reeve
2022-01-09 14:21:13 -08:00
parent bae01e1ea5
commit 63075a6fe9
5 changed files with 110 additions and 45 deletions

View File

@ -4,7 +4,7 @@ import sys
from .book import Book, Work, Edition, BookDataModel
from .author import Author
from .link import Link, FileLink
from .link import Link, FileLink, LinkDomain
from .connector import Connector
from .shelf import Shelf, ShelfBook

View File

@ -1,4 +1,6 @@
""" outlink data """
from urllib.parse import urlparse
from django.db import models
from django.utils.translation import gettext_lazy as _
@ -12,12 +14,25 @@ class Link(ActivitypubMixin, BookWyrmModel):
"""a link to a website"""
url = fields.URLField(max_length=255, activitypub_field="href")
domain = models.ForeignKey(
"LinkDomain", on_delete=models.PROTECT, null=True, blank=True
)
activity_serializer = activitypub.Link
reverse_unfurl = True
@property
def name(self):
"""link name via the assocaited domain"""
return self.domain.name
def save(self, *args, **kwargs):
"""create a link"""
# get or create the associated domain
if not self.domain:
domain = urlparse(self.url).netloc
self.domain, _ = LinkDomain.objects.get_or_create(domain=domain)
# this is never broadcast, the owning model broadcasts an update
if "broadcast" in kwargs:
del kwargs["broadcast"]
@ -43,7 +58,7 @@ StatusChoices = [
class LinkDomain(BookWyrmModel):
"""List of domains used in links"""
domain = models.CharField(max_length=255)
domain = models.CharField(max_length=255, unique=True)
status = models.CharField(max_length=50, choices=StatusChoices, default="pending")
name = models.CharField(max_length=100)