Skip to content

Commit

Permalink
Fix manage command errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jayvarner committed Oct 2, 2024
1 parent c3e6213 commit 85cd4bc
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions readux_ingest_ecds/management/commands/add_ocr.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
from django.core.management.base import BaseCommand, CommandError
from readux_ingest_ecds.helpers import get_iiif_models
from readux_ingest_ecds.services.ocr_services import add_ocr_to_canvases, get_ocr, add_ocr_annotations
from readux_ingest_ecds.services.ocr_services import (
add_ocr_to_canvases,
get_ocr,
add_ocr_annotations,
)

Manifest = get_iiif_models()["Manifest"]
Canvas = get_iiif_models()["Canvas"]
OCR = get_iiif_models()["OCR"]


class Command(BaseCommand):
help = "(Re)Build OCR for a volume or canvas."

def add_arguments(self, parser):
parser.add_argument(
"--volume", type=str, help="PID for volume/manifest."
)
parser.add_argument("--volume", type=str, help="PID for volume/manifest.")

parser.add_argument(
"--canvas", type=str, help="PID for canvas."
)
parser.add_argument("--canvas", type=str, help="PID for canvas.")

def handle(self, *args, **options):
if options[:manifest]:
if options["volume"]:
try:
manifest = Manifest.objects.get(pid=options["volume"])
except Manifest.DoesNotExist:
raise CommandError(f'Manifest {options["volume"]} does not exist')

add_ocr_to_canvases(manifest)
self.stdout.write(
self.style.SUCCESS(f'OCR create for {manifest.pid}')
)
elif options[:canvas]:
self.stdout.write(self.style.SUCCESS(f"OCR create for {manifest.pid}"))
elif options["canvas"]:
try:
canvas = Canvas.objects.get(pid=options['canvas'])
canvas = Canvas.objects.get(pid=options["canvas"])
except Canvas.DoesNotExist:
raise CommandError(f'Canvas {options['canvas']} does not exist.')
raise CommandError(f"Canvas {options['canvas']} does not exist.")

new_ocr_annotations = []
ocr = get_ocr(canvas)
Expand All @@ -42,10 +41,10 @@ def handle(self, *args, **options):

OCR.objects.bulk_create(new_ocr_annotations)

self.stdout.write(
self.style.SUCCESS(f'OCR create for {canvas.pid}')
)
self.stdout.write(self.style.SUCCESS(f"OCR create for {canvas.pid}"))
else:
self.stdout.write(
self.style.ERROR('You must provide a canvas of volume pid using the --volume or --canvas option.')
self.style.ERROR(
"You must provide a canvas of volume pid using the --volume or --canvas option."
)
)

0 comments on commit 85cd4bc

Please sign in to comment.