Ejemplo n.º 1
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)
Ejemplo n.º 2
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)
Ejemplo n.º 3
0
 def _get_formlist_data_points(self, request, owner=None):
     xforms = get_accessible_forms(owner)
     # filter by tags if available.
     tags = self.request.QUERY_PARAMS.get('tags', None)
     if tags and isinstance(tags, basestring):
         tags = tags.split(',')
         xforms = xforms.filter(tags__name__in=tags).distinct()
     rs = {}
     for xform in xforms.distinct():
         point = {u"%s" % xform.id_string:
                  reverse("stats-list", kwargs={
                          "formid": xform.pk,
                          "owner": xform.user.username},
                          request=request)}
         rs.update(point)
     return rs
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
 def _get_formlist_data_points(self, request, owner=None):
     xforms = get_accessible_forms(owner)
     # filter by tags if available.
     tags = self.request.QUERY_PARAMS.get('tags', None)
     if tags and isinstance(tags, basestring):
         tags = tags.split(',')
         xforms = xforms.filter(tags__name__in=tags).distinct()
     rs = {}
     for xform in xforms.distinct():
         point = {
             u"%s" % xform.id_string:
             reverse("submissionstats-list",
                     kwargs={
                         "formid": xform.pk,
                         "owner": xform.user.username
                     },
                     request=request)
         }
         rs.update(point)
     return rs