Skip to content

Commit

Permalink
Add a boolean field (searchable) to the manifest model so we can supp…
Browse files Browse the repository at this point in the history
…ress/remove some from the index.
  • Loading branch information
jayvarner committed Jan 13, 2025
1 parent ec0db98 commit 1aed0ec
Show file tree
Hide file tree
Showing 5 changed files with 330 additions and 149 deletions.
111 changes: 79 additions & 32 deletions apps/iiif/manifests/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Django admin module for manifests"""

from django.contrib import admin
from django.http import HttpResponseRedirect
from django.http.request import HttpRequest
Expand All @@ -14,25 +15,43 @@
from .views import AddToCollectionsView, MetadataImportView
from ..kollections.models import Collection


class ManifestResource(resources.ModelResource):
"""Django admin manifest resource."""

collection_id = fields.Field(
column_name='collections',
attribute='collections',
widget=ManyToManyWidget(Collection, field='label')
column_name="collections",
attribute="collections",
widget=ManyToManyWidget(Collection, field="label"),
)
image_server_link = fields.Field(
column_name='image_server',
attribute='image_server',
widget=ForeignKeyWidget(ImageServer, 'image_server')
column_name="image_server",
attribute="image_server",
widget=ForeignKeyWidget(ImageServer, "image_server"),
)
class Meta: # pylint: disable=too-few-public-methods, missing-class-docstring

class Meta: # pylint: disable=too-few-public-methods, missing-class-docstring
model = Manifest
import_id_fields = ('id',)
import_id_fields = ("id",)
fields = (
'id', 'pid', 'label', 'summary', 'author',
'published_city', 'published_date', 'publisher', 'image_server_link'
'pdf', 'metadata', 'attribution', 'logo', 'logo_url', 'license', 'viewingdirection', 'collection_id'
"id",
"pid",
"label",
"summary",
"author",
"searchable",
"published_city",
"published_date",
"publisher",
"image_server_link",
"pdf",
"metadata",
"attribution",
"logo",
"logo_url",
"license",
"viewingdirection",
"collection_id",
)


Expand All @@ -47,6 +66,7 @@ class RelatedLinksInline(admin.TabularInline):
extra = 1
min_num = 0


class SummernoteMixin(SummernoteModelAdmin):
class Media:
# NOTE: have to include these js and css dependencies for summernote when not using iframe
Expand All @@ -55,62 +75,89 @@ class Media:
"//cdn.jsdelivr.net/npm/[email protected]/jquery.ui.widget.min.js",
)
css = {
"all": ["//cdn.jsdelivr.net/npm/[email protected]/dist/summernote-lite.min.css"],
"all": [
"//cdn.jsdelivr.net/npm/[email protected]/dist/summernote-lite.min.css"
],
}


class ManifestAdmin(ImportExportModelAdmin, SummernoteMixin, admin.ModelAdmin):
"""Django admin configuration for manifests"""

resource_class = ManifestResource
exclude = ('id',)
filter_horizontal = ('collections',)
list_display = ('id', 'pid', 'label', 'created_at', 'author', 'published_date', 'published_city', 'publisher')
search_fields = ('id', 'pid', 'label', 'author', 'published_date')
summernote_fields = ('summary',)
exclude = ("id",)
filter_horizontal = ("collections",)
list_display = (
"id",
"pid",
"label",
"created_at",
"author",
"published_date",
"published_city",
"publisher",
)
search_fields = ("id", "pid", "label", "author", "published_date")
summernote_fields = ("summary",)
form = ManifestAdminForm
actions = ['add_to_collections_action']
actions = ["add_to_collections_action"]
inlines = [RelatedLinksInline]
change_list_template = 'admin/change_list_override.html'
change_list_template = "admin/change_list_override.html"

def add_to_collections_action(self, request, queryset):
"""Action choose manifests to add to collections"""
selected = queryset.values_list('pk', flat=True)
selected_ids = ','.join(str(pk) for pk in selected)
return HttpResponseRedirect(f'add_to_collections/?ids={selected_ids}')
add_to_collections_action.short_description = 'Add selected manifests to collection(s)'
selected = queryset.values_list("pk", flat=True)
selected_ids = ",".join(str(pk) for pk in selected)
return HttpResponseRedirect(f"add_to_collections/?ids={selected_ids}")

add_to_collections_action.short_description = (
"Add selected manifests to collection(s)"
)

def get_urls(self):
urls = super().get_urls()
my_urls = [
path(
'add_to_collections/',
"add_to_collections/",
self.admin_site.admin_view(AddToCollectionsView.as_view()),
{'model_admin': self, },
{
"model_admin": self,
},
name="AddManifestsToCollections",
),
path(
'manifest_metadata_import/',
"manifest_metadata_import/",
self.admin_site.admin_view(MetadataImportView.as_view()),
{'model_admin': self, },
{
"model_admin": self,
},
name="MultiManifestMetadataImport",
)
),
]
return my_urls + urls


class NoteAdmin(admin.ModelAdmin):
"""Django admin configuration for a note."""
class Meta: # pylint: disable=too-few-public-methods, missing-class-docstring

class Meta: # pylint: disable=too-few-public-methods, missing-class-docstring
model = Note


class ImageServerResource(resources.ModelResource):
"""Django admin ImageServer resource."""
class Meta: # pylint: disable=too-few-public-methods, missing-class-docstring

class Meta: # pylint: disable=too-few-public-methods, missing-class-docstring
model = ImageServer
fields = ('id', 'server_base', 'storage_service', 'storage_path')
fields = ("id", "server_base", "storage_service", "storage_path")


class ImageServerAdmin(ImportExportModelAdmin, admin.ModelAdmin):
"""Django admin settings for ImageServer."""

resource_class = ImageServerResource
list_display = ('server_base',)
list_display = ("server_base",)


admin.site.register(Manifest, ManifestAdmin)
admin.site.register(Note, NoteAdmin)
Expand Down
14 changes: 13 additions & 1 deletion apps/iiif/manifests/documents.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Elasticsearch indexing rules for IIIF manifests"""

from os import environ
from html import unescape

from django.conf import settings
Expand Down Expand Up @@ -88,6 +87,19 @@ class Meta:
]
)

def should_index_object(self, obj):
"""
Overwriting parent method.
Only index volumes marked 'searchable'
"""
return obj.searchable

# def get_queryset(self):
# """
# Overwrite parent method to only include searchable volumes.
# """
# return self.django.model._default_manager.filter(searchable=True)

def prepare_authors(self, instance):
"""convert authors string into list"""
if instance.author:
Expand Down
23 changes: 23 additions & 0 deletions apps/iiif/manifests/migrations/0059_auto_20250113_1401.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2.23 on 2025-01-13 14:01

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('manifests', '0058_alter_relatedlink'),
]

operations = [
migrations.AddField(
model_name='manifest',
name='searchable',
field=models.BooleanField(default=True),
),
migrations.AlterField(
model_name='imageserver',
name='storage_service',
field=models.CharField(choices=[('sftp', 'SFTP'), ('s3', 'S3'), ('remote', 'Remote'), ('local', 'Local')], default='sftp', max_length=10),
),
]
Loading

0 comments on commit 1aed0ec

Please sign in to comment.