def query_mongo(cls, username, query=None, fields=None, sort=None, start=0, limit=DEFAULT_LIMIT, count=False): query = dict_for_mongo(query) if query else {} query[cls.ACCOUNT] = username # hack: check for the created_on key in query and turn its values into dates if type(query) == dict and query.has_key(cls.CREATED_ON): if type(query[cls.CREATED_ON]) is dict: for op, val in query[cls.CREATED_ON].iteritems(): try: query[cls.CREATED_ON][op] = datetime.strptime(val, DATETIME_FORMAT) except ValueError, e: pass elif isinstance(query[cls.CREATED_ON], basestring): val = query[cls.CREATED_ON] try: created_on = datetime.strptime(val, DATETIME_FORMAT) except ValueError, e: pass else: # create start and end times for the entire day start_time = created_on.replace(hour=0, minute=0, second=0, microsecond=0) end_time = start_time + timedelta(days=1) query[cls.CREATED_ON] = {"$gte": start_time, "$lte": end_time}
def test_api(self): # query string response = self.client.get(self.api_url, {}) self.assertEqual(response.status_code, 200) d = dict_for_mongo(self.xform.surveys.all()[0].parsed_instance.to_dict()) find_d = simplejson.loads(response.content)[0] self.assertEqual(sorted(find_d, key=find_d.get), sorted(d, key=d.get))
def test_api(self): # query string response = self.client.get(self.api_url, {}) self.assertEqual(response.status_code, 200) d = dict_for_mongo( self.xform.surveys.all()[0].parsed_instance.to_dict()) find_d = simplejson.loads(response.content)[0] self.assertEqual(sorted(find_d, key=find_d.get), sorted(d, key=d.get))
def test_api_with_query_start_limit(self): # query string json = '{"transport/available_transportation_types_to_referral_facility":"none"}' data = {"query": json, "start": 0, "limit": 10} response = self.client.get(self.api_url, data) self.assertEqual(response.status_code, 200) d = dict_for_mongo(self.xform.surveys.all()[0].parsed_instance.to_dict()) find_d = simplejson.loads(response.content)[0] self.assertEqual(sorted(find_d, key=find_d.get), sorted(d, key=d.get))
def query_mongo(username, id_string, query=None, hide_deleted=True): query = json.loads(query, object_hook=json_util.object_hook)\ if query else {} query = dict_for_mongo(query) query[USERFORM_ID] = u'{0}_{1}'.format(username, id_string) if hide_deleted: #display only active elements # join existing query with deleted_at_query on an $and query = {"$and": [query, {"_deleted_at": None}]} return xform_instances.find(query)
def test_api_with_query_start_limit(self): # query string json = '{"transport/available_transportation_types_to_referral_facility":"none"}' data = {'query': json, 'start': 0, 'limit': 10} response = self.client.get(self.api_url, data) self.assertEqual(response.status_code, 200) d = dict_for_mongo( self.xform.surveys.all()[0].parsed_instance.to_dict()) find_d = simplejson.loads(response.content)[0] self.assertEqual(sorted(find_d, key=find_d.get), sorted(d, key=d.get))
def test_api_jsonp(self): # query string callback = "jsonpCallback" response = self.client.get(self.api_url, {"callback": callback}) self.assertEqual(response.status_code, 200) self.assertEqual(response.content.startswith(callback + "("), True) self.assertEqual(response.content.endswith(")"), True) start = callback.__len__() + 1 end = response.content.__len__() - 1 content = response.content[start:end] d = dict_for_mongo(self.xform.surveys.all()[0].parsed_instance.to_dict()) find_d = simplejson.loads(content)[0] self.assertEqual(sorted(find_d, key=find_d.get), sorted(d, key=d.get))
def test_api_jsonp(self): # query string callback = 'jsonpCallback' response = self.client.get(self.api_url, {'callback': callback}) self.assertEqual(response.status_code, 200) self.assertEqual(response.content.startswith(callback + '('), True) self.assertEqual(response.content.endswith(')'), True) start = callback.__len__() + 1 end = response.content.__len__() - 1 content = response.content[start:end] d = dict_for_mongo( self.xform.surveys.all()[0].parsed_instance.to_dict()) find_d = simplejson.loads(content)[0] self.assertEqual(sorted(find_d, key=find_d.get), sorted(d, key=d.get))
def query_mongo(cls, username, query=None, fields=None, sort=None, start=0, limit=DEFAULT_LIMIT, count=False): query = dict_for_mongo(query) if query else {} query[cls.ACCOUNT] = username # hack: check for the created_on key in query and turn its values into dates if type(query) == dict and query.has_key(cls.CREATED_ON): if type(query[cls.CREATED_ON]) is dict: for op, val in query[cls.CREATED_ON].iteritems(): try: query[cls.CREATED_ON][op] = datetime.strptime( val, DATETIME_FORMAT) except ValueError, e: pass elif isinstance(query[cls.CREATED_ON], basestring): val = query[cls.CREATED_ON] try: created_on = datetime.strptime(val, DATETIME_FORMAT) except ValueError, e: pass else: # create start and end times for the entire day start_time = created_on.replace(hour=0, minute=0, second=0, microsecond=0) end_time = start_time + timedelta(days=1) query[cls.CREATED_ON] = { "$gte": start_time, "$lte": end_time }