def get(self, request, dataset_type_id=None): """Return a rendered html page that contains the metadata form and ability to add or delete measurements. This can be built upon an existing dataset type (dataset_type_id) or a blank form. Args: dataset_type_id: optional id to an existing dataset Returns: Rendered HTML for a page that will allow users to customize a dataset """ context = { 'measurements_form': forms.DatasetTypeMeasurementsForm(), 'flags_definition_form': forms.DatasetTypeFlagsDefinitionForm(), } if dataset_type_id: dataset_type = models.DatasetType.objects.using('agdc').get( id=dataset_type_id) context.update( utils.forms_from_definition(dataset_type.definition, display_only=False)) else: context['metadata_form'] = forms.DatasetTypeMetadataForm() return render(request, 'data_cube_manager/dataset_type.html', context)
def create_measurement_form(post_data): measurement_forms = { 'measurement_form': forms.DatasetTypeMeasurementsForm(post_data) } if measurement_forms['measurement_form'].is_valid(): if measurement_forms['measurement_form'].cleaned_data[ 'flags_definition']: measurement_forms[ 'flags_definition_form'] = forms.DatasetTypeFlagsDefinitionForm( post_data) return measurement_forms
def get(self, request, dataset_type_id=None): """View a dataset type and all its attributes Context: Bound forms for: DatasetTypeMeasurementsForm, DatasetTypeFlagsDefinitionForm dataset_type_id: id/pk of the dataset type """ context = { 'measurements_form': forms.DatasetTypeMeasurementsForm(), 'flags_definition_form': forms.DatasetTypeFlagsDefinitionForm(), 'dataset_type_id': dataset_type_id, } dataset_type = models.DatasetType.objects.using('agdc').get(id=dataset_type_id) context.update(utils.forms_from_definition(dataset_type.definition, display_only=True)) return render(request, 'data_cube_manager/dataset_type.html', context)
def forms_from_definition(product_def, display_only=True): #anything without a fallback for .get is required #can't really just do a direct copy/update since things are nested strangly + instrument+data format are held in name. metadata_def = product_def.get('metadata') storage = product_def.get('storage', {}) metadata = { 'name': product_def.get('name'), 'description': product_def.get('description'), 'metadata_type': product_def.get('metadata_type'), #nested in metadata 'platform': metadata_def.get('platform', {}).get('code', None), 'instrument': metadata_def.get('instrument', {}).get('name', None), 'product_type': metadata_def.get('product_type', None), 'data_format': metadata_def.get('format', {}).get('name', None), #stuff nested in storage. 'storage_driver': storage.get('driver', ''), 'resolution_longitude': storage.get('resolution', {}).get('longitude', ''), 'resolution_latitude': storage.get('resolution', {}).get('latitude', ''), 'crs': storage.get('crs', ''), 'tile_size_longitude': storage.get('tile_size', {}).get('longitude', ''), 'tile_size_latitude': storage.get('tile_size', {}).get('latitude', ''), 'chunking_time': storage.get('chunking', {}).get('time', ''), 'chunking_longitude': storage.get('chunking', {}).get('longitude', ''), 'chunking_latitude': storage.get('chunking', {}).get('latitude', ''), } measurements_def = product_def.get("measurements", None) measurements = OrderedDict() initial_measurement = None for measurement in measurements_def: #get standard measurement info measurement_data = { 'name': measurement.get('name'), 'dtype': measurement.get('dtype'), 'nodata': measurement.get('nodata'), 'units': measurement.get('units'), 'aliases': ",".join(measurement.get('aliases', [])), 'flags_definition': measurement.get('flags_definition', False) } measurement_data['flags_definition'] = True if measurement_data[ 'flags_definition'] else False if initial_measurement is None: initial_measurement = measurement_data['name'] #set the measurement form measurements[measurement_data['name']] = { 'measurement_form': forms.DatasetTypeMeasurementsForm( measurement_data, existing_dataset_type=display_only) } #if flag defs exist populate a form and attach to dict. flag_name = list(measurement.get('flags_definition').keys() )[0] if measurement_data['flags_definition'] else None flag_values = sorted( measurement.get('flags_definition').get(flag_name).get('values'). keys()) if measurement_data['flags_definition'] else None measurements[measurement_data['name']][ 'flags_definition_form'] = forms.DatasetTypeFlagsDefinitionForm( { 'flag_name': flag_name, 'bits': ",".join( map( str, measurement.get('flags_definition').get( flag_name).get('bits'))), 'description': measurement.get('flags_definition').get(flag_name).get( 'description'), 'values_for_bits': ",".join(flag_values), 'values': ",".join([ str( measurement.get('flags_definition').get( flag_name).get('values')[key]) for key in flag_values ]) }, existing_dataset_type=display_only ) if measurement_data['flags_definition'] else None return { 'metadata_form': forms.DatasetTypeMetadataForm(metadata, existing_dataset_type=display_only), 'measurements': measurements, 'initial_measurement': initial_measurement }