From 61e0072f4eee78473c4ff0e7eaa847dddb46db87 Mon Sep 17 00:00:00 2001 From: Filipe Pina Date: Fri, 7 Dec 2018 22:57:33 +0000 Subject: [PATCH] codeclimate --- .../commands/clean_duplicate_history.py | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/simple_history/management/commands/clean_duplicate_history.py b/simple_history/management/commands/clean_duplicate_history.py index 8ced4f543..2cc563b81 100644 --- a/simple_history/management/commands/clean_duplicate_history.py +++ b/simple_history/management/commands/clean_duplicate_history.py @@ -49,11 +49,11 @@ def handle(self, *args, **options): except NotHistoricalModelError: continue to_process.add((model, history_model)) - if not to_process and self.verbosity >= 1: - self.stdout.write(self.NO_REGISTERED_MODELS) + if not to_process: + self.log(self.NO_REGISTERED_MODELS) - elif self.verbosity >= 1: - self.stdout.write(self.COMMAND_HINT) + else: + self.log(self.COMMAND_HINT) self._process(to_process, date_back=options["minutes"], dry_run=options["dry"]) @@ -68,8 +68,7 @@ def _process(self, to_process, date_back=None, dry_run=True): if stop_date: m_qs = m_qs.filter(history_date__gte=stop_date) found = m_qs.count() - if self.verbosity >= 2: - self.stdout.write("{0} has {1} historical entries".format(model, found)) + self.log("{0} has {1} historical entries".format(model, found), 2) if not found: continue @@ -100,10 +99,14 @@ def _process_instance(self, instance, model, stop_date=None, dry_run=True): f1 = f2 if extra_one: entries_deleted += self._check_and_delete(f1, extra_one, dry_run) - if self.verbosity >= 1: - self.stdout.write( - self.DONE_CLEANING_FOR_MODEL.format(model=model, count=entries_deleted) - ) + + self.log( + self.DONE_CLEANING_FOR_MODEL.format(model=model, count=entries_deleted) + ) + + def log(self, message, verbosity_level=1): + if self.verbosity >= verbosity_level: + self.stdout.write(message) def _check_and_delete(self, entry1, entry2, dry_run=True): delta = entry1.diff_against(entry2)