Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
7 changes: 6 additions & 1 deletion syncano/models/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,8 +882,12 @@ 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',
'startswith', 'endswith',
'contains', 'istartswith',
'iendswith', 'icontains',
'ieq', 'near',
]

def __init__(self):
Expand Down Expand Up @@ -990,6 +994,7 @@ def _get_lookup_attributes(self, field_name):
return model_name, field_name, lookup

def _validate_lookup(self, model, model_name, field_name, lookup, field):

if not model_name and field_name not in model._meta.field_names:
allowed = ', '.join(model._meta.field_names)
raise SyncanoValueError('Invalid field name "{0}" allowed are {1}.'.format(field_name, 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': 'some'})

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'})