Skip to content

Commit

Permalink
Merge pull request #66 from AltSchool/fix/handlespecialcharacters
Browse files Browse the repository at this point in the history
Unicode params need to work #ALTOS-4519
  • Loading branch information
ryochiji committed Jan 13, 2016
2 parents ce03964 + 3b59d1e commit 3bf26ae
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
2 changes: 1 addition & 1 deletion dynamic_rest/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ def get_valid_fields(self, queryset, view):
for field_name, field in serializer_class().fields.items()
if not getattr(
field, 'write_only', False
) and not field.source == '*'
) and not field.source == '*'
]
else:
serializer_class = getattr(view, 'serializer_class')
Expand Down
6 changes: 5 additions & 1 deletion dynamic_rest/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ class QueryParams(QueryDict):
"""

def __init__(self, query_params, *args, **kwargs):
query_string = getattr(query_params, 'urlencode', lambda: '')()
if hasattr(query_params, 'urlencode'):
query_string = query_params.urlencode()
else:
assert isinstance(query_params, str)
query_string = query_params
kwargs['mutable'] = True
super(QueryParams, self).__init__(query_string, *args, **kwargs)

Expand Down
35 changes: 31 additions & 4 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,33 @@ def testFilterBasic(self):
},
json.loads(response.content))

def testFilterForNonexistingMatch(self):
with self.assertNumQueries(1):
response = self.client.get('/users/?filter{name}[]=foo')
self.assertEquals(200, response.status_code)
self.assertEquals({'users': []}, json.loads(response.content))

def testFilterWithUnicodeNonexistingMatch(self):
with self.assertNumQueries(1):
response = self.client.get(u'/users/?filter{name}[]=%E2%98%82')
self.assertEquals(200, response.status_code)
self.assertEquals({'users': []}, json.loads(response.content))
with self.assertNumQueries(1):
response = self.client.get(u'/users/?filter{name}[]=☂')
self.assertEquals(200, response.status_code)
self.assertEquals({'users': []}, json.loads(response.content))

def testUnicodeFilter(self):
User.objects.create(name=u'☂', last_name='Undermy')
with self.assertNumQueries(1):
response = self.client.get(u'/users/?filter{name}[]=%E2%98%82')
self.assertEquals(200, response.status_code)
self.assertEquals(1, len(json.loads(response.content)['users']))
with self.assertNumQueries(1):
response = self.client.get(u'/users/?filter{name}[]=☂')
self.assertEquals(200, response.status_code)
self.assertEquals(1, len(json.loads(response.content)['users']))

def testFilterIn(self):
url = '/users/?filter{name.in}=1&filter{name.in}=2'
with self.assertNumQueries(1):
Expand Down Expand Up @@ -395,7 +422,7 @@ def testPostResponse(self):
'last_name': 'last',
'location': 1,
'display_name': 'test last' # Read only, should be ignored.
}
}
response = self.client.post(
'/users/', json.dumps(data), content_type='application/json')
self.assertEquals(201, response.status_code)
Expand All @@ -413,14 +440,14 @@ def testPostResponse(self):
"thumbnail_url": None,
"number_of_cats": 1,
"profile": None
}
}
})

def testUpdate(self):
group = Group.objects.create(name='test group')
data = {
'name': 'updated'
}
}
response = self.client.put(
'/groups/%s/' % group.pk,
json.dumps(data),
Expand Down Expand Up @@ -455,7 +482,7 @@ def testDefaultQuerysetWithFilter(self):
'/groups/?filter{id}=1&include[]=loc1users'
'&filter{loc1users|id.in}=3'
'&filter{loc1users|id.in}=1'
)
)
response = self.client.get(url)
content = json.loads(response.content)
self.assertEqual(200, response.status_code)
Expand Down

0 comments on commit 3bf26ae

Please sign in to comment.