Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addition of pre_save_validation and move of pre_save to after validation. #345

Merged
merged 4 commits into from
Jun 4, 2013
Merged

Conversation

amcgregor
Copy link
Contributor

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.

rozza added a commit that referenced this pull request Jun 4, 2013
Addition of pre_save_validation and move of pre_save to after validation.
@rozza rozza merged commit dcd23a0 into MongoEngine:master Jun 4, 2013
@rozza
Copy link
Contributor

rozza commented Jun 4, 2013

Thanks for the code @amcgregor

rozza added a commit that referenced this pull request Jun 4, 2013
@rozza
Copy link
Contributor

rozza commented Jun 4, 2013

Made some minor changes.

pre_save has to stay where it is - as it could be backwards breaking to move it.
pre_save_post_validation - is the post validation signal that has the created flag in it.

This way we can introduce in a minor release and not force anyone to handle upgrading :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants