diff --git a/cliquet/initialization.py b/cliquet/initialization.py index f8a06fbf..292c2993 100644 --- a/cliquet/initialization.py +++ b/cliquet/initialization.py @@ -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): diff --git a/cliquet/resource/__init__.py b/cliquet/resource/__init__.py index 2be81505..5f6e4c0e 100644 --- a/cliquet/resource/__init__.py +++ b/cliquet/resource/__init__.py @@ -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: @@ -883,7 +882,7 @@ 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, ' @@ -891,7 +890,7 @@ def _extract_filters(self, queryparams=None): 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) ) @@ -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 = { @@ -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)) @@ -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) @@ -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 diff --git a/cliquet/storage/memory.py b/cliquet/storage/memory.py index 6949a40c..4010d200 100644 --- a/cliquet/storage/memory.py +++ b/cliquet/storage/memory.py @@ -91,14 +91,14 @@ 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: @@ -106,8 +106,7 @@ def apply_filters(self, records, filters): 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) @@ -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) diff --git a/cliquet/storage/postgresql/__init__.py b/cliquet/storage/postgresql/__init__.py index 83b49318..326c0c3a 100644 --- a/cliquet/storage/postgresql/__init__.py +++ b/cliquet/storage/postgresql/__init__.py @@ -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 = [] @@ -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): @@ -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) @@ -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) @@ -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 diff --git a/cliquet/tests/test_storage.py b/cliquet/tests/test_storage.py index c874d2f5..8b1e75db 100644 --- a/cliquet/tests/test_storage.py +++ b/cliquet/tests/test_storage.py @@ -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) @@ -429,7 +429,7 @@ 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) @@ -437,7 +437,7 @@ def test_get_all_can_filter_with_list_of_values_on_id(self): 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) @@ -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) @@ -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) @@ -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): @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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, diff --git a/cliquet_docs/reference/notifications.rst b/cliquet_docs/reference/notifications.rst index 8a470824..fca8cbf8 100644 --- a/cliquet_docs/reference/notifications.rst +++ b/cliquet_docs/reference/notifications.rst @@ -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