def _reversion_revisionform_view(self, request, version, template_name, extra_context=None): # Check that database transactions are supported. if not connection.features.uses_savepoints: raise ImproperlyConfigured("Cannot use VersionAdmin with a database that does not support savepoints.") # Run the view. try: with transaction.atomic(using=version.db): # Revert the revision. version.revision.revert(delete=True) # Run the normal changeform view. with self.create_revision(request): response = self.changeform_view(request, version.object_id, request.path, extra_context) # Decide on whether the keep the changes. if request.method == "POST" and response.status_code == 302: set_comment(_("Reverted to previous version, saved on %(datetime)s") % { "datetime": localize(template_localtime(version.revision.date_created)), }) else: response.template_name = template_name # Set the template name to the correct template. response.render() # Eagerly render the response, so it's using the latest version. raise _RollBackRevisionView(response) # Raise exception to undo the transaction and revision. except RevertError as ex: opts = self.model._meta messages.error(request, force_text(ex)) return redirect("{}:{}_{}_changelist".format(self.admin_site.name, opts.app_label, opts.model_name)) except _RollBackRevisionView as ex: return ex.response return response
def _reversion_revisionform_view(self, request, version, template_name, extra_context=None): # Check that database transactions are supported. if not connection.features.uses_savepoints: raise ImproperlyConfigured("Cannot use VersionAdmin with a database that does not support savepoints.") # Run the view. try: with transaction.atomic(using=version.db): # Revert the revision. version.revision.revert(delete=True) # Run the normal changeform view. with self.create_revision(request): response = self.changeform_view(request, quote(version.object_id), request.path, extra_context) # Decide on whether the keep the changes. if request.method == "POST" and response.status_code == 302: set_comment(_("Reverted to previous version, saved on %(datetime)s") % { "datetime": localize(template_localtime(version.revision.date_created)), }) else: response.template_name = template_name # Set the template name to the correct template. response.render() # Eagerly render the response, so it's using the latest version. raise _RollBackRevisionView(response) # Raise exception to undo the transaction and revision. except RevertError as ex: opts = self.model._meta messages.error(request, force_text(ex)) return redirect("{}:{}_{}_changelist".format(self.admin_site.name, opts.app_label, opts.model_name)) except _RollBackRevisionView as ex: return ex.response return response
def version_view(self, request, version, *args, **kwargs): try: with transaction.atomic(using=version.db): # Revert the revision. version.revision.revert(delete=True) # Run the normal changeform view. self.object = self.get_object() context = self.get_context_data(object=self.object) context["version"] = version response = self.render_to_response(context) response.render( ) # eager rendering of response is necessary before db rollback raise _RollBackRevisionView(response) except _RollBackRevisionView as ex: return ex.response
def do_revision_view(request, *args, **kwargs): if request_creates_revision(request): try: with create_revision_base(manage_manually=manage_manually, using=using, atomic=atomic): response = func(request, *args, **kwargs) # Check for an error response. if response.status_code >= 400: raise _RollBackRevisionView(response) # Otherwise, we're good. _set_user_from_request(request) # Additional call _add_meta(request) return response except _RollBackRevisionView as ex: return ex.response return func(request, *args, **kwargs)