MongoEngine/mongoengine — pull request #345 by amcgregor (2013-05-29 16:10:11+00:00)
Title: Addition of pre_save_validation and move of pre_save to after validation.

Howdy!

My use case is thus: I want to use `pre_save` to hook automatic slug creation in a dynamic way. E.g.:

@slugify(source='name', target='short', mutable=False)
class Company(Document):
short = StringField(db_field='s', unique=True)
name = StringField(db_field='n', required=True)

The internals of the `slugify` decorator register a `pre_save` handler with the class being decorated. Unfortunately, this hook needs to know if the record is being created (even forced as an insert) in order to properly generate the slug. (Mutable identifies if on subsequent non-insert saves the slug should be updated if the source has changed; usually no.) The use of `post_save` would be bad, requiring additional database round-trips.

This pull request moves `pre_save` to below the validation and determination of creation state and adds a new decorator `pre_save_validation` where `pre_save` once was. If hooks were written according to the docs (with the encompassing `**kwargs` declaration) then this change won't cause errors, but the behaviour is slightly different. If the old behaviour is desired existing `pre_save` hooks will need to switch to `pre_save_validation`.