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

add functions to check for deleted objects in the doi matching code #520

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
41 changes: 37 additions & 4 deletions cmr/doi_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,17 +346,50 @@ def serialize_recommendation(doi_recommendation):
doi_recommendation[field] = str(doi_recommendation[field])
return doi_recommendation

def _convert_field_name_to_model(field_name):
"""
takes a field name such as `campaigns` or `collection_periods` and turns it into a model
such as Campaign or CollectionPeriod
"""

# depluralize the field name
field_name = field_name[:-1] if field_name.endswith('s') else field_name
model_name = field_name.replace("_", "")

return apps.get_model("data_models", model_name)

def _linked_object_exists(self, field_name, uuid):
"""
searches a model for a uuid to confirm whether it exists
this allows us to refrain from adding deleted objects to update drafts
"""
model = self._convert_field_name_to_model(field_name)
return bool(model.objects.filter(uuid=uuid).count())

def create_merged_draft(self, recent_draft, doi_recommendation):
"""Takes an existing doi draft and a newly generated doi_recommendation and
returns a merged object that represents the most up-to-date data, retaining
the originally curated fields but updating any core CMR values.
"""

for field in self.previously_curated_fields:
if recent_draft.update.get(field):
doi_recommendation[field] = recent_draft.update[field]

return doi_recommendation
field_value = recent_draft.update.get(field)
if not field_value:
continue

if field in [
'campaigns',
'instruments',
'platforms',
'collection_periods',
] and not self._linked_object_exists(field, recent_draft.uuid):
continue

# recommendations have fields updated/added only if they
# exist in the prior draft
# in the case of linked fields, still exist in the proper database

doi_recommendation[field] = field_value

@staticmethod
def make_create_draft(doi_recommendation):
Expand Down