How create REVERT opportunity in django-reversion app?

157 Views Asked by At

I have task to rewrite correct Function Based View to Class Based View. Below you can see my code. My question is whats wrong with my Class Based View? It raise error. Where is my mistake?

FBV:

@reversion.create_revision()
def article_revert(request, pk, article_reversion_id):
    article = get_object_or_404(Article, pk=pk)
    revision = get_object_or_404(Version.objects.get_for_object(article), pk=article_reversion_id).revision
    reversion.set_user(request.user)
    reversion.set_comment("REVERT to version: {}".format(revision.id))
    revision.revert()
    return redirect('project:article_list')

CBV:

class ArticleRevert(RevisionMixin, View):
    model = Article

    def get(self, request, *args, **kwargs):
        article = get_object_or_404(Article, pk=pk)
        revision = get_object_or_404(Version.objects.get_for_object(article), pk=article_reversion_id).revision
        reversion.set_comment("REVERT to version: {}".format(revision.id))
        revision.revert()
        return redirect('project:article_list')

When I use CBV it raise next error:

Traceback (most recent call last):
  File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/reversion/views.py", line 43, in do_revision_view
    return func(request, *args, **kwargs)
  File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/Applications/Projects/web/project/article/views.py", line 166, in get
    reversion.set_comment("REVERT to version: {}".format(revision.id))
  File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/reversion/revisions.py", line 122, in set_comment
    _update_frame(comment=comment)
  File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/reversion/revisions.py", line 87, in _update_frame
    _local.stack = _local.stack[:-1] + (_current_frame()._replace(**kwargs),)
  File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/reversion/revisions.py", line 53, in _current_frame
    raise RevisionManagementError("There is no active revision for this thread")
RevisionManagementError: There is no active revision for this thread
0

There are 0 best solutions below