Skip to content
This repository has been archived by the owner on Mar 14, 2019. It is now read-only.

Commit

Permalink
First steps in checking user-supplied filters
Browse files Browse the repository at this point in the history
Checks for `=` and `__` in the filter
Checks that the relationship is valid
  • Loading branch information
Timothée Poisot committed Apr 28, 2014
1 parent 7af2513 commit 6382843
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/user/filtering.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ relation description
============== ========================================================
``startswith`` All fields starting by the target
``endswith`` All fields ending by the target
``exacts`` Exact matching
``exact`` Exact matching
``contains`` Fields that contain the target
``range`` Fields with values in the range
``gt`` Field with values greater than the target
Expand Down
25 changes: 23 additions & 2 deletions pymangal/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,33 @@ def check_filters(filters):
:param filters: A string of filters
:returns: Nothing, but can modify ``filters`` in place
:returns: Nothing, but can modify ``filters`` in place, and raises ``ValueError``s if the filters are badly formatted.
At the moment, this function do not parse the filters to make sure that they are valid.
This functions conducts minimal parsing, to make sure that the relationship exists, and that the filter is generally well formed.
The ``filters`` string is modified in place if it contains space.
"""
if not isinstance(filters, str):
raise TypeError("filters must be a string")
# We replace all spaces with %20
filters.replace(' ', '%20')
# These steps will check that the filters are correct
REL = ['sartswith', 'endswith', 'exact', 'contains', 'range', 'gt', 'lt', 'gte', 'lte', 'in']
filters = filters.split('&')
for fi in filters:
if not '=' in fi:
raise ValueError("Filter "+fi+" is invalid (no =)")
if not '__' in fi:
raise ValueError("Filter "+fi+" is invalid (no __)")
splitted_filter = fi.split('=')
match = splitted_filter[0]
target = splitted_filter[1]
request = match.split('__')
relationship = request[len(request)-1]
if not relationship in REL:
raise ValueError("Filter "+fi+" is invalid ("+ relationship +" is not a valid relationship)")
if len(filters) == 1 :
return filters
else :
return '&'.join(filters)

9 changes: 9 additions & 0 deletions test_pymangal.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ def test_filters_is_str(self):
def test_filters_space_replaced(self):
assert isinstance(self.mg.List('taxa', 'name__contains=s b'), dict)

def test_filters_valid_relationship(self):
self.assertRaises(ValueError, lambda : self.mg.List('taxa', 'name__islike=Lame'))

def test_filters_valid_structure(self):
self.assertRaises(ValueError, lambda : self.mg.List('taxa', 'name__contains=a&name_endswith=s'))

def test_filters_has_equal_sign(self):
self.assertRaises(ValueError, lambda : self.mg.List('taxa', 'name__contains_i'))

def test_resource_available(self):
self.assertRaises(ValueError, lambda : self.mg.List('TAXA'))

Expand Down

0 comments on commit 6382843

Please sign in to comment.