Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix default thumbnail normalization command #1935

Merged
merged 2 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
"""Update commands and rules for Python 3 compatibility."""
from django.db import migrations

JHOVE_TOOL_ID = "085d8690-93b7-4d31-84f7-2c5f4cbf6735"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
from django.db import migrations

OLD_DEFAULT_THUMBNAIL_CMD_UUID = "95149bc4-0620-4c20-964c-1d6c34b9400e"

DEFAULT_THUMBNAIL_RULES = ("3a19f9a3-c5d5-4934-9286-13b3ad6c24d3",)

NEW_DEFAULT_THUMBNAIL_CMD_UUID = "484d3a8f-9e59-4912-a5b8-f8a2deb3466a"
NEW_DEFAULT_THUMBNAIL_CMD = """
import argparse
import base64
import sys

# http://i.imgur.com/ijwSkff.jpg
DEFAULT_THUMBNAIL = \"\"\"
/9j/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAAwADADASIAAhEBAxEB/8QAHAABAQACAgMA
AAAAAAAAAAAACAAFCQEKAwQG/8QANBAAAAUDAgIFCwUAAAAAAAAAAQMEBQYAAgcICRESExQ5eLcV
GSExV1iVmMHS1xZScYWn/8QAFgEBAQEAAAAAAAAAAAAAAAAAAAEC/8QAKBEAAgADBgUFAAAAA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eKfNZH/2Q==\"\"\"

def main(target):
with open(target, 'wb') as f:
f.write(base64.b64decode(DEFAULT_THUMBNAIL))

if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--output-location', required=True)
args, _ = parser.parse_known_args()

sys.exit(main(args.output_location))
"""


def data_migration_up(apps, schema_editor):
FPCommand = apps.get_model("fpr", "FPCommand")
FPRule = apps.get_model("fpr", "FPRule")

# Get the old command
old_command = FPCommand.objects.get(uuid=OLD_DEFAULT_THUMBNAIL_CMD_UUID)

# Replace the existing command with the following
FPCommand.objects.create(
uuid=NEW_DEFAULT_THUMBNAIL_CMD_UUID,
replaces_id=OLD_DEFAULT_THUMBNAIL_CMD_UUID,
tool_id=old_command.tool_id,
enabled=old_command.enabled,
command=NEW_DEFAULT_THUMBNAIL_CMD,
script_type=old_command.script_type,
command_usage=old_command.command_usage,
description=old_command.description,
output_location=old_command.output_location,
output_format_id=old_command.output_format_id,
event_detail_command_id=old_command.event_detail_command_id,
verification_command_id=old_command.verification_command_id,
)

# Update existing rules
FPRule.objects.filter(uuid__in=DEFAULT_THUMBNAIL_RULES).update(
command_id=NEW_DEFAULT_THUMBNAIL_CMD_UUID,
)

# Disable the old command
old_command.enabled = False
old_command.save()


def data_migration_down(apps, schema_editor):
FPCommand = apps.get_model("fpr", "FPCommand")
FPRule = apps.get_model("fpr", "FPRule")

# The order matters. We make sure that the rules point to the previous
# command before the latter is deleted. Otherwise our rules would be
# deleted by Django's on cascade mechanism
FPRule.objects.filter(uuid__in=DEFAULT_THUMBNAIL_RULES).update(
command_id=OLD_DEFAULT_THUMBNAIL_CMD_UUID,
)

# Enable the old command. At this point we do not know if the
# command was in fact enabled before the migration was run, so
# this may have unexpected consequences
old_command = FPCommand.objects.get(uuid=OLD_DEFAULT_THUMBNAIL_CMD_UUID)
old_command.enabled = True
old_command.save()

# Delete the new command
FPCommand.objects.filter(uuid=NEW_DEFAULT_THUMBNAIL_CMD_UUID).delete()


class Migration(migrations.Migration):
dependencies = [("fpr", "0042_update_idtools")]

operations = [migrations.RunPython(data_migration_up, data_migration_down)]