def metadata_element_pre_update_handler(sender, **kwargs): """ Since each of the Raster metadata element is required no need to listen to any delete signal The Raster landing page should not have delete UI functionality for the resource specific metadata elements """ element_name = kwargs['element_name'].lower() request = kwargs['request'] if element_name == "cellinformation": element_form = CellInfoValidationForm(request.POST) elif element_name == 'bandinformation': form_data = {} for field_name in BandInfoValidationForm().fields: matching_key = [ key for key in request.POST if '-' + field_name in key ][0] form_data[field_name] = request.POST[matching_key] element_form = BandInfoValidationForm(form_data) elif element_name == 'originalcoverage': element_form = OriginalCoverageSpatialForm(data=request.POST) 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 update(self, metadata, user): # overriding the base class update method for bulk update of metadata from forms import BandInfoValidationForm # update any core metadata super(RasterMetaData, self).update(metadata, user) # update resource specific metadata # for geo raster resource type only band information can be updated missing_file_msg = "Resource specific metadata can't be updated when there is no " \ "content files" # update repeatable element (BandInformation) for dict_item in metadata: if 'bandinformation' in dict_item: if not self.resource.files.all(): raise ValidationError(missing_file_msg) bandinfo_data = dict_item['bandinformation'] if 'original_band_name' not in bandinfo_data: raise ValidationError("Invalid band information data") # find the matching (lookup by name) bandinformation element to update band_element = self.bandInformations.filter( name=bandinfo_data['original_band_name']).first() if band_element is None: raise ValidationError( "No matching band information element was found") bandinfo_data.pop('original_band_name') if 'name' not in bandinfo_data: bandinfo_data['name'] = band_element.name if 'variableName' not in bandinfo_data: bandinfo_data['variableName'] = band_element.variableName if 'variableUnit' not in bandinfo_data: bandinfo_data['variableUnit'] = band_element.variableUnit validation_form = BandInfoValidationForm(bandinfo_data) if not validation_form.is_valid(): err_string = self.get_form_errors_as_string( validation_form) raise ValidationError(err_string) self.update_element('bandinformation', band_element.id, **bandinfo_data)
def update(self, metadata, user): # overriding the base class update method for bulk update of metadata from forms import BandInfoValidationForm # update any core metadata super(RasterMetaData, self).update(metadata, user) # update resource specific metadata # for geo raster resource type only band information can be updated missing_file_msg = "Resource specific metadata can't be updated when there is no " \ "content files" # update repeatable element (BandInformation) for dict_item in metadata: if 'bandinformation' in dict_item: if not self.resource.files.all(): raise ValidationError(missing_file_msg) bandinfo_data = dict_item['bandinformation'] if 'original_band_name' not in bandinfo_data: raise ValidationError("Invalid band information data") # find the matching (lookup by name) bandinformation element to update band_element = self.bandInformations.filter( name=bandinfo_data['original_band_name']).first() if band_element is None: raise ValidationError("No matching band information element was found") bandinfo_data.pop('original_band_name') if 'name' not in bandinfo_data: bandinfo_data['name'] = band_element.name if 'variableName' not in bandinfo_data: bandinfo_data['variableName'] = band_element.variableName if 'variableUnit' not in bandinfo_data: bandinfo_data['variableUnit'] = band_element.variableUnit validation_form = BandInfoValidationForm(bandinfo_data) if not validation_form.is_valid(): err_string = self.get_form_errors_as_string(validation_form) raise ValidationError(err_string) self.update_element('bandinformation', band_element.id, **bandinfo_data)
def metadata_element_pre_create_handler(sender, **kwargs): element_name = kwargs['element_name'].lower() request = kwargs['request'] if element_name == "cellinformation": element_form = CellInfoValidationForm(request.POST) elif element_name == 'bandinformation': element_form = BandInfoValidationForm(request.POST) elif element_name == 'originalcoverage': element_form = OriginalCoverageSpatialForm(data=request.POST) 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 }