From 0403c29d70eb08eaaa79984cf9a952550bd37ba0 Mon Sep 17 00:00:00 2001 From: Longze Chen Date: Fri, 8 Dec 2023 12:10:44 -0500 Subject: [PATCH] A few tweaks for fetch cedar templates command --- .../fetch_cedar_metadata_templates.py | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/osf/management/commands/fetch_cedar_metadata_templates.py b/osf/management/commands/fetch_cedar_metadata_templates.py index f4da83b608c..265a75a6b72 100644 --- a/osf/management/commands/fetch_cedar_metadata_templates.py +++ b/osf/management/commands/fetch_cedar_metadata_templates.py @@ -3,27 +3,33 @@ from osf.models import CedarMetadataTemplate from osf.external.cedar.client import CedarClient + class Command(BaseCommand): + def handle(self, *args, **kwargs): + + # TODO: add error handling ids = CedarClient().retrieve_all_template_ids() for cedar_id in ids: - json = CedarClient().retrieve_template_by_id(cedar_id) - title = json['title'] - older_versions = CedarMetadataTemplate.objects.filter(cedar_id=cedar_id).order_by('-template_version') - latest_version = older_versions.first() - if latest_version and json != latest_version.template: - new_template = CedarMetadataTemplate.objects.create( - title=title, - template=json, + # TODO: add error handling + template = CedarClient().retrieve_template_by_id(cedar_id) + schema_name = template['schema:name'] + pav_last_updated_on = template['pav:lastUpdatedOn'] + existing_versions = CedarMetadataTemplate.objects.filter(cedar_id=cedar_id) + if not existing_versions: + CedarMetadataTemplate.objects.create( + schema_name=schema_name, + template=template, cedar_id=cedar_id, - template_version=latest_version.template_version + 1 + template_version=1 ) - new_template.save() - else: - new_template = CedarMetadataTemplate.objects.create( - title=title, - template=json, + latest_version = existing_versions.order_by('-template_version').first() + if pav_last_updated_on != latest_version.template['pav:lastUpdatedOn']: + CedarMetadataTemplate.objects.create( + schema_name=schema_name, + template=template, cedar_id=cedar_id, - template_version=1 + template_version=latest_version.template_version + 1 ) - new_template.save() + latest_version.active = False + latest_version.save()