Beispiel #1
0
    def setUp(self):
        super(CBViewTest, self).setUp()
        test_data = TestData(verbose=False)
        self.item1, self.item2 = test_data.create_Simple_data()

        queryset = reversion_api.get_for_object(self.item1)
        self.version_ids1 = queryset.values_list("pk", flat=True)
        
        queryset = reversion_api.get_for_object(self.item2)
        self.version_ids2 = queryset.values_list("pk", flat=True)
    def setUp(self):
        super(CBViewTest, self).setUp()
        test_data = TestData(verbose=False)
        self.item1, self.item2 = test_data.create_Simple_data()

        queryset = reversion_api.get_for_object(self.item1)
        self.version_ids1 = queryset.values_list("pk", flat=True)
        
        queryset = reversion_api.get_for_object(self.item2)
        self.version_ids2 = queryset.values_list("pk", flat=True)
    def test_initial_state(self):
        self.assertTrue(reversion_api.is_registered(SimpleModel))

        self.assertEqual(SimpleModel.objects.count(), 2)
        self.assertEqual(SimpleModel.objects.all()[0].text, "version two")

        self.assertEqual(reversion_api.get_for_object(self.item1).count(), 2)
        self.assertEqual(reversion_api.get_for_object(self.item2).count(), 5)
        self.assertEqual(reversion_api.Revision.objects.all().count(), 7)
        self.assertEqual(reversion_api.Version.objects.all().count(), 7)

        # query.ValuesListQuerySet() -> list():
        self.assertEqual(list(self.version_ids1), [2, 1])
        self.assertEqual(list(self.version_ids2), [7, 6, 5, 4, 3])
Beispiel #4
0
    def test_deleted_objects(self):
        """
        for:
        https://github.com/jedie/django-reversion-compare/commit/ba22008130f4c16a32eeb900381c2d82ca6fdd9e
        https://travis-ci.org/jedie/django-reversion-compare/builds/72317520
        """
        with reversion_api.create_revision():
            factory1 = Factory.objects.create(name="factory one",
                                              address="1 Fake Plaza")
            car = Car.objects.create(name="car", manufacturer=factory1)

        with reversion_api.create_revision():
            factory2 = Factory.objects.create(name="factory two",
                                              address="1 Fake Plaza")
            car.manufacturer = factory2
            car.save()

        with reversion_api.create_revision():
            factory1.delete()

        queryset = reversion_api.get_for_object(car)
        version_ids = queryset.values_list("pk", flat=True)  # [3, 2]
        # print("\n", version_ids)

        # response = self.client.get("/admin/tests/car/%s/history/" % car.pk)
        # debug_response(response) # from django-tools
        self.client.get("/admin/tests/car/%s/history/compare/" % car.pk,
                        data={
                            "version_id2": version_ids[0],
                            "version_id1": version_ids[1]
                        })
    def setUp(self):
        super(OneToOneFieldTest, self).setUp()
        test_data = TestData(verbose=False)
        self.person, self.item = test_data.create_PersonIdentity_data()

        queryset = reversion_api.get_for_object(self.person)
        self.version_ids = queryset.values_list("pk", flat=True)
    def test_deleted_objects(self):
        """
        for:
        https://github.com/jedie/django-reversion-compare/commit/ba22008130f4c16a32eeb900381c2d82ca6fdd9e
        https://travis-ci.org/jedie/django-reversion-compare/builds/72317520
        """
        with reversion_api.create_revision():
            factory1 = Factory.objects.create(name="factory one", address="1 Fake Plaza")
            car = Car.objects.create(
                name="car",
                manufacturer=factory1
            )

        with reversion_api.create_revision():
            factory2 = Factory.objects.create(name="factory two", address="1 Fake Plaza")
            car.manufacturer=factory2
            car.save()

        with reversion_api.create_revision():
            factory1.delete()

        queryset = reversion_api.get_for_object(car)
        version_ids = queryset.values_list("pk", flat=True)  # [3, 2]
        # print("\n", version_ids)

        # response = self.client.get("/admin/tests/car/%s/history/" % car.pk)
        # debug_response(response) # from django-tools

        response = self.client.get(
            "/admin/tests/car/%s/history/compare/" % car.pk,
            data={"version_id2":version_ids[0], "version_id1":version_ids[1]}
        )
    def setUp(self):
        super(VariantModelWithDataTest, self).setUp()

        self.item, self.test_data = TestData(verbose=False).create_VariantModel_data()

        queryset = reversion_api.get_for_object(self.item)
        self.version_ids = queryset.values_list("pk", flat=True)
 def _get_action_list(self, ):
     action_list = [{
         "version": version,
         "revision": version.revision,
     } for version in self._order_version_queryset(
         reversion_api.get_for_object(self.get_object(), ).select_related(
             "revision__user"))]
     return action_list
Beispiel #9
0
    def test_initial_state(self):
        self.assertTrue(reversion_api.is_registered(Pet))
        self.assertTrue(reversion_api.is_registered(Person))

        self.assertEqual(Pet.objects.count(), 3)

        self.assertEqual(reversion_api.get_for_object(self.pet1).count(), 2)
        self.assertEqual(reversion_api.Revision.objects.all().count(), 2)
    def setUp(self):
        super(VariantModelWithDataTest, self).setUp()

        self.item, self.test_data = TestData(
            verbose=False).create_VariantModel_data()

        queryset = reversion_api.get_for_object(self.item)
        self.version_ids = queryset.values_list("pk", flat=True)
    def test_initial_state(self):
        self.assertTrue(reversion_api.is_registered(Pet))
        self.assertTrue(reversion_api.is_registered(Person))

        self.assertEqual(Pet.objects.count(), 3)

        self.assertEqual(reversion_api.get_for_object(self.pet1).count(), 2)
        self.assertEqual(reversion_api.Revision.objects.all().count(), 2)
Beispiel #12
0
    def test_add_m2m(self):
        with reversion_api.create_revision():
            new_pet = Pet.objects.create(name="added pet")
            self.person.pets.add(new_pet)
            self.person.save()
            reversion_api.set_comment("version 3: add a pet")

        self.assertEqual(reversion_api.Revision.objects.all().count(), 3)
        self.assertEqual(reversion_api.Version.objects.all().count(), 12)

        queryset = reversion_api.get_for_object(self.person)
        version_ids = queryset.values_list("pk", flat=True)
        self.assertEqual(len(version_ids), 3)

        response = self.client.get("/admin/tests/person/%s/history/" %
                                   self.person.pk)
        #        debug_response(response) # from django-tools
        self.assertContainsHtml(
            response,
            '<input type="submit" value="compare">',
            '<input type="radio" name="version_id1" value="%i" style="visibility:hidden" />'
            % version_ids[0],
            '<input type="radio" name="version_id2" value="%i" checked="checked" />'
            % version_ids[0],
            '<input type="radio" name="version_id1" value="%i" checked="checked" />'
            % version_ids[1],
            '<input type="radio" name="version_id2" value="%i" />' %
            version_ids[1],
            '<input type="radio" name="version_id2" value="%i" />' %
            version_ids[2],
            '<input type="radio" name="version_id2" value="%i" />' %
            version_ids[2],
        )

        response = self.client.get("/admin/tests/person/%s/history/compare/" %
                                   self.person.pk,
                                   data={
                                       "version_id2": version_ids[0],
                                       "version_id1": version_ids[1]
                                   })
        #        debug_response(response) # from django-tools

        self.assertContainsHtml(
            response,
            """
            <p class="highlight">
                <ins>+ added pet</ins><br />
                Is changed pet<br />
                always the same pet<br />
            </p>
            """,
            "<blockquote>version 3: add a pet</blockquote>",  # edit comment
        )
        self.assertNotContainsHtml(
            response,
            "<h3>name</h3>",  # person name doesn't changed
            'class="follow"'  # All fields are under reversion control
        )
    def setUp(self):
        super(PersonPetModelTest, self).setUp()

        test_data = TestData(verbose=False)
#        test_data = TestData(verbose=True)
        self.pet1, self.pet2, self.person = test_data.create_PersonPet_data()

        queryset = reversion_api.get_for_object(self.person)
        self.version_ids = queryset.values_list("pk", flat=True)
    def setUp(self):
        super(FactoryCarModelTest, self).setUp()

        test_data = TestData(verbose=False)
#        test_data = TestData(verbose=True)
        self.car = test_data.create_FactoryCar_data()

        queryset = reversion_api.get_for_object(self.car)
        self.version_ids = queryset.values_list("pk", flat=True)
Beispiel #15
0
    def setUp(self):
        super(PersonPetModelTest, self).setUp()

        test_data = TestData(verbose=False)
        # test_data = TestData(verbose=True)
        self.pet1, self.pet2, self.person = test_data.create_PersonPet_data()

        queryset = reversion_api.get_for_object(self.person)
        self.version_ids = queryset.values_list("pk", flat=True)
Beispiel #16
0
    def setUp(self):
        super(FactoryCarModelTest, self).setUp()

        test_data = TestData(verbose=False)
        # test_data = TestData(verbose=True)
        self.car = test_data.create_FactoryCar_data()

        queryset = reversion_api.get_for_object(self.car)
        self.version_ids = queryset.values_list("pk", flat=True)
Beispiel #17
0
    def test_m2m_not_changed(self):
        with reversion_api.create_revision():
            self.person.name = "David"
            self.person.save()
            reversion_api.set_comment("version 3: change the name")

        self.assertEqual(reversion_api.Revision.objects.all().count(), 3)
        self.assertEqual(reversion_api.Version.objects.all().count(), 11)

        queryset = reversion_api.get_for_object(self.person)
        version_ids = queryset.values_list("pk", flat=True)
        self.assertEqual(len(version_ids), 3)

        response = self.client.get("/admin/tests/person/%s/history/" %
                                   self.person.pk)
        #        debug_response(response) # from django-tools
        self.assertContainsHtml(
            response,
            '<input type="submit" value="compare">',
            '<input type="radio" name="version_id1" value="%i" style="visibility:hidden" />'
            % version_ids[0],
            '<input type="radio" name="version_id2" value="%i" checked="checked" />'
            % version_ids[0],
            '<input type="radio" name="version_id1" value="%i" checked="checked" />'
            % version_ids[1],
            '<input type="radio" name="version_id2" value="%i" />' %
            version_ids[1],
            '<input type="radio" name="version_id2" value="%i" />' %
            version_ids[2],
            '<input type="radio" name="version_id2" value="%i" />' %
            version_ids[2],
        )

        response = self.client.get("/admin/tests/person/%s/history/compare/" %
                                   self.person.pk,
                                   data={
                                       "version_id2": version_ids[0],
                                       "version_id1": version_ids[1]
                                   })
        #        debug_response(response) # from django-tools

        self.assertContainsHtml(
            response,
            '''
            <p><pre class="highlight">
            <del>- Dave</del>
            <ins>+ David</ins>
            </pre></p>
            ''',
            "<blockquote>version 3: change the name</blockquote>",  # edit comment
        )
        self.assertNotContainsHtml(
            response,
            "pet",
            'class="follow"'  # All fields are under reversion control
        )
    def test_initial_state(self):
        self.assertTrue(reversion_api.is_registered(VariantModel))

        self.assertEqual(VariantModel.objects.count(), 1)

        count = len(self.test_data) + 1  # incl. initial

        self.assertEqual(reversion_api.get_for_object(self.item).count(), count)
        self.assertEqual(reversion_api.Revision.objects.all().count(), count)
        self.assertEqual(len(self.version_ids), count)
    def test_initial_state(self):
        self.assertTrue(reversion_api.is_registered(SimpleModel))

        self.assertEqual(SimpleModel.objects.count(), 2)
        self.assertEqual(SimpleModel.objects.all()[0].text, "version two")

        self.assertEqual(reversion_api.get_for_object(self.item1).count(), 2)
        self.assertEqual(list(self.version_ids1), [2, 1])
        
        self.assertEqual(list(self.version_ids1), [2, 1])
        self.assertEqual(list(self.version_ids2), [7, 6, 5, 4, 3])
    def test_initial_state(self):
        self.assertTrue(reversion_api.is_registered(VariantModel))

        self.assertEqual(VariantModel.objects.count(), 1)

        count = len(self.test_data) + 1  # incl. initial

        self.assertEqual(
            reversion_api.get_for_object(self.item).count(), count)
        self.assertEqual(reversion_api.Revision.objects.all().count(), count)
        self.assertEqual(len(self.version_ids), count)
Beispiel #21
0
    def test_initial_state(self):
        self.assertTrue(reversion_api.is_registered(SimpleModel))

        self.assertEqual(SimpleModel.objects.count(), 2)
        self.assertEqual(SimpleModel.objects.all()[0].text, "version two")

        self.assertEqual(reversion_api.get_for_object(self.item1).count(), 2)
        self.assertEqual(list(self.version_ids1), [2, 1])
        
        self.assertEqual(list(self.version_ids1), [2, 1])
        self.assertEqual(list(self.version_ids2), [7, 6, 5, 4, 3])
 def _get_action_list(self, ):
     action_list = [
         {
             "version": version,
             "revision": version.revision,
         }
         for version
         in self._order_version_queryset(reversion_api.get_for_object(
             self.get_object(),
         ).select_related("revision__user"))
     ]
     return action_list
    def setUp(self):
        reversion_api.unregister(Person)
        reversion_api.unregister(Car)
        reversion_api.unregister(Factory)
        reversion_api.register(Factory, follow=["building_ptr", "cars", "workers"])
        reversion_api.register(Car)
        reversion_api.register(Person, follow=["pets"])
        super(FactoryCarReverseRelationModelTest, self).setUp()

        test_data = TestData(verbose=False)
        self.factory = test_data.create_Factory_reverse_relation_data()
        queryset = reversion_api.get_for_object(self.factory)
        self.version_ids = queryset.values_list("pk", flat=True)
Beispiel #24
0
 def test_text_diff(self):
     """"Generate a new revision and check for a correctly generated diff."""
     with reversion_api.create_revision():
         self.item.text = "version two"
         self.item.save()
     queryset = reversion_api.get_for_object(self.item)
     version_ids = queryset.values_list("pk", flat=True)
     self.assertEqual(len(version_ids), 2)
     url_name = 'admin:%s_%s_compare' % (CustomModel._meta.app_label, CustomModel._meta.model_name)
     diff_url = reverse(url_name, args=(self.item.pk, ))
     data = {"version_id2": version_ids[0], "version_id1": version_ids[1]}
     response = self.client.get(diff_url, data=data)
     self.assertContains(response, "<del>- version one</del>")
     self.assertContains(response, "<ins>+ version two</ins>")
Beispiel #25
0
    def get_many_to_something(self, ids, related_model, is_reverse=False):

        # get instance of reversion.models.Revision():
        # A group of related object versions.
        old_revision = self.version.revision

        # Get a queryset with all related objects.
        versions = old_revision.version_set.filter(
            content_type=ContentType.objects.get_for_model(related_model),
            object_id__in=ids)

        if self.has_int_pk:
            # The primary_keys would be stored in a text field -> convert it to integers
            # This is interesting in different places!
            for version in versions:
                version.object_id = int(version.object_id)

        missing_objects = []
        missing_ids = []

        if self.field_name not in self.adapter.follow:
            # This models was not registered with follow relations
            # Try to fill missing related objects
            target_ids = set(ids)
            actual_ids = set([version.object_id for version in versions])
            missing_ids1 = target_ids.difference(actual_ids)

            # logger.debug(self.field_name, "target: %s - actual: %s - missing: %s" % (target_ids, actual_ids, missing_ids1))
            if missing_ids1:
                missing_objects = related_model.objects.all().filter(
                    pk__in=missing_ids1)
                missing_ids = list(
                    target_ids.difference(
                        set(missing_objects.values_list('pk', flat=True))))

        deleted = []
        if is_reverse:
            true_missing_objects = []
            for o in missing_objects:
                for ver in reversion_api.get_for_object(o):
                    # An object can only be missing if it actually existed prior to this version
                    # Otherwise its a new item
                    if ver.revision.date_created < old_revision.date_created:
                        true_missing_objects.append(o)
            missing_objects = true_missing_objects
            deleted = [
                d for d in reversion_api.get_deleted(related_model)
                if d.revision == old_revision
            ]
        return versions, missing_objects, missing_ids, deleted
    def get_many_to_something(self,
                              target_ids,
                              related_model,
                              is_reverse=False):
        # get instance of reversion.models.Revision():
        # A group of related object versions.
        old_revision = self.version_record.revision

        # Get a queryset with all related objects.
        versions = {
            ver.object_id: ver
            for ver in old_revision.version_set.filter(
                content_type=ContentType.objects.get_for_model(related_model),
                object_id__in=target_ids).all()
        }

        missing_objects_dict = {}
        deleted = []

        if not self.follow:
            # This models was not registered with follow relations
            # Try to fill missing related objects
            potentially_missing_ids = target_ids.difference(
                frozenset(versions))
            # logger.debug(self.field_name, "target: %s - actual: %s - missing: %s" % (target_ids, versions, potentially_missing_ids))
            if potentially_missing_ids:
                missing_objects_dict = {
                    force_text(rel.pk): rel
                    for rel in related_model.objects.filter(
                        pk__in=potentially_missing_ids).iterator()
                }

        if is_reverse:
            missing_objects_dict = {
                ver.object_id: ver
                for o in missing_objects_dict.values()
                for ver in reversion_api.get_for_object(o)
                if ver.revision.date_created < old_revision.date_created
            }

            deleted = [
                d for d in reversion_api.get_deleted(related_model)
                if d.revision == old_revision
            ]

        return versions, missing_objects_dict, deleted
    def test_add_m2m(self):
        with reversion_api.create_revision():
            new_pet = Pet.objects.create(name="added pet")
            self.person.pets.add(new_pet)
            self.person.save()
            reversion_api.set_comment("version 3: add a pet")

        self.assertEqual(reversion_api.Revision.objects.all().count(), 3)
        self.assertEqual(reversion_api.Version.objects.all().count(), 12)

        queryset = reversion_api.get_for_object(self.person)
        version_ids = queryset.values_list("pk", flat=True)
        self.assertEqual(len(version_ids), 3)

        response = self.client.get("/admin/tests/person/%s/history/" % self.person.pk)
#        debug_response(response) # from django-tools
        self.assertContainsHtml(response,
            '<input type="submit" value="compare">',
            '<input type="radio" name="version_id1" value="%i" style="visibility:hidden" />' % version_ids[0],
            '<input type="radio" name="version_id2" value="%i" checked="checked" />' % version_ids[0],
            '<input type="radio" name="version_id1" value="%i" checked="checked" />' % version_ids[1],
            '<input type="radio" name="version_id2" value="%i" />' % version_ids[1],
            '<input type="radio" name="version_id2" value="%i" />' % version_ids[2],
            '<input type="radio" name="version_id2" value="%i" />' % version_ids[2],
        )

        response = self.client.get(
            "/admin/tests/person/%s/history/compare/" % self.person.pk,
            data={"version_id2":version_ids[0], "version_id1":version_ids[1]}
        )
#        debug_response(response) # from django-tools

        self.assertContainsHtml(response,
            """
            <p class="highlight">
                <ins>+ added pet</ins><br />
                Is changed pet<br />
                always the same pet<br />
            </p>
            """,
            "<blockquote>version 3: add a pet</blockquote>", # edit comment
        )
        self.assertNotContainsHtml(response,
            "<h3>name</h3>", # person name doesn't changed
            'class="follow"'# All fields are under reversion control
        )
    def get_many_to_something(self, ids, related_model, is_reverse=False):

        # get instance of reversion.models.Revision():
        # A group of related object versions.
        old_revision = self.version.revision

        # Get a queryset with all related objects.
        versions = old_revision.version_set.filter(
            content_type=ContentType.objects.get_for_model(related_model),
            object_id__in=ids
        )

        if self.has_int_pk:
            # The primary_keys would be stored in a text field -> convert it to integers
            # This is interesting in different places!
            for version in versions:
                version.object_id = int(version.object_id)

        missing_objects = []
        missing_ids = []

        if self.field_name not in self.adapter.follow:
            # This models was not registered with follow relations
            # Try to fill missing related objects
            target_ids = set(ids)
            actual_ids = set([version.object_id for version in versions])
            missing_ids1 = target_ids.difference(actual_ids)

            # logger.debug(self.field_name, "target: %s - actual: %s - missing: %s" % (target_ids, actual_ids, missing_ids1))
            if missing_ids1:
                missing_objects = related_model.objects.all().filter(pk__in=missing_ids1)
                missing_ids = list(target_ids.difference(set(missing_objects.values_list('pk', flat=True))))

        deleted = []
        if is_reverse:
            true_missing_objects = []
            for o in missing_objects:
                for ver in reversion_api.get_for_object(o):
                    # An object can only be missing if it actually existed prior to this version
                    # Otherwise its a new item
                    if ver.revision.date_created < old_revision.date_created:
                        true_missing_objects.append(o)
            missing_objects = true_missing_objects
            deleted = [d for d in reversion_api.get_deleted(related_model) if d.revision == old_revision]
        return versions, missing_objects, missing_ids, deleted
    def test_m2m_not_changed(self):
        with reversion_api.create_revision():
            self.person.name = "David"
            self.person.save()
            reversion_api.set_comment("version 3: change the name")

        self.assertEqual(reversion_api.Revision.objects.all().count(), 3)
        self.assertEqual(reversion_api.Version.objects.all().count(), 11)

        queryset = reversion_api.get_for_object(self.person)
        version_ids = queryset.values_list("pk", flat=True)
        self.assertEqual(len(version_ids), 3)

        response = self.client.get("/admin/tests/person/%s/history/" % self.person.pk)
#        debug_response(response) # from django-tools
        self.assertContainsHtml(response,
            '<input type="submit" value="compare">',
            '<input type="radio" name="version_id1" value="%i" style="visibility:hidden" />' % version_ids[0],
            '<input type="radio" name="version_id2" value="%i" checked="checked" />' % version_ids[0],
            '<input type="radio" name="version_id1" value="%i" checked="checked" />' % version_ids[1],
            '<input type="radio" name="version_id2" value="%i" />' % version_ids[1],
            '<input type="radio" name="version_id2" value="%i" />' % version_ids[2],
            '<input type="radio" name="version_id2" value="%i" />' % version_ids[2],
        )

        response = self.client.get(
            "/admin/tests/person/%s/history/compare/" % self.person.pk,
            data={"version_id2":version_ids[0], "version_id1":version_ids[1]}
        )
#        debug_response(response) # from django-tools

        self.assertContainsHtml(response,
            '''
            <p><pre class="highlight">
            <del>- Dave</del>
            <ins>+ David</ins>
            </pre></p>
            ''',
            "<blockquote>version 3: change the name</blockquote>", # edit comment
        )
        self.assertNotContainsHtml(response,
            "pet",
            'class="follow"'# All fields are under reversion control
        )
    def get_many_to_something(self, target_ids, related_model, is_reverse=False):
        # get instance of reversion.models.Revision():
        # A group of related object versions.
        old_revision = self.version_record.revision

        # Get a queryset with all related objects.
        versions = {
            ver.object_id: ver for ver in old_revision.version_set.filter(
                content_type=ContentType.objects.get_for_model(related_model),
                object_id__in=target_ids
            ).all()
        }

        missing_objects_dict = {}
        deleted = []

        if not self.follow:
            # This models was not registered with follow relations
            # Try to fill missing related objects
            potentially_missing_ids = target_ids.difference(frozenset(versions))
            # logger.debug(self.field_name, "target: %s - actual: %s - missing: %s" % (target_ids, versions, potentially_missing_ids))
            if potentially_missing_ids:
                missing_objects_dict = {
                    force_text(rel.pk): rel
                    for rel in related_model.objects.filter(pk__in=potentially_missing_ids).iterator()
                }

        if is_reverse:
            missing_objects_dict = {
                ver.object_id: ver
                for o in missing_objects_dict.values()
                for ver in reversion_api.get_for_object(o)
                if ver.revision.date_created < old_revision.date_created
            }

            deleted = [d for d in reversion_api.get_deleted(related_model) if d.revision == old_revision]

        return versions, missing_objects_dict, deleted
Beispiel #31
0
 def test_version_selection(self):
     """Generate two revisions and view the version history selection."""
     with reversion_api.create_revision():
         self.item.text = "version two"
         self.item.save()
     with reversion_api.create_revision():
         self.item.text = "version three"
         self.item.save()
     queryset = reversion_api.get_for_object(self.item)
     version_ids = queryset.values_list("pk", flat=True)
     self.assertEqual(len(version_ids), 3)
     url_name = 'admin:%s_%s_history' % (CustomModel._meta.app_label, CustomModel._meta.model_name)
     history_url = reverse(url_name, args=(self.item.pk, ))
     response = self.client.get(history_url)
     self.assertContainsHtml(
         response,
         '<input type="submit" value="compare">',
         '<input type="radio" name="version_id1" value="%i" style="visibility:hidden" />' % version_ids[0],
         '<input type="radio" name="version_id2" value="%i" checked="checked" />' % version_ids[0],
         '<input type="radio" name="version_id1" value="%i" checked="checked" />' % version_ids[1],
         '<input type="radio" name="version_id2" value="%i" />' % version_ids[1],
         '<input type="radio" name="version_id1" value="%i" />' % version_ids[2],
         '<input type="radio" name="version_id2" value="%i" />' % version_ids[2],
     )
    def compare_view(self, request, object_id, extra_context=None):
        """
        compare two versions.
        Used self.make_compare() to create the html diff.
        """
        if self.compare is None:
            raise Http404("Compare view not enabled.")

        form = SelectDiffForm(request.GET)
        if not form.is_valid():
            msg = "Wrong version IDs."
            if settings.DEBUG:
                msg += " (form errors: %s)" % ", ".join(form.errors)
            raise Http404(msg)

        version_id1 = form.cleaned_data["version_id1"]
        version_id2 = form.cleaned_data["version_id2"]

        if version_id1 > version_id2:
            # Compare always the newest one (#2) with the older one (#1)
            version_id1, version_id2 = version_id2, version_id1

        object_id = unquote(object_id)  # Underscores in primary key get quoted to "_5F"
        obj = get_object_or_404(self.model, pk=object_id)
        queryset = reversion_api.get_for_object(obj)
        version1 = get_object_or_404(queryset, pk=version_id1)
        version2 = get_object_or_404(queryset, pk=version_id2)

        next_version = queryset.filter(pk__gt=version_id2).last()
        prev_version = queryset.filter(pk__lt=version_id1).first()

        compare_data, has_unfollowed_fields = self.compare(obj, version1, version2)

        opts = self.model._meta

        context = {
            "opts": opts,
            "app_label": opts.app_label,
            "model_name": capfirst(opts.verbose_name),
            "title": _("Compare %(name)s") % {"name": version1.object_repr},
            "obj": obj,
            "compare_data": compare_data,
            "has_unfollowed_fields": has_unfollowed_fields,
            "version1": version1,
            "version2": version2,
            "changelist_url": reverse("%s:%s_%s_changelist" % (self.admin_site.name, opts.app_label, opts.model_name)),
            "change_url": reverse("%s:%s_%s_change" % (self.admin_site.name, opts.app_label, opts.model_name),
                                  args=(quote(obj.pk),)),
            "original": obj,
            "history_url": reverse("%s:%s_%s_history" % (self.admin_site.name, opts.app_label, opts.model_name),
                                   args=(quote(obj.pk),)),
        }

        # don't use urlencode with dict for generate prev/next-urls
        # Otherwise we can't unitests it!
        if next_version:
            next_url = "?version_id1=%i&version_id2=%i" % (
                version2.id, next_version.id
            )
            context.update({'next_url': next_url})
        if prev_version:
            prev_url = "?version_id1=%i&version_id2=%i" % (
                prev_version.id, version1.id
            )
            context.update({'prev_url': prev_url})

        extra_context = extra_context or {}
        context.update(extra_context)
        return render_to_response(self.compare_template or self._get_template_list("compare.html"),
                                  context, template.RequestContext(request))
    def get_context_data(self, **kwargs):
        context = super(HistoryCompareDetailView, self).get_context_data()
        action_list = self._get_action_list()

        if len(action_list) < 2:
            # Less than two history items aren't enough to compare ;)
            comparable = False
        else:
            comparable = True
            # for pre selecting the compare radio buttons depend on the ordering:
            if self.history_latest_first:
                action_list[0]["first"] = True
                action_list[1]["second"] = True
            else:
                action_list[-1]["first"] = True
                action_list[-2]["second"] = True
            
        if self.request.GET: 
            form = SelectDiffForm(self.request.GET)
            if not form.is_valid():
                msg = "Wrong version IDs."
                raise Http404(msg)

            version_id1 = form.cleaned_data["version_id1"]
            version_id2 = form.cleaned_data["version_id2"]

            if version_id1 > version_id2:
                # Compare always the newest one (#2) with the older one (#1)
                version_id1, version_id2 = version_id2, version_id1

            obj = self.get_object()
            queryset = reversion_api.get_for_object(obj)
            version1 = get_object_or_404(queryset, pk=version_id1)
            version2 = get_object_or_404(queryset, pk=version_id2)

            next_version = queryset.filter(pk__gt=version_id2).last()
            prev_version = queryset.filter(pk__lt=version_id1).first()

            compare_data, has_unfollowed_fields = self.compare(obj, version1, version2)
            
            context.update({
                "compare_data": compare_data,
                "has_unfollowed_fields": has_unfollowed_fields,
                "version1": version1,
                "version2": version2,
            })
            
            if next_version:
                next_url = "?version_id1=%i&version_id2=%i" % (
                    version2.id, next_version.id
                )
                context.update({'next_url': next_url})
            if prev_version:
                prev_url = "?version_id1=%i&version_id2=%i" % (
                    prev_version.id, version1.id
                )
                context.update({'prev_url': prev_url})

        # Compile the context.
        context.update({
            "action_list": action_list,
            "comparable": comparable,
            "compare_view": True,
        })
        return context
    def get_context_data(self, **kwargs):
        context = super(HistoryCompareDetailView, self).get_context_data()
        action_list = self._get_action_list()

        if len(action_list) < 2:
            # Less than two history items aren't enough to compare ;)
            comparable = False
        else:
            comparable = True
            # for pre selecting the compare radio buttons depend on the ordering:
            if self.history_latest_first:
                action_list[0]["first"] = True
                action_list[1]["second"] = True
            else:
                action_list[-1]["first"] = True
                action_list[-2]["second"] = True

        if self.request.GET:
            form = SelectDiffForm(self.request.GET)
            if not form.is_valid():
                msg = "Wrong version IDs."
                raise Http404(msg)

            version_id1 = form.cleaned_data["version_id1"]
            version_id2 = form.cleaned_data["version_id2"]

            if version_id1 > version_id2:
                # Compare always the newest one (#2) with the older one (#1)
                version_id1, version_id2 = version_id2, version_id1

            obj = self.get_object()
            queryset = reversion_api.get_for_object(obj)
            version1 = get_object_or_404(queryset, pk=version_id1)
            version2 = get_object_or_404(queryset, pk=version_id2)

            next_version = queryset.filter(pk__gt=version_id2).last()
            prev_version = queryset.filter(pk__lt=version_id1).first()

            compare_data, has_unfollowed_fields = self.compare(
                obj, version1, version2)

            context.update({
                "compare_data": compare_data,
                "has_unfollowed_fields": has_unfollowed_fields,
                "version1": version1,
                "version2": version2,
            })

            if next_version:
                next_url = "?version_id1=%i&version_id2=%i" % (version2.id,
                                                               next_version.id)
                context.update({'next_url': next_url})
            if prev_version:
                prev_url = "?version_id1=%i&version_id2=%i" % (prev_version.id,
                                                               version1.id)
                context.update({'prev_url': prev_url})

        # Compile the context.
        context.update({
            "action_list": action_list,
            "comparable": comparable,
            "compare_view": True,
        })
        return context
    def compare_view(self, request, object_id, extra_context=None):
        """
        compare two versions.
        Used self.make_compare() to create the html diff.
        """
        if self.compare is None:
            raise Http404("Compare view not enabled.")

        form = SelectDiffForm(request.GET)
        if not form.is_valid():
            msg = "Wrong version IDs."
            if settings.DEBUG:
                msg += " (form errors: %s)" % ", ".join(form.errors)
            raise Http404(msg)

        version_id1 = form.cleaned_data["version_id1"]
        version_id2 = form.cleaned_data["version_id2"]

        if version_id1 > version_id2:
            # Compare always the newest one (#2) with the older one (#1)
            version_id1, version_id2 = version_id2, version_id1

        object_id = unquote(
            object_id)  # Underscores in primary key get quoted to "_5F"
        obj = get_object_or_404(self.model, pk=object_id)
        queryset = reversion_api.get_for_object(obj)
        version1 = get_object_or_404(queryset, pk=version_id1)
        version2 = get_object_or_404(queryset, pk=version_id2)

        next_version = queryset.filter(pk__gt=version_id2).last()
        prev_version = queryset.filter(pk__lt=version_id1).first()

        compare_data, has_unfollowed_fields = self.compare(
            obj, version1, version2)

        opts = self.model._meta

        context = {
            "opts":
            opts,
            "app_label":
            opts.app_label,
            "model_name":
            capfirst(opts.verbose_name),
            "title":
            _("Compare %(name)s") % {
                "name": version1.object_repr
            },
            "obj":
            obj,
            "compare_data":
            compare_data,
            "has_unfollowed_fields":
            has_unfollowed_fields,
            "version1":
            version1,
            "version2":
            version2,
            "changelist_url":
            reverse("%s:%s_%s_changelist" %
                    (self.admin_site.name, opts.app_label, opts.model_name)),
            "change_url":
            reverse("%s:%s_%s_change" %
                    (self.admin_site.name, opts.app_label, opts.model_name),
                    args=(quote(obj.pk), )),
            "original":
            obj,
            "history_url":
            reverse("%s:%s_%s_history" %
                    (self.admin_site.name, opts.app_label, opts.model_name),
                    args=(quote(obj.pk), )),
        }

        # don't use urlencode with dict for generate prev/next-urls
        # Otherwise we can't unitests it!
        if next_version:
            next_url = "?version_id1=%i&version_id2=%i" % (version2.id,
                                                           next_version.id)
            context.update({'next_url': next_url})
        if prev_version:
            prev_url = "?version_id1=%i&version_id2=%i" % (prev_version.id,
                                                           version1.id)
            context.update({'prev_url': prev_url})

        extra_context = extra_context or {}
        context.update(extra_context)
        return render_to_response(
            self.compare_template or self._get_template_list("compare.html"),
            context, template.RequestContext(request))
Beispiel #36
0
 def test_initial_state(self):
     """"Test initial data creation and model registration."""
     self.assertEqual(CustomModel.objects.count(), 1)
     self.assertEqual(reversion_api.get_for_object(self.item).count(), 1)
     self.assertEqual(reversion_api.Revision.objects.all().count(), 1)