def process_subform(self, subform): fd = subform.cleaned_data if fd and not fd.get("DELETE") and not subform.is_blank(fd): gd = GraphData(graph=self.graph, param_value=self.pval) gd.value = fd["value"] gd.err_valmin = fd.get("err_valmin") gd.err_valmax = fd.get("err_valmax") if self.graph.use_clusters(): gd.set_cluster(fd["cluster"]) gd.dataset = fd["dataset"] if self.graph.graph_type == self.graph.LINE: if self.graph.horiz_axis_type == self.graph.NUMERIC: gd.horiz_numericval = fd["horiz_value"] elif self.graph.horiz_axis_type == self.graph.DATE: gd.horiz_dateval = fd["horiz_value"] elif self.graph.horiz_axis_type == self.graph.TIME: gd.horiz_timeval = fd["horiz_value"] elif self.graph.horiz_axis_type == self.graph.TIME: gd.horiz_dateval = fd["horiz_value"].date() gd.horiz_timeval = fd["horiz_value"].time() gd.save()
def add_graph_data(graph, dataset, value, cluster=None, horiz_value=None, pval=None, val_min=None, val_max=None): """Add a graph datapoint. Return the newly created GraphData object on success. Raise a LoaderException on error, or if the provided arguments are not valid for the graph. """ if not isinstance(dataset, GraphDataset): try: dataset = GraphDataset.objects.get(graph=graph, url=dataset) except GraphDataset.DoesNotExist: raise LoaderException("Dataset %s for graph %s does not exist" % (str(dataset), graph.tile.url)) if dataset.use_error_bars: if val_min is None or val_max is None: raise LoaderException( "Dataset %s for graph %s requires error bar limits" % (dataset.url, graph.tile.url)) else: if val_min is not None or val_max is not None: raise LoaderException( "Dataset %s for graph %s does not use error bars" % (dataset.url, graph.tile.url)) value = decimal.Decimal(value).quantize(decimal.Decimal("0.0001"), rounding=decimal.ROUND_HALF_UP) gd = GraphData(graph=graph, param_value=pval, dataset=dataset, value=value, err_valmin=val_min, err_valmax=val_max) if graph.use_clusters(): if not cluster: raise LoaderException("Must supply cluster for data for graph %s" % graph.tile.url) elif not isinstance(cluster, GraphClusterBase): try: if graph.dynamic_clusters: cluster = DynamicGraphCluster.objects.get(graph=graph, url=cluster, param_value=pval) else: cluster = GraphCluster.objects.get(graph=graph, url=cluster) except ObjectDoesNotExist: raise LoaderException( "Cluster %s for graph %s does not exist" % (str(cluster), graph.tile.url)) if graph.dynamic_clusters: gd.dynamic_cluster = cluster else: gd.cluster = cluster else: if graph.horiz_axis_type == graph.NUMERIC: gd.horiz_numericval = decimal.Decimal(horiz_value).quantize( decimal.Decimal("0.0001"), rounding=decimal.ROUND_HALF_UP) elif graph.horiz_axis_type == graph.DATE: gd.horiz_dateval = horiz_value elif graph.horiz_axis_type == graph.TIME: gd.horiz_timeval = horiz_value elif graph.horiz_axis_type == graph.DATETIME: gd.horiz_dateval = horiz_value.date() gd.horiz_timeval = horiz_value.time() gd.save() return gd