def get_stats(self): if user_has_edit_all_permission(self.request.user, self.widget): statistics = Statistic.objects.filter(tile__widget=self.widget) else: statistics = Statistic.objects.filter(editable=True, tile__widget=self.widget) stats = [] for s in statistics: try: data = StatisticData.objects.get(statistic=s, param_value=self.pval) except StatisticData.DoesNotExist: data = None listdata = StatisticListItem.objects.filter(statistic=s, param_value=self.pval) stats.append({ "statistic": s, "data_last_updated": s.data_last_updated(pval=self.pval), "data": data, "listdata": listdata, }) return stats
def view_widget(request, widget_url, label, 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") if w.parametisation and not pval_id: return HttpResponseNotFound("This Widget Definition is parametised") if not w.parametisation: pval = None else: try: pval = ParametisationValue.objects.get(pk=pval_id) except ParametisationValue.DoesNotExist: return HttpResponseNotFound("This parameter value set does not exist") edit_all = user_has_edit_all_permission(request.user, w) if edit_all: statistics = Statistic.objects.filter(tile__widget=w) else: statistics = Statistic.objects.filter(editable=True,tile__widget=w) stats = [] for s in statistics: try: data = StatisticData.objects.get(statistic=s, param_value=pval) except StatisticData.DoesNotExist: data = None listdata = StatisticListItem.objects.filter(statistic=s, param_value=pval) stats.append({ "statistic": s, "data_last_updated": s.data_last_updated(pval=pval), "data": data, "listdata": listdata, }) graphs = [] for graph in GraphDefinition.objects.filter(tile__widget=w): data = graph.get_data(pval=pval) graphs.append({ "graph": graph, "data_last_updated": graph.data_last_updated(pval=pval), "data": data }) if request.method == "POST": form = WidgetDataForm(request.POST) if form.is_valid(): set_actual_frequency_display_text(w.url(), w.label, form.cleaned_data["actual_frequency_display_text"], pval=pval) else: form = WidgetDataForm(initial={ "actual_frequency_display_text": w.actual_frequency_display(pval=pval), }) return render(request, "widget_data/view_widget.html", { "pval": pval, "widget": w, "stats": stats, "graphs": graphs, "form": form, })
def common_startup(self, request, *args, **kwargs): try: self.stat = get_statistic(kwargs["widget_url"], kwargs["label"], kwargs["stat_url"]) except LoaderException: raise Http404("This Statistic does not exist") if not user_has_edit_permission(request.user, self.stat.tile.widget): raise PermissionDenied("You do not have permission to edit the data for this widget") if not self.stat.editable and not user_has_edit_all_permission(request.user, self.stat.tile.widget): raise PermissionDenied("You do not have permission to edit the data for this widget") if self.stat.tile.widget.parametisation and not kwargs.get("pval_id"): raise Http404("This Widget Definition is parametised") if not self.stat.tile.widget.parametisation: self.pval = None else: try: self.pval = ParametisationValue.objects.get(pk=kwargs.get("pval_id")) except ParametisationValue.DoesNotExist: raise Http404("This parameter value set does not exist") return
def get_stats(self): if user_has_edit_all_permission(self.request.user, self.widget): statistics = Statistic.objects.filter(tile__widget=self.widget) else: statistics = Statistic.objects.filter(editable=True,tile__widget=self.widget) stats = [] for s in statistics: try: data = StatisticData.objects.get(statistic=s, param_value=self.pval) except StatisticData.DoesNotExist: data = None listdata = StatisticListItem.objects.filter(statistic=s, param_value=self.pval) stats.append({ "statistic": s, "data_last_updated": s.data_last_updated(pval=self.pval), "data": data, "listdata": listdata, }) return stats
def common_startup(self, request, *args, **kwargs): try: self.stat = get_statistic(kwargs["widget_url"], kwargs["label"], kwargs["stat_url"]) except LoaderException: raise Http404("This Statistic does not exist") if not user_has_edit_permission(request.user, self.stat.tile.widget): raise PermissionDenied( "You do not have permission to edit the data for this widget") if not self.stat.editable and not user_has_edit_all_permission( request.user, self.stat.tile.widget): raise PermissionDenied( "You do not have permission to edit the data for this widget") if self.stat.tile.widget.parametisation and not kwargs.get("pval_id"): raise Http404("This Widget Definition is parametised") if not self.stat.tile.widget.parametisation: self.pval = None else: try: self.pval = ParametisationValue.objects.get( pk=kwargs.get("pval_id")) except ParametisationValue.DoesNotExist: raise Http404("This parameter value set does not exist") return
def edit_stat(request, widget_url, label, stat_url, pval_id=None): try: s = get_statistic(widget_url, label, stat_url) except LoaderException: return HttpResponseNotFound("This Statistic does not exist") if not user_has_edit_permission(request.user, s.tile.widget): return HttpResponseForbidden("You do not have permission to edit the data for this widget") if not s.editable and not user_has_edit_all_permission(request.user, s.tile.widget): return HttpResponseForbidden("You do not have permission to edit the data for this widget") if s.tile.widget.parametisation and not pval_id: return HttpResponseNotFound("This Widget Definition is parametised") if not s.tile.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_statistic(s) if s.is_data_list(): form_class = forms.formsets.formset_factory(form_class, can_delete=True, extra=4) redirect_out = False if request.method == 'POST': if request.POST.get("submit") or request.POST.get("submit_stay"): form = form_class(request.POST) if form.is_valid(): if s.is_data_list(): clear_statistic_list(s, pval=pval) for subform in form: fd = subform.cleaned_data if fd and not fd.get("DELETE"): add_stat_list_item(s, fd["value"], fd["sort_order"], pval, fd.get("datetime"), fd.get("level"), fd.get("date"), fd.get("label"), fd.get("traffic_light_code"), fd.get("icon_code"), fd.get("trend"), fd.get("url")) if request.POST.get("submit"): redirect_out=True else: form = form_class(initial=s.initial_form_data(pval)) else: fd = form.cleaned_data set_stat_data(s, fd["value"], pval, fd.get("traffic_light_code"), fd.get("icon_code"), fd.get("trend"), fd.get("label")) redirect_out=True elif request.POST.get("cancel"): redirect_out=True elif not s.is_data_list() and request.POST.get("delete"): clear_statistic_data(s) redirect_out=True else: form = form_class(initial=s.initial_form_data(pval)) else: form = form_class(initial=s.initial_form_data(pval)) if redirect_out: if pval: return redirect("view_parametised_widget_data", widget_url=s.tile.widget.family.url, label=s.tile.widget.label, pval_id=pval_id) else: return redirect("view_widget_data", widget_url=s.tile.widget.family.url, label=s.tile.widget.label) return render(request, "widget_data/edit_widget.html", { "pval": pval, "widget": s.tile.widget, "statistic": s, "form": form })