bulk metadata update validation for model instance resource type
@@ -2,6 +2,7 @@ from lxml import etree
from django.contrib.contenttypes.fields import GenericRelation
from django.db import models, transaction
+from django.core.exceptions import ValidationError
from mezzanine.pages.page_processors import processor_for
@@ -10,6 +11,7 @@ from hs_core.hydroshare import utils
from hs_model_program.models import ModelProgramResource
+
# extended metadata elements for Model Instance resource type
class ModelOutput(AbstractMetaDataElement):
term = 'ModelOutput'
@@ -29,6 +31,7 @@ class ModelOutput(AbstractMetaDataElement):
else:
return "No"
+
class ExecutedBy(AbstractMetaDataElement):
term = 'ExecutedBY'
# model_name: the id of the model program used for execution
@@ -166,14 +169,25 @@ class ModelInstanceMetaData(CoreMetaData):
def update(self, metadata, user):
# overriding the base class update method for bulk update of metadata
+ from forms import ModelOutputValidationForm, ExecutedByValidationForm
+
super(ModelInstanceMetaData, self).update(metadata, user)
attribute_mappings = {'modeloutput': 'model_output', 'executedby': 'executed_by'}
with transaction.atomic():
# update/create non-repeatable element
for element_name in attribute_mappings.keys():
+ for dict_item in metadata:
+ if element_name in dict_item:
+ if element_name == 'modeloutput':
+ validation_form = ModelOutputValidationForm(dict_item[element_name])
+ else:
+ validation_form = ExecutedByValidationForm(dict_item[element_name])
+ if not validation_form.is_valid():
+ err_string = self.get_form_errors_as_string(validation_form)
+ raise ValidationError(err_string)
element_property_name = attribute_mappings[element_name]
- self.update_non_repeatable_element(element_name, metadata, element_property_name)
-
+ self.update_non_repeatable_element(element_name, metadata,
+ element_property_name)
def get_xml(self, pretty_print=True, include_format_elements=True):
# get the xml string representation of the core metadata elements
