Repository: django-cms/django-cms

Issue:
Django Reversion + CMS
When you try to restore deleted page (3.0.9 cms):

TransactionManagementError at /admin/cms/page/recover/1044/
An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.
Request Method: POST
Request URL:    http://xxxxxx/admin/cms/page/recover/1044/?language=pl
Django Version: 1.7.3
Exception Type: TransactionManagementError
Exception Value:  
An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.
Exception Location: /srv/venv/py33dj17A/lib/python3.3/site-packages/django/db/backends/**init**.py in validate_no_broken_transaction, line 386
Python Executable:  /opt/rh/python33/root/usr/bin/uwsgi
Python Version: 3.3.2
Python Path:  
['.',
 '',
 '/srv/venv/py33dj17A/lib64/python33.zip',
 '/srv/venv/py33dj17A/lib64/python3.3',
 '/srv/venv/py33dj17A/lib64/python3.3/plat-linux',
 '/srv/venv/py33dj17A/lib64/python3.3/lib-dynload',
 '/opt/rh/python33/root/usr/lib64/python3.3',
 '/opt/rh/python33/root/usr/lib/python3.3',
 '/srv/venv/py33dj17A/lib/python3.3/site-packages']
Server time:    Pt, 6 Luty 2015 09:55:06 +0100
Traceback Switch to copy-and-paste view

/srv/venv/py33dj17A/lib/python3.3/site-packages/django/core/handlers/base.py in get_response
                    response = wrapped_callback(request, _callback_args, *_callback_kwargs) ...
▶ Local vars
/srv/venv/py33dj17A/lib/python3.3/site-packages/django/utils/decorators.py in _wrapped_view
                    response = view_func(request, _args, *_kwargs) ...
▶ Local vars
/srv/venv/py33dj17A/lib/python3.3/site-packages/django/views/decorators/cache.py in _wrapped_view_func
        response = view_func(request, _args, *_kwargs) ...
▶ Local vars
/srv/venv/py33dj17A/lib/python3.3/site-packages/django/contrib/admin/sites.py in inner
            return view(request, _args, *_kwargs) ...
▶ Local vars
/srv/venv/py33dj17A/lib/python3.3/site-packages/reversion/revisions.py in do_revision_context
                return func(_args, *_kwargs) ...
▶ Local vars
/srv/venv/py33dj17A/lib/python3.3/site-packages/cms/admin/pageadmin.py in recover_view
        return super(PageAdmin, self).recover_view(request, version_id, extra_context) ...
▶ Local vars
/srv/venv/py33dj17A/lib/python3.3/site-packages/django/db/transaction.py in inner
                return func(_args, *_kwargs) ...
▶ Local vars
/srv/venv/py33dj17A/lib/python3.3/site-packages/reversion/admin.py in recover_view
        return self.render_revision_form(request, obj, version, context, recover=True) ...
▶ Local vars
/srv/venv/py33dj17A/lib/python3.3/site-packages/cms/admin/pageadmin.py in render_revision_form
        return super(PageAdmin, self).render_revision_form(request, obj, version, context, revert, recover) ...
▶ Local vars
/srv/venv/py33dj17A/lib/python3.3/site-packages/reversion/admin.py in render_revision_form
                self.save_model(request, new_object, form, change=True) ...
▶ Local vars
/srv/venv/py33dj17A/lib/python3.3/site-packages/cms/admin/pageadmin.py in save_model
            revert_plugins(request, obj.version.pk, obj) ...
▶ Local vars
/srv/venv/py33dj17A/lib/python3.3/site-packages/cms/admin/views.py in revert_plugins
            title.pk = Title.objects.get(page=page, language=title.language).pk ...
▶ Local vars
/srv/venv/py33dj17A/lib/python3.3/site-packages/django/db/models/manager.py in manager_method
                return getattr(self.get_queryset(), name)(_args, *_kwargs) ...
▶ Local vars
/srv/venv/py33dj17A/lib/python3.3/site-packages/django/db/models/query.py in get
        num = len(clone) ...
▶ Local vars
/srv/venv/py33dj17A/lib/python3.3/site-packages/django/db/models/query.py in __len__
        self._fetch_all() ...
▶ Local vars
/srv/venv/py33dj17A/lib/python3.3/site-packages/django/db/models/query.py in _fetch_all
            self._result_cache = list(self.iterator()) ...
▶ Local vars
/srv/venv/py33dj17A/lib/python3.3/site-packages/django/db/models/query.py in iterator
        for row in compiler.results_iter(): ...
▶ Local vars
/srv/venv/py33dj17A/lib/python3.3/site-packages/django/db/models/sql/compiler.py in results_iter
        for rows in self.execute_sql(MULTI): ...
▶ Local vars
/srv/venv/py33dj17A/lib/python3.3/site-packages/django/db/models/sql/compiler.py in execute_sql
            cursor.execute(sql, params) ...
▶ Local vars
/srv/venv/py33dj17A/lib/python3.3/site-packages/django/db/backends/utils.py in execute
            return super(CursorDebugWrapper, self).execute(sql, params) ...
▶ Local vars
/srv/venv/py33dj17A/lib/python3.3/site-packages/django/db/backends/utils.py in execute
        self.db.validate_no_broken_transaction() ...
▶ Local vars
/srv/venv/py33dj17A/lib/python3.3/site-packages/django/db/backends/__init__.py in validate_no_broken_transaction
                "An error occurred in the current transaction. You can't " ...
▶ Local vars



Discussion:
What database backend are you using? Which reversion version?

Python 3.3.2 (default, Aug 14 2014, 14:25:52) 
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux
print(reversion.**version**)
(1, 8, 5)
print(MySQLdb.**version**)
1.3.4

Is the tree clean?
Could you first run `manage.py cms delete-orphaned-plugins` and `manage.py cms fix-mptt` and then retry?

ad manage.py cms delete-orphaned-plugins:
 Deleted instances of: 
    0 uninstalled plugins  
    0 plugins with unsaved instances
all done

ad fix-mptt:
fixing mptt page tree
fixing mptt plugin tree
all done

I was testing it on fresh install of app so probably there was not any tree issues. Now I will turn off solid_i118n_patterns and use standard  django.conf.urls.i18n import i18n_patterns - to be sure there  is not caused by uncompatible module.

Problem still exists (without solid_i118n_patterns). You can revert blank site (without any plugin e.q text in placeholder). When you have added text, or (probably) any other plugin then you receive the error.

Update of all pip packages do not help to solve problem.

Ok, I reproduced:
- Python 3.4 (sorry can't test on 3.3 currently)
- Django 1.7.4
- django-cms 3.0.9
- mysqlclient 1.3.5
- django-reversion 1.8.5

Steps:
- clone django-cms: `git clone https://github.com/divio/django-cms/`
- run develop.py: `DATABASE_URL="mysql://user:pwd@localhost/djangocms_test" develop.py server`
- create home page from the splash screen
- create sub page from the page tree
- go to the sub page in frontend
- add text plugin in any placeholder
- publish the page
- delete the sub page
- go to page tree
- click "recover deleted pages"
- select sub page
- save from the recover form

yakky if you remove home page (which has text plugin) and try to restore it from reversion you will receive the same error (to clarify that the problem concerns every page, even with simple/root tree structure, without subpages)


Resolving patch:
diff --git a/cms/models/titlemodels.py b/cms/models/titlemodels.py
--- a/cms/models/titlemodels.py
+++ b/cms/models/titlemodels.py
@@ -131,4 +131,13 @@ def overwrite_url(self):
         return None
 
 
-reversion_register(Title)
+def _reversion():
+    exclude_fields = ['publisher_is_draft', 'publisher_public', 'publisher_state']
+
+    reversion_register(
+        Title,
+        exclude_fields=exclude_fields
+    )
+
+
+_reversion()
\ No newline at end of file


Test patch: