mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-12-04 23:36:32 +00:00
Re-structures link models
This commit is contained in:
parent
af8cb51325
commit
86b294afd7
7 changed files with 32 additions and 27 deletions
|
@ -26,6 +26,7 @@ class Link:
|
||||||
|
|
||||||
href: str
|
href: str
|
||||||
name: str
|
name: str
|
||||||
|
mediaType: str = None
|
||||||
type: str = "Link"
|
type: str = "Link"
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# Generated by Django 3.2.5 on 2021-12-15 20:38
|
# Generated by Django 3.2.5 on 2021-12-16 00:20
|
||||||
|
|
||||||
import bookwyrm.models.activitypub_mixin
|
import bookwyrm.models.activitypub_mixin
|
||||||
import bookwyrm.models.fields
|
import bookwyrm.models.fields
|
||||||
|
@ -58,27 +58,19 @@ class Migration(migrations.Migration):
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
("filetype", bookwyrm.models.fields.CharField(max_length=5)),
|
("filetype", bookwyrm.models.fields.CharField(max_length=5)),
|
||||||
|
(
|
||||||
|
"book",
|
||||||
|
bookwyrm.models.fields.ForeignKey(
|
||||||
|
null=True,
|
||||||
|
on_delete=django.db.models.deletion.CASCADE,
|
||||||
|
related_name="file_links",
|
||||||
|
to="bookwyrm.book",
|
||||||
|
),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
options={
|
options={
|
||||||
"abstract": False,
|
"abstract": False,
|
||||||
},
|
},
|
||||||
bases=("bookwyrm.link",),
|
bases=("bookwyrm.link",),
|
||||||
),
|
),
|
||||||
migrations.AddField(
|
|
||||||
model_name="author",
|
|
||||||
name="links",
|
|
||||||
field=bookwyrm.models.fields.ManyToManyField(to="bookwyrm.Link"),
|
|
||||||
),
|
|
||||||
migrations.AddField(
|
|
||||||
model_name="book",
|
|
||||||
name="links",
|
|
||||||
field=bookwyrm.models.fields.ManyToManyField(to="bookwyrm.Link"),
|
|
||||||
),
|
|
||||||
migrations.AddField(
|
|
||||||
model_name="book",
|
|
||||||
name="file_links",
|
|
||||||
field=bookwyrm.models.fields.ManyToManyField(
|
|
||||||
related_name="editions", to="bookwyrm.FileLink"
|
|
||||||
),
|
|
||||||
),
|
|
||||||
]
|
]
|
|
@ -44,7 +44,6 @@ class BookDataModel(ObjectMixin, BookWyrmModel):
|
||||||
bnf_id = fields.CharField( # Bibliothèque nationale de France
|
bnf_id = fields.CharField( # Bibliothèque nationale de France
|
||||||
max_length=255, blank=True, null=True, deduplication_field=True
|
max_length=255, blank=True, null=True, deduplication_field=True
|
||||||
)
|
)
|
||||||
links = fields.ManyToManyField("Link")
|
|
||||||
search_vector = SearchVectorField(null=True)
|
search_vector = SearchVectorField(null=True)
|
||||||
|
|
||||||
last_edited_by = fields.ForeignKey(
|
last_edited_by = fields.ForeignKey(
|
||||||
|
@ -116,7 +115,6 @@ class Book(BookDataModel):
|
||||||
|
|
||||||
objects = InheritanceManager()
|
objects = InheritanceManager()
|
||||||
field_tracker = FieldTracker(fields=["authors", "title", "subtitle", "cover"])
|
field_tracker = FieldTracker(fields=["authors", "title", "subtitle", "cover"])
|
||||||
file_links = fields.ManyToManyField("FileLink", related_name="editions")
|
|
||||||
|
|
||||||
if ENABLE_THUMBNAIL_GENERATION:
|
if ENABLE_THUMBNAIL_GENERATION:
|
||||||
cover_bw_book_xsmall_webp = ImageSpecField(
|
cover_bw_book_xsmall_webp = ImageSpecField(
|
||||||
|
@ -237,7 +235,7 @@ class Work(OrderedCollectionPageMixin, Book):
|
||||||
|
|
||||||
activity_serializer = activitypub.Work
|
activity_serializer = activitypub.Work
|
||||||
serialize_reverse_fields = [("editions", "editions", "-edition_rank")]
|
serialize_reverse_fields = [("editions", "editions", "-edition_rank")]
|
||||||
deserialize_reverse_fields = [("editions", "editions")]
|
deserialize_reverse_fields = [("editions", "editions"), ("file_links", "fileLinks")]
|
||||||
|
|
||||||
|
|
||||||
# https://schema.org/BookFormatType
|
# https://schema.org/BookFormatType
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
""" outlink data """
|
""" outlink data """
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
|
from bookwyrm import activitypub
|
||||||
from .activitypub_mixin import ActivitypubMixin
|
from .activitypub_mixin import ActivitypubMixin
|
||||||
from .base_model import BookWyrmModel
|
from .base_model import BookWyrmModel
|
||||||
from . import fields
|
from . import fields
|
||||||
|
@ -7,9 +10,12 @@ from . import fields
|
||||||
class Link(ActivitypubMixin, BookWyrmModel):
|
class Link(ActivitypubMixin, BookWyrmModel):
|
||||||
"""a link to a website"""
|
"""a link to a website"""
|
||||||
|
|
||||||
url = fields.URLField(max_length=255)
|
url = fields.URLField(max_length=255, activitypub_field="href")
|
||||||
name = fields.CharField(max_length=255)
|
name = fields.CharField(max_length=255)
|
||||||
|
|
||||||
|
activity_serializer = activitypub.Link
|
||||||
|
reverse_unfurl = True
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
"""create a link"""
|
"""create a link"""
|
||||||
# this is never broadcast, the owning model broadcasts an update
|
# this is never broadcast, the owning model broadcasts an update
|
||||||
|
@ -21,4 +27,7 @@ class Link(ActivitypubMixin, BookWyrmModel):
|
||||||
class FileLink(Link):
|
class FileLink(Link):
|
||||||
"""a link to a file"""
|
"""a link to a file"""
|
||||||
|
|
||||||
filetype = fields.CharField(max_length=5)
|
book = fields.ForeignKey(
|
||||||
|
"Book", on_delete=models.CASCADE, related_name="file_links", null=True
|
||||||
|
)
|
||||||
|
filetype = fields.CharField(max_length=5, activitypub_field="mediaType")
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
|
|
||||||
{% block modal-body %}
|
{% block modal-body %}
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
|
<input type="hidden" name="book" value="{{ book.id }}">
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<label class="label" for="id_url">{% trans "Name:" %}</label>
|
<label class="label" for="id_url">{% trans "Name:" %}</label>
|
||||||
{{ file_link_form.name }}
|
{{ file_link_form.name }}
|
||||||
|
|
|
@ -432,9 +432,9 @@ urlpatterns = [
|
||||||
views.add_description,
|
views.add_description,
|
||||||
name="add-description",
|
name="add-description",
|
||||||
),
|
),
|
||||||
re_path(rf"{BOOK_PATH}/file-link/?$", views.FileLink.as_view(), name="file-link"),
|
re_path(rf"{BOOK_PATH}/filelink/?$", views.FileLink.as_view(), name="file-link"),
|
||||||
re_path(
|
re_path(
|
||||||
rf"{BOOK_PATH}/file-link/(?P<link_id>\d+)/?$",
|
rf"{BOOK_PATH}/filelink/(?P<link_id>\d+)/?$",
|
||||||
views.FileLink.as_view(),
|
views.FileLink.as_view(),
|
||||||
name="file-link",
|
name="file-link",
|
||||||
),
|
),
|
||||||
|
|
|
@ -7,6 +7,7 @@ from django.views import View
|
||||||
from django.utils.decorators import method_decorator
|
from django.utils.decorators import method_decorator
|
||||||
|
|
||||||
from bookwyrm import forms, models
|
from bookwyrm import forms, models
|
||||||
|
from bookwyrm.activitypub import ActivitypubResponse
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=no-self-use
|
# pylint: disable=no-self-use
|
||||||
|
@ -17,10 +18,13 @@ from bookwyrm import forms, models
|
||||||
class FileLink(View):
|
class FileLink(View):
|
||||||
"""a book! this is the stuff"""
|
"""a book! this is the stuff"""
|
||||||
|
|
||||||
def get(self, request, book_id, link_id=None):
|
def get(self, request, book_id=None, link_id=None):
|
||||||
"""info about a book"""
|
"""info about a book"""
|
||||||
book = get_object_or_404(models.Edition, id=book_id)
|
|
||||||
link = get_object_or_404(models.FileLink, id=link_id) if link_id else None
|
link = get_object_or_404(models.FileLink, id=link_id) if link_id else None
|
||||||
|
if not book_id:
|
||||||
|
return ActivitypubResponse(link.to_activity())
|
||||||
|
|
||||||
|
book = get_object_or_404(models.Edition, id=book_id)
|
||||||
data = {
|
data = {
|
||||||
"file_link_form": forms.FileLinkForm(instance=link),
|
"file_link_form": forms.FileLinkForm(instance=link),
|
||||||
"book": book,
|
"book": book,
|
||||||
|
|
Loading…
Reference in a new issue