Skip to content

Commit

Permalink
A few tweaks for fetch cedar templates command
Browse files Browse the repository at this point in the history
  • Loading branch information
cslzchen committed Dec 8, 2023
1 parent 82edf2f commit 0403c29
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions osf/management/commands/fetch_cedar_metadata_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 0403c29

Please sign in to comment.