Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR for #57 and #58 #59

Merged
merged 23 commits into from
Feb 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ python:

env:
- DJANGO='1.8' REVERSION='1.9' EXTRA=''
- DJANGO='1.8' REVERSION='1.10' EXTRA=''
- DJANGO='1.9' REVERSION='1.10' EXTRA=''
- DJANGO='1.8' REVERSION='1.9' EXTRA='diff-match-patch'
- DJANGO='1.8' REVERSION='1.10' EXTRA='diff-match-patch'
- DJANGO='1.9' REVERSION='1.10' EXTRA='diff-match-patch'

install:
Expand All @@ -31,4 +33,4 @@ after_success:
- coveralls

notifications:
irc: "irc.freenode.org#pylucid"
irc: "irc.freenode.org#pylucid"
14 changes: 12 additions & 2 deletions reversion_compare/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ def __eq__(self, other):

if self.value != other.value:
return False

if not hasattr(self.field, 'get_internal_type') or self.field.get_internal_type() == "ForeignKey": # FIXME!

# see - https://hynek.me/articles/hasattr/
internal_type = getattr(self.field,'get_internal_type',None)
if internal_type is None or internal_type() == "ForeignKey": # FIXME!
if self.version.field_dict != other.version.field_dict:
return False

Expand All @@ -98,6 +100,14 @@ def get_reverse_foreign_key(self):
ids = []
else:
ids = [v.id for v in getattr(obj, str(self.field.related_name)).all()] # is: version.field_dict[field.name]
if ids == [] and any([f.name.endswith('_ptr') for f in obj._meta.fields]):
# If there is a _ptr this is a multiinheritance table and inherits from a non-abstract class
# lets try and get the parent items associated entries for this field
others = self.version.revision.version_set.filter(object_id=self.version.object_id)
for p in others:
p_obj = p.object_version.object
if type(p_obj) != type(obj) and hasattr(p_obj,str(self.field.related_name)):
ids = [v.id for v in getattr(p_obj, str(self.field.related_name)).all()]

else:
return ([], [], [], []) # TODO: refactory that
Expand Down
5 changes: 3 additions & 2 deletions reversion_compare/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,11 @@ def compare_DateTimeField(self, obj_compare):
return render_to_string("reversion-compare/compare_DateTimeField.html", context)

def compare_BooleanField(self, obj_compare):
''' compare all model BooleanField field '''
''' compare booleans as a complete field, rather than as a string '''
context = {
"bool1": obj_compare.value1,
"bool2": obj_compare.value2,
}
return render_to_string("reversion-compare/compare_BooleanField.html", context)
compare_NullBooleanField=compare_BooleanField

compare_NullBooleanField = compare_BooleanField
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

<p class="highlight">
<del>{{ bool1 }}</del> {% trans 'changed to:' %} <ins>{{ bool2 }}</ins>
</p>
</p>
2 changes: 1 addition & 1 deletion reversion_compare/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,4 @@ def get_context_data(self, **kwargs):
"comparable": comparable,
"compare_view": True,
})
return context
return context
10 changes: 9 additions & 1 deletion tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ def __str__(self):
"""

@python_2_unicode_compatible
class Factory(models.Model):
class Building(models.Model):
address = models.CharField(max_length=128)
def __str__(self):
return self.address

@python_2_unicode_compatible
class Factory(Building):
name = models.CharField(max_length=128)
def __str__(self):
return self.name
Expand All @@ -67,6 +73,8 @@ def __str__(self):
class Person(models.Model):
name = models.CharField(max_length=100)
pets = models.ManyToManyField(Pet, blank=True)
# If you work someone, its at a build, but maybe not a factory!
workplace = models.ForeignKey(Building, related_name="workers", null=True)
def __str__(self):
return self.name

Expand Down
6 changes: 3 additions & 3 deletions tests/test_factory_car_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_initial_state(self):

self.assertEqual(reversion_api.Revision.objects.all().count(), 3)
self.assertEqual(len(self.version_ids), 3)
self.assertEqual(reversion_api.Version.objects.all().count(), 10)
self.assertEqual(reversion_api.Version.objects.all().count(), 17)

def test_select_compare(self):
response = self.client.get("/admin/tests/car/%s/history/" % self.car.pk)
Expand Down Expand Up @@ -153,14 +153,14 @@ def test_deleted_objects(self):
https://travis-ci.org/jedie/django-reversion-compare/builds/72317520
"""
with reversion_api.create_revision():
factory1 = Factory.objects.create(name="factory one")
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")
factory2 = Factory.objects.create(name="factory two", address="1 Fake Plaza")
car.manufacturer=factory2
car.save()

Expand Down
16 changes: 12 additions & 4 deletions tests/test_factory_car_reverse_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

from reversion_compare import reversion_api

from .models import Factory, Car
from .models import Factory, Car, Person
from .test_utils.test_cases import BaseTestCase
from .test_utils.test_data import TestData

Expand All @@ -45,10 +45,12 @@ class FactoryCarReverseRelationModelTest(BaseTestCase):
so no relation data would be stored
"""
def setUp(self):
reversion_api.unregister(Person)
reversion_api.unregister(Car)
reversion_api.unregister(Factory)
reversion_api.register(Factory, follow=["cars"])
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)
Expand All @@ -61,7 +63,7 @@ def test_initial_state(self):
self.assertTrue(reversion_api.is_registered(Car))
self.assertEqual(reversion_api.Revision.objects.all().count(), 3)
self.assertEqual(len(self.version_ids), 3)
self.assertEqual(reversion_api.Version.objects.all().count(), 13)
self.assertEqual(reversion_api.Version.objects.all().count(), 19)

def test_select_compare(self):
response = self.client.get("/admin/tests/factory/%s/history/" % self.factory.pk)
Expand Down Expand Up @@ -92,5 +94,11 @@ def test_diff1(self):
motor-car one from factory one supplier(s): <br />
</p>
''',
'<blockquote>version 2: discontinued car-three, add car-four</blockquote>', # edit comment
'''
<p class="highlight">
<ins>+ Bob Bobertson</ins><br />
</p>
''',
'<blockquote>version 2: discontinued car-three, add car-four, add Bob the worker</blockquote>', # edit comment
)

23 changes: 15 additions & 8 deletions tests/test_utils/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ def create_Simple_data(self):

def create_FactoryCar_data(self):
with reversion_api.create_revision():
manufacture = Factory.objects.create(name="factory one")
supplier1 = Factory.objects.create(name="always the same supplier")
supplier2 = Factory.objects.create(name="would be deleted supplier")
supplier3 = Factory.objects.create(name="would be removed supplier")
manufacture = Factory.objects.create(name="factory one", address="1 Fake Plaza")
supplier1 = Factory.objects.create(name="always the same supplier", address="1 Fake Plaza")
supplier2 = Factory.objects.create(name="would be deleted supplier", address="1 Fake Plaza")
supplier3 = Factory.objects.create(name="would be removed supplier", address="1 Fake Plaza")
car = Car.objects.create(
name="motor-car one",
manufacturer=manufacture
Expand Down Expand Up @@ -138,7 +138,7 @@ def create_FactoryCar_data(self):
manufacture.name = "factory I"
manufacture.save()
supplier2.delete() # - would be deleted supplier
supplier4 = Factory.objects.create(name="new, would be renamed supplier")
supplier4 = Factory.objects.create(name="new, would be renamed supplier", address="1 Fake Plaza")
car.supplier.add(supplier4) # + new, would be renamed supplier
car.supplier.remove(supplier3) # - would be removed supplier
car.save()
Expand Down Expand Up @@ -179,8 +179,8 @@ def create_Factory_reverse_relation_data(self):
from django.db import transaction

with transaction.atomic(), reversion_api.create_revision():
manufacturer = Factory.objects.create(name="factory one")
different_manufacturer = Factory.objects.create(name="factory two")
manufacturer = Factory.objects.create(name="factory one", address="1 Fake Plaza")
different_manufacturer = Factory.objects.create(name="factory two", address="1 Fake Plaza")
car1 = Car.objects.create(
name="motor-car one",
manufacturer=manufacturer
Expand Down Expand Up @@ -222,8 +222,14 @@ def create_Factory_reverse_relation_data(self):
manufacturer=manufacturer
)
car4.save()

worker1 = Person.objects.create(
name="Bob Bobertson",
workplace=manufacturer
)

manufacturer.save()
reversion_api.set_comment("version 2: discontinued car-three, add car-four")
reversion_api.set_comment("version 2: discontinued car-three, add car-four, add Bob the worker")

if self.verbose:
print("version 2:", manufacturer)
Expand Down Expand Up @@ -331,6 +337,7 @@ def create_VariantModel_data(self):

test_data = (
("boolean", True),
("null_boolean", True),
("null_boolean", False),
("char", "B"),
("text", "Bar 'two'"),
Expand Down
1 change: 0 additions & 1 deletion tests/test_variant_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,3 @@ def test_all_changes(self):
"<del>- 192.168.0.1</del>",
"<ins>+ 10.0.0.0</ins>",
)