moviewyrm/bookwyrm/models/link.py

96 lines
2.6 KiB
Python
Raw Normal View History

2021-10-04 20:14:32 +00:00
""" outlink data """
2022-01-09 22:21:13 +00:00
from urllib.parse import urlparse
2022-01-10 19:19:20 +00:00
from django.core.exceptions import PermissionDenied
2021-12-16 00:23:21 +00:00
from django.db import models
2022-01-09 21:19:29 +00:00
from django.utils.translation import gettext_lazy as _
2021-12-16 00:23:21 +00:00
from bookwyrm import activitypub
2021-12-15 20:40:31 +00:00
from .activitypub_mixin import ActivitypubMixin
2021-10-04 20:14:32 +00:00
from .base_model import BookWyrmModel
from . import fields
2021-10-07 23:53:39 +00:00
2021-12-15 20:40:31 +00:00
class Link(ActivitypubMixin, BookWyrmModel):
2021-10-04 20:14:32 +00:00
"""a link to a website"""
2021-12-16 00:23:21 +00:00
url = fields.URLField(max_length=255, activitypub_field="href")
2022-01-10 21:27:01 +00:00
added_by = fields.ForeignKey(
2022-01-10 22:55:10 +00:00
"User", on_delete=models.SET_NULL, null=True, activitypub_field="attributedTo"
2022-01-10 21:27:01 +00:00
)
2022-01-09 22:21:13 +00:00
domain = models.ForeignKey(
2022-01-10 19:21:03 +00:00
"LinkDomain",
on_delete=models.CASCADE,
null=True,
blank=True,
related_name="links",
2022-01-09 22:21:13 +00:00
)
2021-10-04 20:14:32 +00:00
2021-12-16 00:23:21 +00:00
activity_serializer = activitypub.Link
reverse_unfurl = True
2022-01-09 22:21:13 +00:00
@property
def name(self):
"""link name via the assocaited domain"""
return self.domain.name
2021-12-15 20:40:31 +00:00
def save(self, *args, **kwargs):
"""create a link"""
2022-01-09 22:21:13 +00:00
# get or create the associated domain
if not self.domain:
domain = urlparse(self.url).netloc
self.domain, _ = LinkDomain.objects.get_or_create(domain=domain)
2021-12-15 20:40:31 +00:00
# this is never broadcast, the owning model broadcasts an update
if "broadcast" in kwargs:
del kwargs["broadcast"]
return super().save(*args, **kwargs)
2021-10-04 20:14:32 +00:00
2022-01-17 17:21:58 +00:00
AvailabilityChoices = [
("free", _("Free")),
("purchase", _("Purchasable")),
("loan", _("Available for loan")),
]
2021-10-04 20:14:32 +00:00
class FileLink(Link):
"""a link to a file"""
2021-12-16 01:10:59 +00:00
book = models.ForeignKey(
2021-12-16 00:23:21 +00:00
"Book", on_delete=models.CASCADE, related_name="file_links", null=True
)
2022-01-17 17:03:53 +00:00
filetype = fields.CharField(max_length=50, activitypub_field="mediaType")
2022-01-17 17:21:58 +00:00
availability = fields.CharField(
max_length=100, choices=AvailabilityChoices, default="free"
)
2022-01-09 21:19:29 +00:00
StatusChoices = [
("approved", _("Approved")),
("blocked", _("Blocked")),
("pending", _("Pending")),
]
class LinkDomain(BookWyrmModel):
"""List of domains used in links"""
2022-01-09 22:21:13 +00:00
domain = models.CharField(max_length=255, unique=True)
2022-01-09 21:19:29 +00:00
status = models.CharField(max_length=50, choices=StatusChoices, default="pending")
name = models.CharField(max_length=100)
reported_by = models.ForeignKey(
"User", blank=True, null=True, on_delete=models.SET_NULL
)
2022-01-09 21:19:29 +00:00
2022-01-10 19:19:20 +00:00
def raise_not_editable(self, viewer):
if viewer.has_perm("moderate_post"):
return
raise PermissionDenied()
2022-01-09 21:19:29 +00:00
def save(self, *args, **kwargs):
"""set a default name"""
if not self.name:
self.name = self.domain
super().save(*args, **kwargs)