def _replace_snapshots(self, replace_snapshots, dataset_id): """Replace the dataset snapshots (dataset_id) from the specified dataset (replace_snapshots)""" # Look up the two datasets, exit on dataset not found try: data_set = DataSet.objects.get(pk=dataset_id) except DataSet.DoesNotExist: raise CommandError("DataSet not found: %s\n" % dataset_id) try: replace_set = DataSet.objects.get(pk=replace_snapshots) except DataSet.DoesNotExist: raise CommandError("DataSet not found: %s\n" % replace_snapshots) # # Copy the snapshots from the replacement set in to the target set # existing_snapshots = Snapshot.objects.filter(dataset=data_set) existing_snapshots.delete() snapshots = Snapshot.objects.filter(dataset=replace_set) for source_snapshot in snapshots: snapshot_dict = model_to_dict(source_snapshot, exclude=["id", "dataset"]) new_snapshot = Snapshot(**snapshot_dict) new_snapshot.dataset = data_set new_snapshot.save() return
def _load_properties(self, data_set, xml_filename): """Replace the existing properties for the nominated dataset.""" # Delete the existing properties properties = DataSetProperty.objects.filter(dataset=data_set) properties.delete() # Iterate over the supplied file and load the new properties tree = ET.ElementTree(file=xml_filename) fields = tree.getroot().find("properties") for field in fields: data_type_name = field.attrib.get("Type", None) data_type = DataSetProperty.data_type_enum(data_type_name) label = field.text field_name = field.attrib.get("DBFieldName", label) is_computed = field.attrib.get("IsComputedField", False) is_filter = field.attrib.get("IsFilterField", True) is_output = field.attrib.get("IsOutputField", True) description = field.attrib.get("FieldDesc", "") units = field.attrib.get("Units", "") if int(self._options["verbosity"]) >= 2: print ("Adding {field}...".format(field=field_name)) new_property = DataSetProperty( dataset=data_set, name=field_name, label=label, data_type=data_type, units=units, is_computed=is_computed, is_filter=is_filter, is_output=is_output, description=description, ) new_property.save() snapshots = Snapshot.objects.filter(dataset=data_set) snapshots.delete() redshifts = tree.getroot().find("snapshots") if redshifts is None: # redshifts are optional redshifts = [] for redshift in redshifts: value = redshift.text new_redshift = Snapshot(dataset=data_set, redshift=value) new_redshift.save()