Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[LIB-743] Add string field filtering
  • Loading branch information
opalczynski committed May 25, 2016
commit 4f20f4769a81cb197d8cfd16ea4d477772e1bfbb
18 changes: 16 additions & 2 deletions syncano/models/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Field(object):
allow_increment = False

creation_counter = 0
field_lookups = []

def __init__(self, name=None, **kwargs):
self.name = name
Expand Down Expand Up @@ -214,6 +215,16 @@ class EndpointField(WritableField):

class StringField(WritableField):

field_lookups = [
'startswith',
'endswith',
'contains',
'istartswith',
'iendswith',
'icontains',
'ieq',
]

def to_python(self, value):
value = super(StringField, self).to_python(value)

Expand Down Expand Up @@ -695,6 +706,8 @@ def to_native(self, value):

class GeoPointField(Field):

field_lookups = ['near', 'exists']

def validate(self, value, model_instance):
super(GeoPointField, self).validate(value, model_instance)

Expand Down Expand Up @@ -735,7 +748,7 @@ def to_query(self, value, lookup_type, **kwargs):
"""
super(GeoPointField, self).to_query(value, lookup_type, **kwargs)

if lookup_type not in ['near', 'exists']:
if lookup_type not in self.field_lookups:
raise SyncanoValueError('Lookup {} not supported for geopoint field'.format(lookup_type))

if lookup_type in ['exists']:
Expand Down Expand Up @@ -804,6 +817,7 @@ def _process_value(cls, value):

class RelationField(RelationValidatorMixin, WritableField):
query_allowed = True
field_lookups = ['contains', 'is']

def __call__(self, instance, field_name):
return RelationManager(instance=instance, field_name=field_name)
Expand All @@ -828,7 +842,7 @@ def to_query(self, value, lookup_type, related_field_name=None, related_field_lo
if not self.query_allowed:
raise self.ValidationError('Query on this field is not supported.')

if lookup_type not in ['contains', 'is']:
if lookup_type not in self.field_lookups:
raise SyncanoValueError('Lookup {} not supported for relation field.'.format(lookup_type))

query_dict = {}
Expand Down
6 changes: 4 additions & 2 deletions syncano/models/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ class for :class:`~syncano.models.base.Object` model.
LOOKUP_SEPARATOR = '__'
ALLOWED_LOOKUPS = [
'gt', 'gte', 'lt', 'lte',
'eq', 'neq', 'exists', 'in', 'startswith',
'eq', 'neq', 'exists', 'in',
'near', 'is', 'contains',
]

Expand Down Expand Up @@ -994,7 +994,9 @@ def _validate_lookup(self, model, model_name, field_name, lookup, field):
allowed = ', '.join(model._meta.field_names)
raise SyncanoValueError('Invalid field name "{0}" allowed are {1}.'.format(field_name, allowed))

if lookup not in self.ALLOWED_LOOKUPS:
allowed_lookups = set(self.ALLOWED_LOOKUPS + field.field_lookups)

if lookup not in allowed_lookups:
allowed = ', '.join(self.ALLOWED_LOOKUPS)
raise SyncanoValueError('Invalid lookup type "{0}" allowed are {1}.'.format(lookup, allowed))

Expand Down
35 changes: 35 additions & 0 deletions tests/integration_test_string_filtering.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
from syncano.models import Object
from tests.integration_test import InstanceMixin, IntegrationTest


class StringFilteringTest(InstanceMixin, IntegrationTest):

@classmethod
def setUpClass(cls):
super(StringFilteringTest, cls).setUpClass()
cls.klass = cls.instance.classes.create(name='class_a',
schema=[{'name': 'title', 'type': 'string', 'filter_index': True}])
cls.object = cls.klass.objects.create(title='Some great title')

def _test_filter(self, filter):
filtered_obj = Object.please.list(class_name='class_a').filter(
**filter
).first()

self.assertTrue(filtered_obj.id)

def test_starstwith(self):
self._test_filter({'title__startswith': 'Some'})
self._test_filter({'title__istartswith': 'omes'})

def test_endswith(self):
self._test_filter({'title__endswith': 'tle'})
self._test_filter({'title__iendswith': 'TLE'})

def test_contains(self):
self._test_filter({'title__contains': 'gre'})
self._test_filter({'title__icontains': 'gRe'})

def test_eq(self):
self._test_filter({'title__ieq': 'some gREAt title'})