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_to_revision() in other middleware.py does not save #664

Closed
pybean opened this issue Sep 12, 2017 · 7 comments
Closed

add_to_revision() in other middleware.py does not save #664

pybean opened this issue Sep 12, 2017 · 7 comments

Comments

@pybean
Copy link

pybean commented Sep 12, 2017

Hello,
I created my own django middleware.py, and simply called "add_to_revision(instance)" in its process_request() method. I see that my middleware works without any exceptions, but I do not see the new Version instance created by the "add_to_revision" call in database. This middleware worked fine when I was using reversion 2.0.8, and then it stops storing version instance after upgrading the reversion module to 2.0.10.

Please let me know if you need more information. Thanks!

@etianen
Copy link
Owner

etianen commented Sep 12, 2017 via email

@pybean
Copy link
Author

pybean commented Sep 12, 2017

Thanks for the reply.

  1. Yes, I am using RevisionMiddleware, so I could simply call add_to_revision without using additional create_revision() block and handle the rest.
  2. my middleware is old-style.
  3. No, I am still using django 1.8.

Please check my code below. The purpose is to backup the latest snapshot before the instance deletion. Thanks again!

def backup_pre_delete_info(sender, instance, **kwargs):
    if reversion.is_registered(sender):
        reversion.add_to_revision(instance)

class MyMiddleware(object):
    def process_request(self, request):
        if not request.method in ('GET', 'HEAD', 'OPTIONS', 'TRACE'):
            reversion.set_user(request.user)
            signals.pre_delete.connect(backup_pre_delete_info, weak=False)

@etianen
Copy link
Owner

etianen commented Sep 12, 2017 via email

@etianen
Copy link
Owner

etianen commented Sep 12, 2017 via email

@pybean
Copy link
Author

pybean commented Sep 12, 2017

Thank you so much for this clear explanation.
I understand that in general we don't have to save a backup copy before instance deletion. But for my case:

  1. My reversion_* tables are growing pretty big, so I need to archive old reversion data.
  2. Able to trace who, when, why the instance was deleted
  3. Grouping all related instances from cascade delete, and/or other modified instances in a single revision (my favorite part)

I am trying to avoid using signal handler in model.py, because I have to handle create_revision block and transaction.atomic for different situations, and your RevisionMiddleware already takes care about this.

Do you think is it okay to stick with 2.0.8? Please advise if it has any limitations, or I shouldn't use it at all. Thanks!

@etianen
Copy link
Owner

etianen commented Sep 13, 2017 via email

@pybean
Copy link
Author

pybean commented Sep 13, 2017

Thank you for the fast reply, then I will stick with 2.0.8 for the time being.

What I pasted was the partial code. I disconnect those signals in process_response() with unique identifiers so it works as expected. Still thanks for your kind reminder.

There are complicated models in my project. Sometimes purging an top level instance will remove more than 100 related instances triggered by cascade delete. So I needed a complete snapshot for all deleted instances triggered by a single delete call, and logging (when/who/why) is just a bonus because the Revision model already has all those fields.

I wonder if you could add an option for create_revision block, to ignore the new check that a model still exists in the database before adding it to the revision. Please consider. Thanks!

@etianen etianen closed this as completed Jul 19, 2018
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

No branches or pull requests

2 participants