Esempio n. 1
0
def add_graph_data(graph, dataset, value, cluster=None, horiz_value=None, pval=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))
    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)
    if graph.use_clusters():
        if not cluster:
            raise LoaderException("Must supply cluster for data for graph %s" % graph.tile.url)   
        elif not isinstance(cluster, GraphCluster):
            try:
                cluster = GraphCluster.objects.get(graph=graph, url=cluster)
            except GraphCluster.DoesNotExist:
                raise LoaderException("Cluster %s for graph %s does not exist" % (str(cluster), graph.tile.url))
        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
Esempio n. 2
0
 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()
Esempio n. 3
0
 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()
Esempio n. 4
0
def edit_graph(request, widget_url, label, tile_url, pval_id=None):
    try:
        w = WidgetDefinition.objects.get(family__url=widget_url, 
                        label=label)
    except WidgetDefinition.DoesNotExist:
        return HttpResponseNotFound("This Widget Definition does not exist")
    if not user_has_edit_permission(request.user, w):
        return HttpResponseForbidden("You do not have permission to edit the data for this widget")
    try:
        g = GraphDefinition.objects.get(tile__widget=w, tile__url=tile_url)
    except GraphDefinition.DoesNotExist:
        return HttpResponseNotFound("This Graph does not exist")
    if not g.widget().parametisation:
        pval = None
    else:
        try:
            pval = ParametisationValue.objects.get(pk=pval_id)
        except ParametisationValue.DoesNotExist:
            return HttpResponseNotFound("This parameter value set does not exist")

    form_class = get_form_class_for_graph(g)
    form_class = forms.formsets.formset_factory(form_class, can_delete=True, extra=10)
    overrides_form_class = get_override_form_class_for_graph(g)
    overrides_form = None
    return_redirect = False
    if request.method == 'POST':
        if request.POST.get("submit") or request.POST.get("submit_stay"):
            form = form_class(request.POST)
            if overrides_form_class:
                overrides_form = overrides_form_class(request.POST)
            if form.is_valid() and (not overrides_form or overrides_form.is_valid()):
                clear_graph_data(g, pval=pval)
                for subform in form:
                    fd = subform.cleaned_data
                    if fd and not fd.get("DELETE"):
                        gd = GraphData(graph=g, param_value=pval)
                        gd.value = fd["value"]
                        if g.use_clusters():
                            # Lookup?
                            gd.cluster = fd["cluster"]
                        # Lookup?
                        gd.dataset = fd["dataset"]
                        if g.graph_type == g.LINE:
                            if g.horiz_axis_type == g.NUMERIC:
                                gd.horiz_numericval = fd["horiz_value"]
                            elif g.horiz_axis_type == g.DATE:
                                gd.horiz_dateval = fd["horiz_value"]
                            elif g.horiz_axis_type == g.TIME:
                                gd.horiz_timeval = fd["horiz_value"]
                            elif g.horiz_axis_type == g.TIME:
                                gd.horiz_dateval = fd["horiz_value"].date()
                                gd.horiz_timeval = fd["horiz_value"].time()
                        gd.save()
                if overrides_form:
                    fod = overrides_form.cleaned_data
                    for fldname, fldval in fod.items():
                        ov_type, ov_url = fldname.split("_", 1)
                        if ov_type == 'cluster':
                            set_cluster_override(g, ov_url, fldval, pval=pval)
                        else:
                            set_dataset_override(g, ov_url, fldval, pval=pval)
                if request.POST.get("submit"):
                    return_redirect=True
                else:
                    form = form_class(initial=g.initial_form_data(pval))
                    if overrides_form_class:
                        overrides_form = overrides_form_class(initial=g.initial_override_form_data(pval))
        elif request.POST.get("cancel"):
            return_redirect=True
        else:
            form = form_class(initial=g.initial_form_data(pval))
            if overrides_form_class:
                overrides_form = overrides_form_class(initial=g.initial_override_form_data(pval))
    else:
        form = form_class(initial=g.initial_form_data(pval))
        if overrides_form_class:
            overrides_form = overrides_form_class(initial=g.initial_override_form_data(pval))
    if return_redirect:
        if pval:
            return redirect("view_parametised_widget_data", 
                        widget_url=w.family.url, 
                        label=label, pval_id=pval_id)
        else: 
            return redirect("view_widget_data", 
                        widget_url=w.family.url, 
                        label=label)
    return render(request, "widget_data/edit_graph.html", {
                "widget": w,
                "pval": pval,
                "graph": g,
                "form": form,
                "overrides_form": overrides_form
                })
Esempio n. 5
0
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