Skip to content
This repository was archived by the owner on Mar 28, 2019. It is now read-only.
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
1 change: 1 addition & 0 deletions cliquet/initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ def on_new_response(event):

class EventActionFilter(object):
def __init__(self, actions, config):
actions = ACTIONS.from_string_list(actions)
self.actions = [action.value for action in actions]

def phash(self):
Expand Down
25 changes: 10 additions & 15 deletions cliquet/resource/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,7 @@ def put(self):
existing = self._get_record_or_404(self.record_id)
except HTTPNotFound:
# Look if this record used to exist (for preconditions check).
filter_by_id = Filter(id_field, self.record_id,
COMPARISON.EQ.value)
filter_by_id = Filter(id_field, self.record_id, COMPARISON.EQ)
tombstones, _ = self.model.get_records(filters=[filter_by_id],
include_deleted=True)
if len(tombstones) > 0:
Expand Down Expand Up @@ -883,15 +882,15 @@ def _extract_filters(self, queryparams=None):
raise_invalid(self.request, **error_details)

if param == '_since':
operator = COMPARISON.GT.value
operator = COMPARISON.GT
else:
if param == '_to':
message = ('_to is now deprecated, '
'you should use _before instead')
url = ('http://cliquet.rtfd.org/en/2.4.0/api/resource'
'.html#list-of-available-url-parameters')
send_alert(self.request, message, url)
operator = COMPARISON.LT.value
operator = COMPARISON.LT
filters.append(
Filter(self.model.modified_field, value, operator)
)
Expand All @@ -900,9 +899,9 @@ def _extract_filters(self, queryparams=None):
m = re.match(r'^(min|max|not|lt|gt|in|exclude)_(\w+)$', param)
if m:
keyword, field = m.groups()
operator = getattr(COMPARISON, keyword.upper()).value
operator = getattr(COMPARISON, keyword.upper())
else:
operator, field = COMPARISON.EQ.value, param
operator, field = COMPARISON.EQ, param

if not self.is_known_field(field):
error_details = {
Expand All @@ -912,7 +911,7 @@ def _extract_filters(self, queryparams=None):
raise_invalid(self.request, **error_details)

value = native_value(paramvalue)
if operator in (COMPARISON.IN.value, COMPARISON.EXCLUDE.value):
if operator in (COMPARISON.IN, COMPARISON.EXCLUDE):
value = set([native_value(v) for v in paramvalue.split(',')])

filters.append(Filter(field, value, operator))
Expand Down Expand Up @@ -958,17 +957,14 @@ def _build_pagination_rules(self, sorting, last_record, rules=None):
next_sorting = sorting[:-1]

for field, _ in next_sorting:
rule.append(Filter(field, last_record.get(field),
COMPARISON.EQ.value))
rule.append(Filter(field, last_record.get(field), COMPARISON.EQ))

field, direction = sorting[-1]

if direction == -1:
rule.append(Filter(field, last_record.get(field),
COMPARISON.LT.value))
rule.append(Filter(field, last_record.get(field), COMPARISON.LT))
else:
rule.append(Filter(field, last_record.get(field),
COMPARISON.GT.value))
rule.append(Filter(field, last_record.get(field), COMPARISON.GT))

rules.append(rule)

Expand Down Expand Up @@ -1081,8 +1077,7 @@ def _extract_filters(self, queryparams=None):

ids = self.context.shared_ids
if ids:
filter_by_id = Filter(self.model.id_field, ids,
COMPARISON.IN.value)
filter_by_id = Filter(self.model.id_field, ids, COMPARISON.IN)
filters.insert(0, filter_by_id)

return filters
Expand Down
23 changes: 11 additions & 12 deletions cliquet/storage/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,22 @@ def apply_filters(self, records, filters):
"""Filter the specified records, using basic iteration.
"""
operators = {
COMPARISON.LT.value: operator.lt,
COMPARISON.MAX.value: operator.le,
COMPARISON.EQ.value: operator.eq,
COMPARISON.NOT.value: operator.ne,
COMPARISON.MIN.value: operator.ge,
COMPARISON.GT.value: operator.gt,
COMPARISON.IN.value: operator.contains,
COMPARISON.EXCLUDE.value: lambda x, y: not operator.contains(x, y),
COMPARISON.LT: operator.lt,
COMPARISON.MAX: operator.le,
COMPARISON.EQ: operator.eq,
COMPARISON.NOT: operator.ne,
COMPARISON.MIN: operator.ge,
COMPARISON.GT: operator.gt,
COMPARISON.IN: operator.contains,
COMPARISON.EXCLUDE: lambda x, y: not operator.contains(x, y),
}

for record in records:
matches = True
for f in filters:
left = record.get(f.field)
right = f.value
if f.operator in (COMPARISON.IN.value,
COMPARISON.EXCLUDE.value):
if f.operator in (COMPARISON.IN, COMPARISON.EXCLUDE):
right = left
left = f.value
matches = matches and operators[f.operator](left, right)
Expand Down Expand Up @@ -331,11 +330,11 @@ def get_unicity_rules(collection_id, parent_id, record, unique_fields,
if value is None:
continue

filters = [Filter(field, value, COMPARISON.EQ.value)]
filters = [Filter(field, value, COMPARISON.EQ)]

if not for_creation:
object_id = record[id_field]
exclude = Filter(id_field, object_id, COMPARISON.NOT.value)
exclude = Filter(id_field, object_id, COMPARISON.NOT)
filters.append(exclude)

rules.append(filters)
Expand Down
18 changes: 9 additions & 9 deletions cliquet/storage/postgresql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,10 +566,10 @@ def _format_conditions(self, filters, id_field, modified_field,
:rtype: tuple
"""
operators = {
COMPARISON.EQ.value: '=',
COMPARISON.NOT.value: '<>',
COMPARISON.IN.value: 'IN',
COMPARISON.EXCLUDE.value: 'NOT IN',
COMPARISON.EQ: '=',
COMPARISON.NOT: '<>',
COMPARISON.IN: 'IN',
COMPARISON.EXCLUDE: 'NOT IN',
}

conditions = []
Expand All @@ -589,8 +589,7 @@ def _format_conditions(self, filters, id_field, modified_field,
# If field is missing, we default to ''.
sql_field = "coalesce(data->>:%s, '')" % field_holder

if filtr.operator not in (COMPARISON.IN.value,
COMPARISON.EXCLUDE.value):
if filtr.operator not in (COMPARISON.IN, COMPARISON.EXCLUDE):
# For the IN operator, let psycopg escape the values list.
# Otherwise JSON-ify the native value (e.g. True -> 'true')
if not isinstance(filtr.value, six.string_types):
Expand All @@ -602,7 +601,8 @@ def _format_conditions(self, filters, id_field, modified_field,
value_holder = '%s_value_%s' % (prefix, i)
holders[value_holder] = value

sql_operator = operators.setdefault(filtr.operator, filtr.operator)
sql_operator = operators.setdefault(filtr.operator,
filtr.operator.value)
cond = "%s %s :%s" % (sql_field, sql_operator, value_holder)
conditions.append(cond)

Expand Down Expand Up @@ -704,7 +704,7 @@ def _check_unicity(self, conn, collection_id, parent_id, record,
if value is None:
continue
sql, holders = self._format_conditions(
[Filter(field, value, COMPARISON.EQ.value)],
[Filter(field, value, COMPARISON.EQ)],
id_field,
modified_field,
prefix=field)
Expand All @@ -721,7 +721,7 @@ def _check_unicity(self, conn, collection_id, parent_id, record,
if not for_creation:
object_id = record[id_field]
sql, holders = self._format_conditions(
[Filter(id_field, object_id, COMPARISON.NOT.value)],
[Filter(id_field, object_id, COMPARISON.NOT)],
id_field,
modified_field)
safeholders['condition_record'] = sql
Expand Down
29 changes: 14 additions & 15 deletions cliquet/tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ def test_get_all_handle_sorting_on_id(self):
def test_get_all_can_filter_with_list_of_values(self):
for l in ['a', 'b', 'c']:
self.create_record({'code': l})
filters = [Filter('code', ['a', 'b'], utils.COMPARISON.IN.value)]
filters = [Filter('code', ['a', 'b'], utils.COMPARISON.IN)]
records, _ = self.storage.get_all(filters=filters,
**self.storage_kw)
self.assertEqual(len(records), 2)
Expand All @@ -429,15 +429,15 @@ def test_get_all_can_filter_with_list_of_values_on_id(self):
record1 = self.create_record({'code': 'a'})
record2 = self.create_record({'code': 'b'})
filters = [Filter('id', [record1['id'], record2['id']],
utils.COMPARISON.IN.value)]
utils.COMPARISON.IN)]
records, _ = self.storage.get_all(filters=filters,
**self.storage_kw)
self.assertEqual(len(records), 2)

def test_get_all_can_filter_with_list_of_excluded_values(self):
for l in ['a', 'b', 'c']:
self.create_record({'code': l})
filters = [Filter('code', ('a', 'b'), utils.COMPARISON.EXCLUDE.value)]
filters = [Filter('code', ('a', 'b'), utils.COMPARISON.EXCLUDE)]
records, _ = self.storage.get_all(filters=filters,
**self.storage_kw)
self.assertEqual(len(records), 1)
Expand All @@ -451,7 +451,7 @@ def test_get_all_handle_a_pagination_rules(self):
records, total_records = self.storage.get_all(
limit=5,
pagination_rules=[
[Filter('number', 1, utils.COMPARISON.GT.value)]
[Filter('number', 1, utils.COMPARISON.GT)]
], **self.storage_kw)
self.assertEqual(total_records, 10)
self.assertEqual(len(records), 3)
Expand All @@ -464,8 +464,8 @@ def test_get_all_handle_all_pagination_rules(self):

records, total_records = self.storage.get_all(
limit=5, pagination_rules=[
[Filter('number', 1, utils.COMPARISON.GT.value)],
[Filter('id', last_record['id'], utils.COMPARISON.EQ.value)],
[Filter('number', 1, utils.COMPARISON.GT)],
[Filter('id', last_record['id'], utils.COMPARISON.EQ)],
], **self.storage_kw)
self.assertEqual(total_records, 10)
self.assertEqual(len(records), 4)
Expand Down Expand Up @@ -642,7 +642,7 @@ def _get_last_modified_filters(self):
start = self.storage.collection_timestamp(**self.storage_kw)
time.sleep(0.1)
return [
Filter(self.modified_field, start, utils.COMPARISON.GT.value)
Filter(self.modified_field, start, utils.COMPARISON.GT)
]

def create_and_delete_record(self, record=None):
Expand Down Expand Up @@ -773,7 +773,7 @@ def test_delete_all_deletes_records(self):
def test_delete_all_can_delete_partially(self):
self.create_record({'foo': 'po'})
self.create_record()
filters = [Filter('foo', 'bar', utils.COMPARISON.EQ.value)]
filters = [Filter('foo', 'bar', utils.COMPARISON.EQ)]
self.storage.delete_all(filters=filters, **self.storage_kw)
_, count = self.storage.get_all(**self.storage_kw)
self.assertEqual(count, 1)
Expand Down Expand Up @@ -894,7 +894,7 @@ def test_filtering_on_arbitrary_field_excludes_deleted_records(self):
self.create_record({'status': 0})
self.create_and_delete_record({'status': 0})

filters += [Filter('status', 0, utils.COMPARISON.EQ.value)]
filters += [Filter('status', 0, utils.COMPARISON.EQ)]
records, count = self.storage.get_all(filters=filters,
include_deleted=True,
**self.storage_kw)
Expand All @@ -906,7 +906,7 @@ def test_support_filtering_on_deleted_field(self):
self.create_record()
self.create_and_delete_record()

filters += [Filter('deleted', True, utils.COMPARISON.EQ.value)]
filters += [Filter('deleted', True, utils.COMPARISON.EQ)]
records, count = self.storage.get_all(filters=filters,
include_deleted=True,
**self.storage_kw)
Expand All @@ -919,7 +919,7 @@ def test_support_filtering_out_on_deleted_field(self):
self.create_record()
self.create_and_delete_record()

filters += [Filter('deleted', True, utils.COMPARISON.NOT.value)]
filters += [Filter('deleted', True, utils.COMPARISON.NOT)]
records, count = self.storage.get_all(filters=filters,
include_deleted=True,
**self.storage_kw)
Expand All @@ -932,7 +932,7 @@ def test_return_empty_set_if_filtering_on_deleted_false(self):
self.create_record()
self.create_and_delete_record()

filters += [Filter('deleted', False, utils.COMPARISON.EQ.value)]
filters += [Filter('deleted', False, utils.COMPARISON.EQ)]
records, count = self.storage.get_all(filters=filters,
include_deleted=True,
**self.storage_kw)
Expand All @@ -943,7 +943,7 @@ def test_return_empty_set_if_filtering_on_deleted_without_include(self):
self.create_record()
self.create_and_delete_record()

filters = [Filter('deleted', True, utils.COMPARISON.EQ.value)]
filters = [Filter('deleted', True, utils.COMPARISON.EQ)]
records, count = self.storage.get_all(filters=filters,
**self.storage_kw)
self.assertEqual(len(records), 0)
Expand All @@ -961,8 +961,7 @@ def test_pagination_rules_on_last_modified_apply_to_deleted_records(self):
else:
self.create_record()

pagination = [[Filter('last_modified', 314,
utils.COMPARISON.GT.value)]]
pagination = [[Filter('last_modified', 314, utils.COMPARISON.GT)]]
sorting = [Sort('last_modified', 1)]
records, count = self.storage.get_all(sorting=sorting,
pagination_rules=pagination,
Expand Down
4 changes: 3 additions & 1 deletion cliquet_docs/reference/notifications.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ For example:

.. code-block:: python

from cliquet.events import ResourceChanged, ACTIONS

config.add_subscriber(on_mushroom_changed, ResourceChanged, for_resources=('mushroom',))
config.add_subscriber(on_record_deleted, ResourceChanged, for_actions=('delete',))
config.add_subscriber(on_record_deleted, ResourceChanged, for_actions=(ACTIONS.DELETE,))


Payload
Expand Down