Use urlfield

This commit is contained in:
Mouse Reeve 2021-12-15 12:40:31 -08:00
parent 1d6b200172
commit 4f576b77a0
7 changed files with 36 additions and 14 deletions

View file

@ -216,6 +216,12 @@ class CoverForm(CustomForm):
help_texts = {f: None for f in fields} help_texts = {f: None for f in fields}
class FileLinkForm(CustomForm):
class Meta:
model = models.FileLink
exclude = ["remote_id"]
class EditionForm(CustomForm): class EditionForm(CustomForm):
class Meta: class Meta:
model = models.Edition model = models.Edition

View file

@ -1,4 +1,4 @@
# Generated by Django 3.2.5 on 2021-12-15 18:18 # Generated by Django 3.2.5 on 2021-12-15 20:38
import bookwyrm.models.activitypub_mixin import bookwyrm.models.activitypub_mixin
import bookwyrm.models.fields import bookwyrm.models.fields
@ -35,13 +35,13 @@ class Migration(migrations.Migration):
validators=[bookwyrm.models.fields.validate_remote_id], validators=[bookwyrm.models.fields.validate_remote_id],
), ),
), ),
("url", bookwyrm.models.fields.CharField(max_length=255)), ("url", bookwyrm.models.fields.URLField(max_length=255)),
("name", bookwyrm.models.fields.CharField(max_length=255)), ("name", bookwyrm.models.fields.CharField(max_length=255)),
], ],
options={ options={
"abstract": False, "abstract": False,
}, },
bases=(bookwyrm.models.activitypub_mixin.CollectionItemMixin, models.Model), bases=(bookwyrm.models.activitypub_mixin.ActivitypubMixin, models.Model),
), ),
migrations.CreateModel( migrations.CreateModel(
name="FileLink", name="FileLink",
@ -58,10 +58,6 @@ class Migration(migrations.Migration):
), ),
), ),
("filetype", bookwyrm.models.fields.CharField(max_length=5)), ("filetype", bookwyrm.models.fields.CharField(max_length=5)),
(
"filetype_description",
bookwyrm.models.fields.CharField(max_length=100),
),
], ],
options={ options={
"abstract": False, "abstract": False,

View file

@ -516,6 +516,10 @@ class CharField(ActivitypubFieldMixin, models.CharField):
"""activitypub-aware char field""" """activitypub-aware char field"""
class URLField(ActivitypubFieldMixin, models.URLField):
"""activitypub-aware url field"""
class TextField(ActivitypubFieldMixin, models.TextField): class TextField(ActivitypubFieldMixin, models.TextField):
"""activitypub-aware text field""" """activitypub-aware text field"""

View file

@ -1,18 +1,24 @@
""" outlink data """ """ outlink data """
from .activitypub_mixin import CollectionItemMixin from .activitypub_mixin import ActivitypubMixin
from .base_model import BookWyrmModel from .base_model import BookWyrmModel
from . import fields from . import fields
class Link(CollectionItemMixin, BookWyrmModel): class Link(ActivitypubMixin, BookWyrmModel):
"""a link to a website""" """a link to a website"""
url = fields.CharField(max_length=255) url = fields.URLField(max_length=255)
name = fields.CharField(max_length=255) name = fields.CharField(max_length=255)
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)
class FileLink(Link): class FileLink(Link):
"""a link to a file""" """a link to a file"""
filetype = fields.CharField(max_length=5) filetype = fields.CharField(max_length=5)
filetype_description = fields.CharField(max_length=100)

View file

@ -351,7 +351,10 @@
{% if book.file_links %} {% if book.file_links %}
<ul> <ul>
{% for link in book.file_links.all %} {% for link in book.file_links.all %}
<li><a href="{{ link.url }}">{{ link.name }}</a> ({{ link.filetype }})</li> <li>
<a href="{{ link.url }}" rel="noopener" target="_blank" title="{{ link.url }}">{{ link.name }}</a>
({{ link.filetype }})
</li>
{% endfor %} {% endfor %}
</ul> </ul>
{% endif %} {% endif %}

View file

@ -19,7 +19,7 @@
<div class="columns"> <div class="columns">
<div class="column is-four-fifths"> <div class="column is-four-fifths">
<label class="label" for="id_url">{% trans "URL:" %}</label> <label class="label" for="id_url">{% trans "URL:" %}</label>
<input type="text" name="url" maxlength="255" class="input" required="" id="id_url" value="{% firstof file_link_form.url.value "" %}" placeholder="https://..."> <input type="url" name="url" maxlength="255" class="input" required="" id="id_url" value="{% firstof file_link_form.url.value "" %}" placeholder="https://...">
</div> </div>
<div class="column is-one-fifth"> <div class="column is-one-fifth">
<label class="label" for="id_filetype">{% trans "File type:" %}</label> <label class="label" for="id_filetype">{% trans "File type:" %}</label>

View file

@ -427,7 +427,14 @@ urlpatterns = [
re_path( re_path(
r"^upload-cover/(?P<book_id>\d+)/?$", views.upload_cover, name="upload-cover" r"^upload-cover/(?P<book_id>\d+)/?$", views.upload_cover, name="upload-cover"
), ),
re_path(r"^add-description/(?P<book_id>\d+)/?$", views.add_description), re_path(
r"^add-description/(?P<book_id>\d+)/?$",
views.add_description,
name="add-description",
),
re_path(
r"^add-file-link/(?P<book_id>\d+)/?$", views.add_file_link, name="add-file-link"
),
re_path(r"^resolve-book/?$", views.resolve_book, name="resolve-book"), re_path(r"^resolve-book/?$", views.resolve_book, name="resolve-book"),
re_path(r"^switch-edition/?$", views.switch_edition, name="switch-edition"), re_path(r"^switch-edition/?$", views.switch_edition, name="switch-edition"),
re_path( re_path(