Skip to content

Commit

Permalink
Use boolean is_structured_data, not data_type
Browse files Browse the repository at this point in the history
for seeAlso (#958)
  • Loading branch information
blms committed Dec 5, 2023
1 parent e8ef16d commit 60f3862
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 21 deletions.
2 changes: 1 addition & 1 deletion apps/iiif/manifests/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class RelatedLinksInline(admin.TabularInline):
exclude = ("id",)
fields = (
"link",
"data_type",
"is_structured_data",
"format",
)
extra = 1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 3.2.12 on 2023-12-05 19:59

from django.db import migrations, models


def populate_is_structured_data(apps, schema_editor):
# Data migration to populate RelatedLink.is_structured_data for existing data
RelatedLink = apps.get_model("manifests", "RelatedLink")
rl_set = RelatedLink.objects.all()
for rl in rl_set:
# Assume all existing RelatedLinks are structured data, since they were previously only
# being added in Remote ingests when there was an existing manifest (which is structured
# data)
rl.is_structured_data = True
RelatedLink.objects.bulk_update(rl_set, ["is_structured_data"], batch_size=1000)


class Migration(migrations.Migration):
dependencies = [
("manifests", "0059_alter_relatedlink_id"),
]

operations = [
migrations.AddField(
model_name="relatedlink",
name="is_structured_data",
field=models.BooleanField(
default=False,
help_text="True if this link is structured data that should appear in the manifest's 'seeAlso' field; if false, the link will appear in the 'related' field instead. Leave unchecked if unsure.",
),
),
migrations.AlterField(
model_name="relatedlink",
name="data_type",
field=models.CharField(default="Dataset", max_length=255),
),
migrations.RunPython(populate_is_structured_data, migrations.RunPython.noop),
]
34 changes: 17 additions & 17 deletions apps/iiif/manifests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,12 @@ def external_links(self):
"see_also": [
link.link
for link in related_links
if link.data_type.lower() == "dataset"
if link.is_structured_data
],
"related": [
link.link
for link in related_links
if link.data_type.lower() != "dataset"
if not link.is_structured_data
],
}

Expand All @@ -304,18 +304,15 @@ def see_also_links(self):
:return: List of links to structured data describing Manifest
:rtype: list
"""
links = []
for link in self.relatedlink_set.all():
if link.data_type.lower() == "dataset":
links.append(
{
"@id": link.link,
"format": link.format,
}
if link.format
else link.link
)
return links
return [
{
"@id": link.link,
"format": link.format,
}
if link.format
else link.link
for link in self.relatedlink_set.filter(is_structured_data=True)
]

# TODO: Is this needed? It doesn't seem to be called anywhere.
# Could we just use the label as is?
Expand Down Expand Up @@ -398,9 +395,12 @@ class RelatedLink(models.Model):
data_type = models.CharField(
max_length=255,
default='Dataset',
help_text="Leave as 'Dataset' for structured data describing this document (e.g. a " +
"remote manifest) and this link will appear in 'seeAlso'; change to any other value and " +
"it will only appear in the 'related' property of the manifest.",
)
is_structured_data = models.BooleanField(
default=False,
help_text="True if this link is structured data that should appear in the manifest's " +
"'seeAlso' field; if false, the link will appear in the 'related' field instead. Leave " +
"unchecked if unsure.",
)
label = GenericRelation(ValueByLanguage)
format = models.CharField(max_length=255, choices=Choices.MIMETYPES, blank=True, null=True)
Expand Down
2 changes: 1 addition & 1 deletion apps/iiif/manifests/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def test_serialized_related_links(self):
)
assert not no_links['seeAlso']

link = RelatedLink(link='images.org', manifest=manifest)
link = RelatedLink(link='images.org', manifest=manifest, is_structured_data=True)
link.save()
manifest.refresh_from_db()

Expand Down
5 changes: 3 additions & 2 deletions apps/ingest/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def create_related_links(manifest, related_str):
manifest=manifest,
link=link,
format=format or "text/html", # assume web page if MIME type cannot be determined
data_type="Document", # assume this is not meant for seeAlso
is_structured_data=False, # assume this is not meant for seeAlso
)

def create_manifest(ingest):
Expand Down Expand Up @@ -108,7 +108,8 @@ def create_manifest(ingest):
RelatedLink(
manifest=manifest,
link=ingest.remote_url,
format='application/ld+json'
format='application/ld+json',
is_structured_data=True,
).save()

return manifest
Expand Down

0 comments on commit 60f3862

Please sign in to comment.