Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
renamed to
Browse files Browse the repository at this point in the history
  • Loading branch information
jstoiko committed Oct 7, 2015
1 parent f2b2629 commit 298e58d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 38 deletions.
5 changes: 3 additions & 2 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
Changelog
=========

* :release:`0.5.0 <2015-09-xx>`
* :support:`-` Added support for Nefertari event handlers and removed support for processors
* :release:`0.5.0 <2015-10-xx>`
* :support:`-` Added support for Nefertari event handlers
* :support:`-` Simplified field processors, '_before_processors' is now '_processors', removed '_after_processors'
* :support:`-` Added support for `'nefertari-guards' <https://nefertari-guards.readthedocs.org/>`_
* :support:`-` Added support for Nefertari '_hidden_fields'
* :support:`-` ACL permission names in RAML now match real permission names instead of http methods
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Field validators
Field processors
================

Ramses allows users to define functions that accept field data and return modified field value, may perform validation or perform other actions related to field.

These functions are called "field validators". They are set up per-field and are called when request comes into application that modifies the field for which validator is set up (when field is present in request JSON).
These functions are called "field processors". They are set up per-field and are called when request comes into application that modifies the field for which validator is set up (when field is present in request JSON).


Usage basics
------------

Field validators are defined in your target project just like event handlers:
Field processors are defined in your target project just like event handlers:

.. code-block:: python
Expand All @@ -19,9 +19,9 @@ Field validators are defined in your target project just like event handlers:
return (kwargs['new_value'] or '').lower().strip()
To use this field validator, define ``_validators`` attribute in your field definition (next to ``_db_settings``) which should be an array listing names of validators to apply. You can also use ``_backref_validators`` attribute defined the same way to specify validators for backref field. For backref validators to be set up, ``_db_settings`` must contain attributes ``document``, ``type=relationship`` and ``backref_name``.
To use this field validator, define ``_processors`` attribute in your field definition (next to ``_db_settings``) which should be an array listing names of processors to apply. You can also use ``_backref_processors`` attribute defined the same way to specify processors for backref field. For backref processors to be set up, ``_db_settings`` must contain attributes ``document``, ``type=relationship`` and ``backref_name``.

Field validators should expect following kwargs to be passed:
Field processors should expect following kwargs to be passed:

**new_value**
New value of of field.
Expand All @@ -47,7 +47,7 @@ Processors are called in order they are listed. Each validator must return proce
Examples
--------

If we had following validators defined:
If we had following processors defined:

.. code-block:: python
Expand Down Expand Up @@ -84,8 +84,8 @@ If we had following validators defined:
"document": "Story",
"backref_name": "owner"
},
"_validators": ["validate_stories_exist"],
"_backref_validators": ["lowercase"]
"_processors": ["validate_stories_exist"],
"_backref_processors": ["lowercase"]
},
...
}
Expand Down
2 changes: 1 addition & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Table of Contents
schemas
fields
event_handlers
field_validators
field_processors
relationships
changelog

Expand Down
26 changes: 13 additions & 13 deletions ramses/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def generate_model_cls(config, schema, model_name, raml_resource,
# Generate new model class
model_cls = metaclass(model_name, tuple(bases), attrs)
setup_model_event_subscribers(config, model_cls, schema)
setup_fields_validators(config, model_cls, schema)
setup_fields_processors(config, model_cls, schema)
return model_cls, auth_model


Expand Down Expand Up @@ -233,11 +233,11 @@ def setup_model_event_subscribers(config, model_cls, schema):
sub_func, event_objects, **event_kwargs)


def setup_fields_validators(config, model_cls, schema):
""" Set up model fields' validators.
def setup_fields_processors(config, model_cls, schema):
""" Set up model fields' processors.
:param config: Pyramid Configurator instance.
:param model_cls: Model class for field of which validators should be
:param model_cls: Model class for field of which processors should be
set up.
:param schema: Dict of model JSON schema.
"""
Expand All @@ -246,27 +246,27 @@ def setup_fields_validators(config, model_cls, schema):
if not props:
continue

validators = props.get('_validators')
backref_validators = props.get('_backref_validators')
processors = props.get('_processors')
backref_processors = props.get('_backref_processors')

if validators:
validators = [resolve_to_callable(val) for val in validators]
if processors:
processors = [resolve_to_callable(val) for val in processors]
setup_kwargs = {'model': model_cls, 'field': field_name}
config.add_field_processors(validators, **setup_kwargs)
config.add_field_processors(processors, **setup_kwargs)

if backref_validators:
if backref_processors:
db_settings = props.get('_db_settings', {})
is_relationship = db_settings.get('type') == 'relationship'
document = db_settings.get('document')
backref_name = db_settings.get('backref_name')
if not (is_relationship and document and backref_name):
continue

backref_validators = [
resolve_to_callable(val) for val in backref_validators]
backref_processors = [
resolve_to_callable(val) for val in backref_processors]
setup_kwargs = {
'model': engine.get_document_cls(document),
'field': backref_name
}
config.add_field_processors(
backref_validators, **setup_kwargs)
backref_processors, **setup_kwargs)
28 changes: 14 additions & 14 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def test_handle_model_generation(self, mock_set):
assert auth_model


@patch('ramses.models.setup_fields_validators')
@patch('ramses.models.setup_fields_processors')
@patch('ramses.models.setup_model_event_subscribers')
@patch('ramses.models.registry')
@pytest.mark.usefixtures('engine_mock')
Expand Down Expand Up @@ -382,7 +382,7 @@ def test_setup_model_event_subscribers(self, mock_get, mock_resolve):

@patch('ramses.models.resolve_to_callable')
@patch('ramses.models.engine')
def test_setup_fields_validators(self, mock_eng, mock_resolve):
def test_setup_fields_processors(self, mock_eng, mock_resolve):
from ramses import models
config = Mock()
schema = {
Expand All @@ -393,13 +393,13 @@ def test_setup_fields_validators(self, mock_eng, mock_resolve):
"document": "Story",
"backref_name": "owner",
},
"_validators": ["lowercase"],
"_backref_validators": ["backref_lowercase"]
"_processors": ["lowercase"],
"_backref_processors": ["backref_lowercase"]
}
}
}

models.setup_fields_validators(config, 'mymodel', schema)
models.setup_fields_processors(config, 'mymodel', schema)

mock_resolve.assert_has_calls([
call('lowercase'), call('backref_lowercase')])
Expand All @@ -412,7 +412,7 @@ def test_setup_fields_validators(self, mock_eng, mock_resolve):

@patch('ramses.models.resolve_to_callable')
@patch('ramses.models.engine')
def test_setup_fields_validators_backref_not_rel(
def test_setup_fields_processors_backref_not_rel(
self, mock_eng, mock_resolve):
from ramses import models
config = Mock()
Expand All @@ -424,16 +424,16 @@ def test_setup_fields_validators_backref_not_rel(
"document": "Story",
"backref_name": "owner",
},
"_backref_validators": ["backref_lowercase"]
"_backref_processors": ["backref_lowercase"]
}
}
}
models.setup_fields_validators(config, 'mymodel', schema)
models.setup_fields_processors(config, 'mymodel', schema)
assert not config.add_field_processors.called

@patch('ramses.models.resolve_to_callable')
@patch('ramses.models.engine')
def test_setup_fields_validators_backref_no_doc(
def test_setup_fields_processors_backref_no_doc(
self, mock_eng, mock_resolve):
from ramses import models
config = Mock()
Expand All @@ -444,16 +444,16 @@ def test_setup_fields_validators_backref_no_doc(
"type": "relationship",
"backref_name": "owner",
},
"_backref_validators": ["backref_lowercase"]
"_backref_processors": ["backref_lowercase"]
}
}
}
models.setup_fields_validators(config, 'mymodel', schema)
models.setup_fields_processors(config, 'mymodel', schema)
assert not config.add_field_processors.called

@patch('ramses.models.resolve_to_callable')
@patch('ramses.models.engine')
def test_setup_fields_validators_backref_no_backname(
def test_setup_fields_processors_backref_no_backname(
self, mock_eng, mock_resolve):
from ramses import models
config = Mock()
Expand All @@ -464,9 +464,9 @@ def test_setup_fields_validators_backref_no_backname(
"type": "relationship",
"document": "Story",
},
"_backref_validators": ["backref_lowercase"]
"_backref_processors": ["backref_lowercase"]
}
}
}
models.setup_fields_validators(config, 'mymodel', schema)
models.setup_fields_processors(config, 'mymodel', schema)
assert not config.add_field_processors.called

0 comments on commit 298e58d

Please sign in to comment.