예제 #1
0
    def get(self, request, formid, field_name, format=None):
        # TODO: seems like model is taking care of object-level perms,
        # should we just rely on that
        xform = get_xform(formid, request)

        # check if its the special _submission_time META
        if field_name == common_tags.SUBMISSION_TIME:
            field = common_tags.SUBMISSION_TIME
        else:
            # use specified field to get summary
            dd = xform.data_dictionary()
            fields = filter(
                lambda f: f.name == field_name, [e for e in
                                                 dd.survey_elements])

            if len(fields) == 0:
                raise Http404(
                    "Field %s does not not exist on the form" % field_name)

            field = fields[0]

        data = build_chart_data_for_field(xform, field)
        data.update({
            'xform': xform
        })

        return Response(data, template_name='chart_detail.html')
    def list(self, request, owner=None, formid=None, **kwargs):
        if owner is None and not request.user.is_anonymous():
            owner = request.user.username

        data = []

        if formid:
            xform = get_xform(formid, request)

            field = "_submission_time"
            name = "date_of_submission"
            group = request.QUERY_PARAMS.get("group", None)
            alt_name = request.QUERY_PARAMS.get("name", None)

            if group:
                name = field = group
            if alt_name:
                name = alt_name

            try:
                data = get_form_submissions_grouped_by_field(xform, field, name)
            except ValueError as e:
                raise exceptions.ParseError(detail=e.message)
        else:
            data = self._get_formlist_data_points(request, owner)

        return Response(data)
예제 #3
0
    def list(self, request, owner=None, formid=None, **kwargs):
        if owner is None and not request.user.is_anonymous():
            owner = request.user.username

        data = []

        if formid:
            xform = get_xform(formid, request)

            field = '_submission_time'
            name = 'date_of_submission'
            group = request.QUERY_PARAMS.get('group', None)
            alt_name = request.QUERY_PARAMS.get('name', None)

            if group:
                name = field = group
            if alt_name:
                name = alt_name

            try:
                data = get_form_submissions_grouped_by_field(
                    xform, field, name)
            except ValueError as e:
                raise exceptions.ParseError(detail=e.message)
        else:
            data = self._get_formlist_data_points(request, owner)

        return Response(data)
예제 #4
0
    def labels(self, request, owner, formid, dataid, **kwargs):
        class TagForm(forms.Form):
            tags = TagField()

        owner = owner is None and (
            not request.user.is_anonymous() and request.user.username)

        get_xform(formid, request, owner)
        status = 400
        instance = get_object_or_404(ParsedInstance, instance__pk=int(dataid))

        if request.method == 'POST':
            form = TagForm(request.DATA)

            if form.is_valid():
                tags = form.cleaned_data.get('tags', None)

                if tags:
                    for tag in tags:
                        instance.instance.tags.add(tag)
                    instance.save()
                    status = 201

        label = kwargs.get('label', None)

        if request.method == 'GET' and label:
            data = [
                tag['name'] for tag in
                instance.instance.tags.filter(name=label).values('name')]

        elif request.method == 'DELETE' and label:
            count = instance.instance.tags.count()
            instance.instance.tags.remove(label)

            # Accepted, label does not exist hence nothing removed
            if count == instance.instance.tags.count():
                status = 202

            data = list(instance.instance.tags.names())
        else:
            data = list(instance.instance.tags.names())

        if request.method == 'GET':
            status = 200

        return Response(data, status=status)
예제 #5
0
    def list(self, request, owner=None, formid=None, dataid=None, **kwargs):
        data = None
        xform = None
        query = None
        tags = self.request.QUERY_PARAMS.get('tags', None)
        if owner is None and not request.user.is_anonymous():
            owner = request.user.username
        if not formid and not dataid and not tags:
            data = self._get_formlist_data_points(request, owner)

        if formid:
            xform = get_xform(formid, request)

            query = {}
            query[ParsedInstance.USERFORM_ID] = \
                u'%s_%s' % (xform.user.username, xform.id_string)

        if xform and dataid and dataid == 'labels':
            return Response(list(xform.tags.names()))
        if dataid:
            if query:
                query.update({'_id': int(dataid)})
            else:
                query = {'_id': int(dataid)}
        rquery = request.QUERY_PARAMS.get('query', None)
        if rquery:
            rquery = json.loads(rquery)
            if query:
                query.update(rquery)
            else:
                query = rquery
        if tags:
            query = query if query else {}
            query['_tags'] = {'$all': tags.split(',')}
        if xform:
            data = self._get_form_data(xform, query=query)
        if not xform and not data:
            xforms = get_accessible_forms(owner)
            if not query:
                query = {}
            query[ParsedInstance.USERFORM_ID] = {
                '$in': [
                    u'%s_%s' % (form.user.username, form.id_string)
                    for form in xforms
                ]
            }
            # query['_id'] = {'$in': [form.pk for form in xforms]}
            data = self._get_form_data(xform, query=query)
        if dataid and len(data):
            data = data[0]
        return Response(data)
예제 #6
0
    def list(self, request, owner=None, formid=None, dataid=None, **kwargs):
        data = None
        xform = None
        query = None
        tags = self.request.QUERY_PARAMS.get('tags', None)
        if owner is None and not request.user.is_anonymous():
            owner = request.user.username
        if not formid and not dataid and not tags:
            data = self._get_formlist_data_points(request, owner)

        if formid:
            xform = get_xform(formid, request)

            query = {}
            query[ParsedInstance.USERFORM_ID] = \
                u'%s_%s' % (xform.user.username, xform.id_string)

        if xform and dataid and dataid == 'labels':
            return Response(list(xform.tags.names()))
        if dataid:
            if query:
                query.update({'_id': int(dataid)})
            else:
                query = {'_id': int(dataid)}
        rquery = request.QUERY_PARAMS.get('query', None)
        if rquery:
            rquery = json.loads(rquery)
            if query:
                query.update(rquery)
            else:
                query = rquery
        if tags:
            query = query if query else {}
            query['_tags'] = {'$all': tags.split(',')}
        if xform:
            data = self._get_form_data(xform, query=query)
        if not xform and not data:
            xforms = get_accessible_forms(owner)
            if not query:
                query = {}
            query[ParsedInstance.USERFORM_ID] = {
                '$in': [
                    u'%s_%s' % (form.user.username, form.id_string)
                    for form in xforms]
            }
            # query['_id'] = {'$in': [form.pk for form in xforms]}
            data = self._get_form_data(xform, query=query)
        if dataid and len(data):
            data = data[0]
        return Response(data)
예제 #7
0
    def list(self, request, owner=None, formid=None, dataid=None, **kwargs):
        xform = None
        query = {}
        tags = self.request.QUERY_PARAMS.get('tags', None)
        owner = owner or (
            not request.user.is_anonymous() and request.user.username)
        data = not formid and not dataid and not tags and\
            self._get_formlist_data_points(request, owner)

        if formid:
            xform = get_xform(formid, request)
            query[ParsedInstance.USERFORM_ID] = u'%s_%s' % (
                xform.user.username, xform.id_string)

        if xform and dataid and dataid == 'labels':
            return Response(list(xform.tags.names()))

        if dataid:
            try:
                query.update({'_id': int(dataid)})
            except ValueError:
                return HttpResponseBadRequest(_("Invalid _id"))

        rquery = request.QUERY_PARAMS.get('query', None)
        if rquery:
            rquery = json.loads(rquery)
            query.update(rquery)

        if tags:
            query['_tags'] = {'$all': tags.split(',')}

        if xform:
            data = self._get_form_data(xform, query=query)

        if not xform and not data:
            xforms = get_accessible_forms(owner)
            query[ParsedInstance.USERFORM_ID] = {
                '$in': [
                    u'%s_%s' % (form.user.username, form.id_string)
                    for form in xforms]
            }
            data = self._get_form_data(xform, query=query)

        if dataid and len(data):
            data = data[0]

        return Response(data)
예제 #8
0
    def list(self, request, owner=None, formid=None, **kwargs):
        if owner is None and not request.user.is_anonymous():
            owner = request.user.username

        data = []

        if formid:
            xform = get_xform(formid, request)
            try:
                method = request.QUERY_PARAMS.get('method', None)
                field = request.QUERY_PARAMS.get('field', None)
                if method is None:
                    data = get_all_stats(xform, field)
                else:
                    data = STATS_FUNCTIONS[method.lower()](xform, field)
            except ValueError as e:
                raise exceptions.ParseError(detail=e.message)
        else:
            data = self._get_formlist_data_points(request, owner)

        return Response(data)
예제 #9
0
    def list(self, request, owner=None, formid=None, **kwargs):
        if owner is None and not request.user.is_anonymous():
            owner = request.user.username

        data = []

        if formid:
            xform = get_xform(formid, request)
            try:
                method = request.QUERY_PARAMS.get('method', None)
                field = request.QUERY_PARAMS.get('field', None)
                if method is None:
                    data = get_all_stats(xform, field)
                else:
                    data = STATS_FUNCTIONS[method.lower()](xform, field)
            except ValueError as e:
                raise exceptions.ParseError(detail=e.message)
        else:
            data = self._get_formlist_data_points(request, owner)

        return Response(data)
예제 #10
0
    def list(self, request, owner=None, formid=None, **kwargs):
        owner = owner is None and (
            not request.user.is_anonymous() and request.user.username)

        data = []

        if formid:
            xform = get_xform(formid, request, owner)
            try:
                method = request.QUERY_PARAMS.get('method', None)
                field = request.QUERY_PARAMS.get('field', None)

                # check that field is in XForm
                if field and field not in xform.data_dictionary().get_keys():
                    return HttpResponseBadRequest(_("Field not in XForm."))

                data = get_all_stats(xform, field) if method is None else\
                    STATS_FUNCTIONS[method.lower()](xform, field)
            except ValueError as e:
                raise exceptions.ParseError(detail=e.message)
        else:
            data = self._get_formlist_data_points(request, owner)

        return Response(data)
예제 #11
0
 def pre_save(self, obj):
     # throws PermissionDenied if request.user has no permission to xform
     get_xform(obj.instance.xform.pk, self.request)
예제 #12
0
 def pre_save(self, obj):
     # throws PermissionDenied if request.user has no permission to xform
     get_xform(obj.instance.xform.pk, self.request)