Exemple #1
0
 def test_xform_delete_cascades_mongo_instances(self):
     initial_mongo_count = ParsedInstance.query_mongo(
         self.user.username,
         self.xform.id_string,
         '{}',
         '[]',
         '{}',
         count=True)[0]["count"]
     # submit instance
     for i in range(len(self.surveys)):
         self._submit_transport_instance(i)
     # check mongo record exists
     mongo_count = ParsedInstance.query_mongo(self.user.username,
                                              self.xform.id_string,
                                              '{}',
                                              '[]',
                                              '{}',
                                              count=True)[0]["count"]
     self.assertEqual(mongo_count, initial_mongo_count + len(self.surveys))
     # delete form
     xform_delete_url = reverse(delete_xform,
                                kwargs={
                                    'username': self.user.username,
                                    'id_string': self.xform.id_string
                                })
     self.client.post(xform_delete_url)
     mongo_count = ParsedInstance.query_mongo(self.user.username,
                                              self.xform.id_string,
                                              '{}',
                                              '[]',
                                              '{}',
                                              count=True)[0]["count"]
     self.assertEqual(mongo_count, initial_mongo_count)
Exemple #2
0
def api(request, username=None, id_string=None):
    '''
    Returns all results as JSON.  If a parameter string is passed,
    it takes the 'query' parameter, converts this string to a dictionary, an
    that is then used as a MongoDB query string.

    NOTE: only a specific set of operators are allow, currently $or and $and.
    Please send a request if you'd like another operator to be enabled.

    NOTE: Your query must be valid JSON, double check it here,
    http://json.parser.online.fr/

    E.g. api?query='{"last_name": "Smith"}'
    '''
    xform, owner = check_and_set_user_and_form(username, id_string, request)
    if not xform:
        return HttpResponseForbidden('Not shared.')
    try:
        args = {"username": username, "id_string": id_string, "query": request.GET.get('query'),
                "fields": request.GET.get('fields'), "sort": request.GET.get('sort')}
        if 'start' in request.GET:
            args["start"] = int(request.GET.get('start'))
        if 'limit' in request.GET:
            args["limit"] = int(request.GET.get('limit'))
        if 'count' in request.GET:
            args["count"] = True if int(request.GET.get('count')) > 0 else False
        cursor = ParsedInstance.query_mongo(**args)
    except ValueError, e:
        return HttpResponseBadRequest(e.message)
Exemple #3
0
 def _get_form_data(self, xform, **kwargs):
     margs = {
         'username': xform.user.username,
         'id_string': xform.id_string,
         'query': kwargs.get('query', None),
         'fields': kwargs.get('fields', None),
         'sort': kwargs.get('sort', None)
     }
     # TODO: Possibly add "url" field to all data records
     cursor = ParsedInstance.query_mongo(**margs)
     records = list(record for record in cursor)
     return records
Exemple #4
0
 def _get_form_data(self, xform, **kwargs):
     query = kwargs.get('query', {})
     query = query if query is not None else {}
     if xform:
         query[ParsedInstance.USERFORM_ID] =\
             u'%s_%s' % (xform.user.username, xform.id_string)
     query = json.dumps(query) if isinstance(query, dict) else query
     margs = {
         'query': query,
         'fields': kwargs.get('fields', None),
         'sort': kwargs.get('sort', None)
     }
     cursor = ParsedInstance.query_mongo_minimal(**margs)
     records = list(record for record in cursor)
     return records