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

Add fields to context wrapper #731

Merged
merged 3 commits into from Jul 5, 2018

Conversation

massover
Copy link

@massover massover commented Jun 14, 2018

This adds meta, user, and comment support to the _ContextWrapper.

I found it nice to build/test my own create_revision.

def create_revision_for_obj(user, manage_manually=False, using=None, atomic=True, **kwargs):
    from reversion.models import Revision
    using = using or router.db_for_write(Revision)
    return _ContextWrapper(_create_revision_context, (manage_manually, using, atomic), ObjMeta, user, **kwargs)

I then could use it like so (as user and the meta obj were always required):

meta_values = {'hello': 'world'}
with create_revision_for_obj(request.user, **meta_values):
  obj.save()

Compared to this:

meta_values = {'hello': 'world'}
with create_revision():
  add_meta(ObjMeta, **meta_values)
  set_user(request.user)
  obj.save()

I'm not particularly attached to modifying the actual create_revision from the library. But I did add that in case that change to the api was liked. This is more about the changes to _ContextWrapper to build your own create_revision context managers. I am glad to remove it.

@etianen
Copy link
Owner

etianen commented Jun 15, 2018

Nice idea!

I think that this should be implemented inside the _create_revision_context function, since then the calls to set_meta, set_comment etc only need to be implemeted in one place.

@massover
Copy link
Author

massover commented Jun 28, 2018

@etianen that was a great suggestion!

I moved the code to _create_revision_context. I actually removed the changes from create_revision as to not touch that since I don't need it. I also added an arguably useless test in another commit so it can be removed easily if you prefer to not keep it. Let me know if you think there is any documentation to add for this.

@etianen etianen merged commit 1fdc824 into etianen:master Jul 5, 2018
@etianen
Copy link
Owner

etianen commented Jul 5, 2018

Thanks!

Any chance of a pull request to update the docs: https://github.com/etianen/django-reversion/tree/master/docs ?

@etianen
Copy link
Owner

etianen commented Jul 19, 2018

Unfortunately, I've had to revert this pull request.
Extra fields should be added to create_revision(), not _create_revision_context(), which is a private API.

etianen added a commit that referenced this pull request Jul 19, 2018
…rapper"

This reverts commit 1fdc824, reversing
changes made to d211d0a.
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.

None yet

2 participants