Example #1
0
    def test_response_change_change_history_setting_off(self):
        """
        Test the response_change method that it works with a _change_history
        in the POST and settings.SIMPLE_HISTORY_EDIT set to False
        """
        request = RequestFactory().post("/")
        request.POST = {"_change_history": True}
        request.session = "session"
        request._messages = FallbackStorage(request)
        request.path = "/awesome/url/"
        request.user = self.user

        poll = Poll.objects.create(question="why?", pub_date=today)
        poll.question = "how?"
        poll.save()

        admin_site = AdminSite()
        admin = SimpleHistoryAdmin(Poll, admin_site)

        response = admin.response_change(request, poll)

        with patch("simple_history.admin.admin.ModelAdmin.response_change"
                   ) as m_admin:
            m_admin.return_value = "it was called"
            response = admin.response_change(request, poll)

        self.assertEqual(response, "it was called")
    def test_response_change_change_history_setting_off(self):
        """
        Test the response_change method that it works with a _change_history
        in the POST and settings.SIMPLE_HISTORY_EDIT set to False
        """
        request = RequestFactory().post('/')
        request.POST = {'_change_history': True}
        request.session = 'session'
        request._messages = FallbackStorage(request)
        request.path = '/awesome/url/'
        request.user = self.user

        poll = Poll.objects.create(question="why?", pub_date=today)
        poll.question = "how?"
        poll.save()

        admin_site = AdminSite()
        admin = SimpleHistoryAdmin(Poll, admin_site)

        response = admin.response_change(request, poll)

        with patch(
            'simple_history.admin.admin.ModelAdmin.response_change'
        ) as m_admin:
            m_admin.return_value = 'it was called'
            response = admin.response_change(request, poll)

        self.assertEqual(response, 'it was called')
    def test_history_does_not_exist(self):
        request = RequestFactory().post('/')
        request.session = 'session'
        request._messages = FallbackStorage(request)
        request.user = self.user

        admin_site = AdminSite()
        admin = SimpleHistoryAdmin(Poll, admin_site)
        admin.show_all_history = True
        with self.assertRaises(http.Http404) as cm:
            admin.history_view(request, '9')
    def test_response_change_no_change_history(self):
        request = RequestFactory().post("/")
        request.session = "session"
        request._messages = FallbackStorage(request)
        request.user = self.user

        poll = Poll.objects.create(question="why?", pub_date=today)
        poll.question = "how?"
        poll.save()

        admin_site = AdminSite()
        admin = SimpleHistoryAdmin(Poll, admin_site)

        with patch("simple_history.admin.admin.ModelAdmin.response_change") as m_admin:
            m_admin.return_value = "it was called"
            response = admin.response_change(request, poll)

        self.assertEqual(response, "it was called")
    def test_response_change_no_change_history(self):
        request = RequestFactory().post("/")
        request.session = "session"
        request._messages = FallbackStorage(request)
        request.user = self.user

        poll = Poll.objects.create(question="why?", pub_date=today)
        poll.question = "how?"
        poll.save()

        admin_site = AdminSite()
        admin = SimpleHistoryAdmin(Poll, admin_site)

        with patch("simple_history.admin.admin.ModelAdmin.response_change") as m_admin:
            m_admin.return_value = "it was called"
            response = admin.response_change(request, poll)

        self.assertEqual(response, "it was called")
Example #6
0
    def test_history_form_view_accepts_additional_context(self):
        request = RequestFactory().post("/")
        request.session = "session"
        request._messages = FallbackStorage(request)
        request.user = self.user

        poll = Poll.objects.create(question="why?", pub_date=today)
        poll.question = "how?"
        poll.save()
        history = poll.history.all()[0]

        admin_site = AdminSite()
        admin = SimpleHistoryAdmin(Poll, admin_site)

        with patch("simple_history.admin.render") as mock_render:
            admin.history_form_view(
                request,
                poll.id,
                history.pk,
                extra_context={"anything_else": "will be merged into context"},
            )

        context = {
            # Verify this is set for original object
            "anything_else": "will be merged into context",
            "original": poll,
            "change_history": False,
            "title": "Revert %s" % force_str(poll),
            "adminform": ANY,
            "object_id": poll.id,
            "is_popup": False,
            "media": ANY,
            "errors": ANY,
            "app_label": "tests",
            "original_opts": ANY,
            "changelist_url": "/admin/tests/poll/",
            "change_url": ANY,
            "history_url": "/admin/tests/poll/1/history/",
            "add": False,
            "change": True,
            "has_add_permission": admin.has_add_permission(request),
            "has_change_permission":
            admin.has_change_permission(request, poll),
            "has_delete_permission":
            admin.has_delete_permission(request, poll),
            "revert_disabled": admin.revert_disabled,
            "has_file_field": True,
            "has_absolute_url": False,
            "form_url": "",
            "opts": ANY,
            "content_type_id": ANY,
            "save_as": admin.save_as,
            "save_on_top": admin.save_on_top,
            "root_path": getattr(admin_site, "root_path", None),
        }
        context.update(admin_site.each_context(request))
        mock_render.assert_called_once_with(request,
                                            admin.object_history_form_template,
                                            context)
Example #7
0
    def test_history_form_view_getting_history(self):
        request = RequestFactory().post('/')
        request.session = 'session'
        request._messages = FallbackStorage(request)
        request.user = self.user
        request.POST = {'_change_history': True}

        poll = Poll.objects.create(question="why?", pub_date=today)
        poll.question = "how?"
        poll.save()
        history = poll.history.all()[0]

        admin_site = AdminSite()
        admin = SimpleHistoryAdmin(Poll, admin_site)

        with patch('simple_history.admin.render') as mock_render:
            with patch('simple_history.admin.SIMPLE_HISTORY_EDIT', True):
                admin.history_form_view(request, poll.id, history.pk)

        context = {
            # Verify this is set for history object not poll object
            'original': history.instance,
            'change_history': True,
            'title': 'Revert %s' % force_text(history.instance),
            'adminform': ANY,
            'object_id': poll.id,
            'is_popup': False,
            'media': ANY,
            'errors': ANY,
            'app_label': 'tests',
            'original_opts': ANY,
            'changelist_url': '/admin/tests/poll/',
            'change_url': ANY,
            'history_url':
            '/admin/tests/poll/{pk}/history/'.format(pk=poll.pk),
            'add': False,
            'change': True,
            'has_add_permission': admin.has_add_permission(request),
            'has_change_permission':
            admin.has_change_permission(request, poll),
            'has_delete_permission':
            admin.has_delete_permission(request, poll),
            'has_file_field': True,
            'has_absolute_url': False,
            'form_url': '',
            'opts': ANY,
            'content_type_id': ANY,
            'save_as': admin.save_as,
            'save_on_top': admin.save_on_top,
            'root_path': getattr(admin_site, 'root_path', None),
        }

        mock_render.assert_called_once_with(
            request,
            template_name=admin.object_history_form_template,
            dictionary=context,
            current_app=admin_site.name)
    def test_history_form_view_getting_history_abstract_external(self):
        request = RequestFactory().post("/")
        request.session = "session"
        request._messages = FallbackStorage(request)
        request.user = self.user
        request.POST = {"_change_history": True}

        obj = ConcreteExternal.objects.create(name="test")
        obj.name = "new_test"
        obj.save()
        history = obj.history.all()[0]

        admin_site = AdminSite()
        admin = SimpleHistoryAdmin(ConcreteExternal, admin_site)

        with patch("simple_history.admin.render") as mock_render:
            with patch("simple_history.admin.SIMPLE_HISTORY_EDIT", True):
                admin.history_form_view(request, obj.id, history.pk)

        context = {
            # Verify this is set for history object
            "original": history.instance,
            "change_history": True,
            "title": "Revert %s" % force_str(history.instance),
            "adminform": ANY,
            "object_id": obj.id,
            "is_popup": False,
            "media": ANY,
            "errors": ANY,
            "app_label": "tests",
            "original_opts": ANY,
            "changelist_url": "/admin/tests/concreteexternal/",
            "change_url": ANY,
            "history_url": "/admin/tests/concreteexternal/{pk}/history/".format(
                pk=obj.pk
            ),
            "add": False,
            "change": True,
            "has_add_permission": admin.has_add_permission(request),
            "has_change_permission": admin.has_change_permission(request, obj),
            "has_delete_permission": admin.has_delete_permission(request, obj),
            "revert_disabled": admin.revert_disabled,
            "has_file_field": True,
            "has_absolute_url": False,
            "form_url": "",
            "opts": ANY,
            "content_type_id": ANY,
            "save_as": admin.save_as,
            "save_on_top": admin.save_on_top,
            "root_path": getattr(admin_site, "root_path", None),
        }
        context.update(admin_site.each_context(request))
        mock_render.assert_called_once_with(
            request, admin.object_history_form_template, context
        )
Example #9
0
    def test_history_form_view_getting_history_abstract_external(self):
        request = RequestFactory().post('/')
        request.session = 'session'
        request._messages = FallbackStorage(request)
        request.user = self.user
        request.POST = {'_change_history': True}

        obj = ConcreteExternal.objects.create(name='test')
        obj.name = "new_test"
        obj.save()
        history = obj.history.all()[0]

        admin_site = AdminSite()
        admin = SimpleHistoryAdmin(ConcreteExternal, admin_site)

        with patch('simple_history.admin.render') as mock_render:
            with patch('simple_history.admin.SIMPLE_HISTORY_EDIT', True):
                admin.history_form_view(request, obj.id, history.pk)

        context = {
            # Verify this is set for history object
            'original': history.instance,
            'change_history': True,

            'title': 'Revert %s' % force_text(history.instance),
            'adminform': ANY,
            'object_id': obj.id,
            'is_popup': False,
            'media': ANY,
            'errors': ANY,
            'app_label': 'tests',
            'original_opts': ANY,
            'changelist_url': '/admin/tests/concreteexternal/',
            'change_url': ANY,
            'history_url':
                '/admin/tests/concreteexternal/{pk}/history/'
                .format(pk=obj.pk),
            'add': False,
            'change': True,
            'has_add_permission': admin.has_add_permission(request),
            'has_change_permission': admin.has_change_permission(
                request, obj),
            'has_delete_permission': admin.has_delete_permission(
                request, obj),
            'has_file_field': True,
            'has_absolute_url': False,
            'form_url': '',
            'opts': ANY,
            'content_type_id': ANY,
            'save_as': admin.save_as,
            'save_on_top': admin.save_on_top,
            'root_path': getattr(admin_site, 'root_path', None),
        }
        mock_render.assert_called_once_with(
            request, admin.object_history_form_template, context)
Example #10
0
    def test_response_change(self):
        """
        Test the response_change method that it works with a _change_history
        in the POST and settings.SIMPLE_HISTORY_EDIT set to True
        """
        request = RequestFactory().post("/")
        request.POST = {"_change_history": True}
        request.session = "session"
        request._messages = FallbackStorage(request)
        request.path = "/awesome/url/"

        poll = Poll.objects.create(question="why?", pub_date=today)
        poll.question = "how?"
        poll.save()

        admin_site = AdminSite()
        admin = SimpleHistoryAdmin(Poll, admin_site)

        with patch("simple_history.admin.SIMPLE_HISTORY_EDIT", True):
            response = admin.response_change(request, poll)

        self.assertEqual(response["Location"], "/awesome/url/")
    def test_response_change(self):
        """
        Test the response_change method that it works with a _change_history
        in the POST and settings.SIMPLE_HISTORY_EDIT set to True
        """
        request = RequestFactory().post('/')
        request.POST = {'_change_history': True}
        request.session = 'session'
        request._messages = FallbackStorage(request)
        request.path = '/awesome/url/'

        poll = Poll.objects.create(question="why?", pub_date=today)
        poll.question = "how?"
        poll.save()

        admin_site = AdminSite()
        admin = SimpleHistoryAdmin(Poll, admin_site)

        with patch('simple_history.admin.SIMPLE_HISTORY_EDIT', True):
            response = admin.response_change(request, poll)

        self.assertEqual(response['Location'], '/awesome/url/')
Example #12
0
    def test_history_form_view_without_getting_history(self):
        request = RequestFactory().post('/')
        request.session = 'session'
        request._messages = FallbackStorage(request)
        request.user = self.user

        poll = Poll.objects.create(question="why?", pub_date=today)
        poll.question = "how?"
        poll.save()
        history = poll.history.all()[0]

        admin_site = AdminSite()
        admin = SimpleHistoryAdmin(Poll, admin_site)

        with patch('simple_history.admin.render') as mock_render:
            admin.history_form_view(request, poll.id, history.pk)

        context = {
            # Verify this is set for original object
            'original': poll,
            'change_history': False,
            'title': 'Revert %s' % force_text(poll),
            'adminform': ANY,
            'object_id': poll.id,
            'is_popup': False,
            'media': ANY,
            'errors': ANY,
            'app_label': 'tests',
            'original_opts': ANY,
            'changelist_url': '/admin/tests/poll/',
            'change_url': ANY,
            'history_url': '/admin/tests/poll/1/history/',
            'add': False,
            'change': True,
            'has_add_permission': admin.has_add_permission(request),
            'has_change_permission':
            admin.has_change_permission(request, poll),
            'has_delete_permission':
            admin.has_delete_permission(request, poll),
            'has_file_field': True,
            'has_absolute_url': False,
            'form_url': '',
            'opts': ANY,
            'content_type_id': ANY,
            'save_as': admin.save_as,
            'save_on_top': admin.save_on_top,
            'root_path': getattr(admin_site, 'root_path', None),
        }
        context.update(admin_site.each_context(request))
        mock_render.assert_called_once_with(request,
                                            admin.object_history_form_template,
                                            context)
    def test_history_form_view_getting_history(self):
        request = RequestFactory().post('/')
        request.session = 'session'
        request._messages = FallbackStorage(request)
        request.user = self.user
        request.POST = {'_change_history': True}

        poll = Poll.objects.create(question="why?", pub_date=today)
        poll.question = "how?"
        poll.save()
        history = poll.history.all()[0]

        admin_site = AdminSite()
        admin = SimpleHistoryAdmin(Poll, admin_site)

        with patch('simple_history.admin.render') as mock_render:
            with patch('simple_history.admin.SIMPLE_HISTORY_EDIT', True):
                admin.history_form_view(request, poll.id, history.pk)

        context = {
            # Verify this is set for history object not poll object
            'original': history.instance,
            'change_history': True,

            'title': 'Revert %s' % force_text(history.instance),
            'adminform': ANY,
            'object_id': poll.id,
            'is_popup': False,
            'media': ANY,
            'errors': ANY,
            'app_label': 'tests',
            'original_opts': ANY,
            'changelist_url': '/admin/tests/poll/',
            'change_url': ANY,
            'history_url': '/admin/tests/poll/{pk}/history/'.format(
                pk=poll.pk),
            'add': False,
            'change': True,
            'has_add_permission': admin.has_add_permission(request),
            'has_change_permission': admin.has_change_permission(
                request, poll),
            'has_delete_permission': admin.has_delete_permission(
                request, poll),
            'has_file_field': True,
            'has_absolute_url': False,
            'form_url': '',
            'opts': ANY,
            'content_type_id': ANY,
            'save_as': admin.save_as,
            'save_on_top': admin.save_on_top,
            'root_path': getattr(admin_site, 'root_path', None),
        }

        mock_render.assert_called_once_with(
            request, template_name=admin.object_history_form_template,
            dictionary=context, current_app=admin_site.name)
    def test_history_form_view_accepts_additional_context(self):
        request = RequestFactory().post("/")
        request.session = "session"
        request._messages = FallbackStorage(request)
        request.user = self.user

        poll = Poll.objects.create(question="why?", pub_date=today)
        poll.question = "how?"
        poll.save()
        history = poll.history.all()[0]

        admin_site = AdminSite()
        admin = SimpleHistoryAdmin(Poll, admin_site)

        with patch("simple_history.admin.render") as mock_render:
            admin.history_form_view(
                request,
                poll.id,
                history.pk,
                extra_context={"anything_else": "will be merged into context"},
            )

        context = {
            # Verify this is set for original object
            "anything_else": "will be merged into context",
            "original": poll,
            "change_history": False,
            "title": "Revert %s" % force_text(poll),
            "adminform": ANY,
            "object_id": poll.id,
            "is_popup": False,
            "media": ANY,
            "errors": ANY,
            "app_label": "tests",
            "original_opts": ANY,
            "changelist_url": "/admin/tests/poll/",
            "change_url": ANY,
            "history_url": "/admin/tests/poll/1/history/",
            "add": False,
            "change": True,
            "has_add_permission": admin.has_add_permission(request),
            "has_change_permission": admin.has_change_permission(request, poll),
            "has_delete_permission": admin.has_delete_permission(request, poll),
            "has_file_field": True,
            "has_absolute_url": False,
            "form_url": "",
            "opts": ANY,
            "content_type_id": ANY,
            "save_as": admin.save_as,
            "save_on_top": admin.save_on_top,
            "root_path": getattr(admin_site, "root_path", None),
        }
        context.update(admin_site.each_context(request))
        mock_render.assert_called_once_with(
            request, admin.object_history_form_template, context
        )
    def test_history_form_view_getting_history_abstract_external(self):
        request = RequestFactory().post("/")
        request.session = "session"
        request._messages = FallbackStorage(request)
        request.user = self.user
        request.POST = {"_change_history": True}

        obj = ConcreteExternal.objects.create(name="test")
        obj.name = "new_test"
        obj.save()
        history = obj.history.all()[0]

        admin_site = AdminSite()
        admin = SimpleHistoryAdmin(ConcreteExternal, admin_site)

        with patch("simple_history.admin.render") as mock_render:
            with patch("simple_history.admin.SIMPLE_HISTORY_EDIT", True):
                admin.history_form_view(request, obj.id, history.pk)

        context = {
            # Verify this is set for history object
            "original": history.instance,
            "change_history": True,
            "title": "Revert %s" % force_text(history.instance),
            "adminform": ANY,
            "object_id": obj.id,
            "is_popup": False,
            "media": ANY,
            "errors": ANY,
            "app_label": "tests",
            "original_opts": ANY,
            "changelist_url": "/admin/tests/concreteexternal/",
            "change_url": ANY,
            "history_url": "/admin/tests/concreteexternal/{pk}/history/".format(
                pk=obj.pk
            ),
            "add": False,
            "change": True,
            "has_add_permission": admin.has_add_permission(request),
            "has_change_permission": admin.has_change_permission(request, obj),
            "has_delete_permission": admin.has_delete_permission(request, obj),
            "has_file_field": True,
            "has_absolute_url": False,
            "form_url": "",
            "opts": ANY,
            "content_type_id": ANY,
            "save_as": admin.save_as,
            "save_on_top": admin.save_on_top,
            "root_path": getattr(admin_site, "root_path", None),
        }
        context.update(admin_site.each_context(request))
        mock_render.assert_called_once_with(
            request, admin.object_history_form_template, context
        )