Skip to content

Commit

Permalink
Most recent with excluded fields (#477)
Browse files Browse the repository at this point in the history
* Added changes in manager to compensate for excluded fields while feetching most recent. Updated test_model_with_excluded_fields test to verify.

* Requirement changes.

* hot fix

* Formatted test_models file

* Refactored exclude fields to another method.

* Requested changes for excluded fields

* Getting history excluded fields attribute if it exists

* changed nested ifs
  • Loading branch information
Alig1493 authored and Ross Mechanic committed Dec 24, 2018
1 parent 507fa7d commit 65785a0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
7 changes: 7 additions & 0 deletions simple_history/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ def get_queryset(self):
key_name = self.instance._meta.pk.name
return self.get_super_queryset().filter(**{key_name: self.instance.pk})

def get_excluded_fields(self):
return getattr(self.model, "_history_excluded_fields", [])

def most_recent(self):
"""
Returns the most recent copy of the instance available in the history.
Expand All @@ -45,7 +48,11 @@ def most_recent(self):
)
)
tmp = []
excluded_fields = self.get_excluded_fields()

for field in self.instance._meta.fields:
if field.name in excluded_fields:
continue
if isinstance(field, models.ForeignKey):
tmp.append(field.name + "_id")
else:
Expand Down
6 changes: 6 additions & 0 deletions simple_history/tests/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,12 @@ def test_model_with_excluded_fields(self):
self.assertIn("question", all_fields_names)
self.assertNotIn("pub_date", all_fields_names)

most_recent = p.history.most_recent()
self.assertIn("question", all_fields_names)
self.assertNotIn("pub_date", all_fields_names)
self.assertEqual(most_recent.__class__, PollWithExcludeFields)
self.assertIn("pub_date", history._history_excluded_fields)

def test_user_model_override(self):
user1 = User.objects.create_user("user1", "1@example.com")
user2 = User.objects.create_user("user2", "1@example.com")
Expand Down

0 comments on commit 65785a0

Please sign in to comment.