Skip to content

Commit

Permalink
Adding verbose option for more explicit output on get_dirty_fields, p…
Browse files Browse the repository at this point in the history
…roposition for #59
  • Loading branch information
romgar committed Apr 13, 2016
1 parent 928e548 commit 60f41d7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/dirtyfields/dirtyfields.py
Expand Up @@ -54,7 +54,7 @@ def _as_dict(self, check_relationship):

return all_field

def get_dirty_fields(self, check_relationship=False):
def get_dirty_fields(self, check_relationship=False, verbose=False):
# check_relationship indicates whether we want to check for foreign keys
# and one-to-one fields or ignore them
new_state = self._as_dict(check_relationship)
Expand All @@ -64,7 +64,12 @@ def get_dirty_fields(self, check_relationship=False):
original_value = self._original_state[key]

is_identical = self.compare_function[0](value, original_value, **self.compare_function[1])
if not is_identical:
if is_identical:
continue

if verbose:
all_modify_field[key] = {'saved': original_value, 'current': value}
else:
all_modify_field[key] = original_value

return all_modify_field
Expand Down
15 changes: 15 additions & 0 deletions tests/test_core.py
Expand Up @@ -127,3 +127,18 @@ def test_validationerror():

tm.boolean = False
assert tm.get_dirty_fields() == {'boolean': None}


def test_verbose_mode():
tm = TestModel()

# initial state is dirty because it has not been saved yet in the db
assert tm.is_dirty()
assert tm.get_dirty_fields() == {}

# changing values should flag them as dirty
tm.boolean = False

assert tm.get_dirty_fields(verbose=True) == {
'boolean': {'saved': True, 'current': False}
}

0 comments on commit 60f41d7

Please sign in to comment.