def metadata_element_pre_create_handler(sender, **kwargs): """Select proper form class based on element_name. This handler is executed only when a metadata element is added as part of editing a resource """ element_name = kwargs['element_name'] request = kwargs['request'] if element_name == "subject": # keywords element_form = SubjectsForm(data=request.POST) elif element_name == "description": # abstract element_form = AbstractValidationForm(request.POST) elif element_name == "creator": try: post_data_dict = Party.get_post_data_with_identifiers(request=request) except Exception as ex: return {'is_valid': False, 'element_data_dict': None, "errors": {"identifiers": [ex.message]}} element_form = CreatorValidationForm(post_data_dict) elif element_name == "contributor": try: post_data_dict = Party.get_post_data_with_identifiers(request=request) except Exception as ex: return {'is_valid': False, 'element_data_dict': None, "errors": {"identifiers": [ex.message]}} element_form = ContributorValidationForm(post_data_dict) elif element_name == 'relation': element_form = RelationValidationForm(request.POST) elif element_name == 'source': element_form = SourceValidationForm(request.POST) elif element_name == 'rights': element_form = RightsValidationForm(request.POST) elif element_name == 'language': element_form = LanguageValidationForm(request.POST) elif element_name == 'date': element_form = ValidDateValidationForm(request.POST) elif element_name == 'fundingagency': element_form = FundingAgencyValidationForm(request.POST) elif element_name == 'coverage': if 'type' in request.POST: if request.POST['type'].lower() == 'point' or request.POST['type'].lower() == 'box': element_form = CoverageSpatialForm(data=request.POST) else: element_form = CoverageTemporalForm(data=request.POST) else: element_form = CoverageTemporalForm(data=request.POST) elif element_name == 'identifier': element_form = IdentifierForm(data=request.POST) else: raise Exception("Invalid metadata element name:{}".format(element_name)) if element_form.is_valid(): return {'is_valid': True, 'element_data_dict': element_form.cleaned_data} else: return {'is_valid': False, 'element_data_dict': None, "errors": element_form.errors}
def metadata_element_pre_create_handler(sender, **kwargs): element_name = kwargs['element_name'] request = kwargs['request'] if element_name == "subject": # keywords element_form = SubjectsForm(data=request.POST) elif element_name == "description": # abstract element_form = AbstractValidationForm(request.POST) elif element_name == "creator": element_form = CreatorValidationForm(request.POST) elif element_name == "contributor": element_form = ContributorValidationForm(request.POST) elif element_name == 'relation': element_form = RelationValidationForm(request.POST) elif element_name == 'source': element_form = SourceValidationForm(request.POST) elif element_name == 'rights': element_form = RightsValidationForm(request.POST) elif element_name == 'language': element_form = LanguageValidationForm(request.POST) elif element_name == 'date': element_form = ValidDateValidationForm(request.POST) elif element_name == 'fundingagency': element_form = FundingAgencyValidationForm(request.POST) elif element_name == 'coverage': if 'type' in request.POST: if request.POST['type'].lower() == 'point' or request.POST['type'].lower() == 'box': element_form = CoverageSpatialForm(data=request.POST) else: element_form = CoverageTemporalForm(data=request.POST) else: element_form = CoverageTemporalForm(data=request.POST) elif element_name == 'identifier': element_form = IdentifierForm(data=request.POST) else: raise Exception("Invalid metadata element name:{}".format(element_name)) if element_form.is_valid(): return {'is_valid': True, 'element_data_dict': element_form.cleaned_data} else: return {'is_valid': False, 'element_data_dict': None, "errors": element_form.errors}