Skip to content

Commit

Permalink
feat: add diff to import_attribute method
Browse files Browse the repository at this point in the history
Signed-off-by: David Wallace <[email protected]>
  • Loading branch information
MyPyDavid committed Nov 2, 2023
1 parent 7d86eed commit 052c154
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions rdmo/domain/imports.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import copy
import logging

from django.contrib.sites.models import Site

from rdmo.core.imports import check_permissions, set_common_fields, set_foreign_field, validate_instance
from rdmo.core.imports import (
check_diff_instance,
check_permissions,
set_common_fields,
set_foreign_field,
validate_instance,
)

from .models import Attribute
from .validators import AttributeLockedValidator, AttributeParentValidator, AttributeUniqueURIValidator
Expand All @@ -13,8 +20,13 @@
def import_attribute(element, save=False, user=None):
try:
attribute = Attribute.objects.get(uri=element.get('uri'))
original_attribute = copy.deepcopy(vars(attribute))
element['updated'] = True
_msg = 'Attribute %s updated.', element.get('uri')
except Attribute.DoesNotExist:
attribute = Attribute()
element['created'] = True
_msg = 'Attribute created with uri %s.', element.get('uri')

set_common_fields(attribute, element)

Expand All @@ -27,15 +39,20 @@ def import_attribute(element, save=False, user=None):

check_permissions(attribute, element, user)

if save and not element.get('errors'):
if attribute.id:
element['updated'] = True
logger.debug('Attribute %s updated.', element.get('uri'))
else:
element['created'] = True
logger.debug('Attribute created with uri %s.', element.get('uri'))
if element.get('errors'):
return attribute

if element['updated']:

diffs = check_diff_instance(original_attribute, element, check=True)
if diffs:
# breakpoint()
diff_warning = "\n".join([f"{k}:{' '.join(val)}" for k, val in diffs.items()])
element['diffs'] = diff_warning

if save:
attribute.save()
attribute.editors.add(Site.objects.get_current())
logger.debug(_msg)

return attribute

0 comments on commit 052c154

Please sign in to comment.