Example #1
0
    def charts(self, request, *args, **kwargs):
        dataview = self.get_object()
        xform = dataview.xform
        serializer = self.get_serializer(dataview)

        field_name = request.query_params.get('field_name')
        field_xpath = request.query_params.get('field_xpath')
        fmt = kwargs.get('format', request.accepted_renderer.format)
        group_by = request.query_params.get('group_by')

        if field_name:
            field = get_field_from_field_name(field_name, xform)
            field_xpath = field_name if isinstance(field, basestring) \
                else field.get_abbreviated_xpath()

        if field_xpath and field_xpath not in dataview.columns and \
                field_xpath not in [common_tags.SUBMISSION_TIME,
                                    common_tags.SUBMITTED_BY,
                                    common_tags.DURATION]:
            raise Http404("Field %s does not not exist on the dataview" %
                          field_name)

        if field_name or field_xpath:
            data = get_chart_data_for_field(field_name,
                                            xform,
                                            fmt,
                                            group_by,
                                            field_xpath,
                                            data_view=dataview)

            return Response(data, template_name='chart_detail.html')

        if fmt != 'json' and field_name is None:
            raise ParseError("Not supported")

        data = serializer.data
        data["fields"] = {}
        for field in xform.survey_elements:
            field_xpath = field.get_abbreviated_xpath()
            if field_xpath in dataview.columns:
                url = reverse('dataviews-charts',
                              kwargs={'pk': dataview.pk},
                              request=request,
                              format=fmt)
                field_url = get_form_field_chart_url(url, field.name)
                data["fields"][field.name] = field_url

        return Response(data)
Example #2
0
    def charts(self, request, *args, **kwargs):
        dataview = self.get_object()
        xform = dataview.xform
        serializer = self.get_serializer(dataview)

        field_name = request.query_params.get('field_name')
        field_xpath = request.query_params.get('field_xpath')
        fmt = kwargs.get('format', request.accepted_renderer.format)
        group_by = request.query_params.get('group_by')

        if field_name:
            field = get_field_from_field_name(field_name, xform)
            field_xpath = field_name if isinstance(field, basestring) \
                else field.get_abbreviated_xpath()

        if field_xpath and field_xpath not in dataview.columns and \
                field_xpath not in [common_tags.SUBMISSION_TIME,
                                    common_tags.SUBMITTED_BY,
                                    common_tags.DURATION]:
            raise Http404(
                "Field %s does not not exist on the dataview" % field_name)

        if field_name or field_xpath:
            data = get_chart_data_for_field(
                field_name, xform, fmt, group_by, field_xpath,
                data_view=dataview
            )

            return Response(data, template_name='chart_detail.html')

        if fmt != 'json' and field_name is None:
            raise ParseError("Not supported")

        data = serializer.data
        data["fields"] = {}
        for field in xform.survey_elements:
            field_xpath = field.get_abbreviated_xpath()
            if field_xpath in dataview.columns:
                url = reverse('dataviews-charts', kwargs={'pk': dataview.pk},
                              request=request, format=fmt)
                field_url = get_form_field_chart_url(url, field.name)
                data["fields"][field.name] = field_url

        return Response(data)