moviewyrm/bookwyrm/models/link.py

55 lines
1.5 KiB
Python
Raw Normal View History

2021-10-04 20:14:32 +00:00
""" outlink data """
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")
2021-10-04 20:14:32 +00:00
2021-12-16 00:23:21 +00:00
activity_serializer = activitypub.Link
reverse_unfurl = True
2021-12-15 20:40:31 +00:00
def save(self, *args, **kwargs):
"""create a link"""
# 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
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
)
filetype = fields.CharField(max_length=5, activitypub_field="mediaType")
2022-01-09 21:19:29 +00:00
StatusChoices = [
("approved", _("Approved")),
("blocked", _("Blocked")),
("pending", _("Pending")),
]
class LinkDomain(BookWyrmModel):
"""List of domains used in links"""
domain = models.CharField(max_length=255)
status = models.CharField(max_length=50, choices=StatusChoices, default="pending")
name = models.CharField(max_length=100)
def save(self, *args, **kwargs):
"""set a default name"""
if not self.name:
self.name = self.domain
super().save(*args, **kwargs)