Repository: modoboa/modoboa

Issue:
cleanlogs command is broken with 1.7.x
# Impacted versions

* Modoboa: 1.7.x

# Steps to reproduce
Run `manage.py cleanlogs`

# Current behavior
```
python /var/modoboa/mailadmin/manage.py cleanlogs
Traceback (most recent call last):
  File "/var/modoboa/mailadmin/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 208, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 40, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/usr/local/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/usr/local/lib/python2.7/site-packages/modoboa/core/management/commands/cleanlogs.py", line 11, in <module>
    from ... import parameters
ImportError: cannot import name parameters
```
# Expected behavior
Should not crash




Discussion:

Resolving patch:
diff --git a/modoboa/core/factories.py b/modoboa/core/factories.py
--- a/modoboa/core/factories.py
+++ b/modoboa/core/factories.py
@@ -50,3 +50,14 @@ def groups(self, create, extracted, **kwargs):
         if extracted:
             for group in extracted:
                 self.groups.add(Group.objects.get(name=group))
+
+
+class LogFactory(factory.DjangoModelFactory):
+    """Factory for Log."""
+
+    class Meta:
+        model = models.Log
+
+    logger = "modoboa.admin"
+    message = "A message"
+    level = "info"
diff --git a/modoboa/core/management/commands/cleanlogs.py b/modoboa/core/management/commands/cleanlogs.py
--- a/modoboa/core/management/commands/cleanlogs.py
+++ b/modoboa/core/management/commands/cleanlogs.py
@@ -7,12 +7,12 @@
 from django.utils import timezone
 
 from modoboa.core.models import Log
-
-from ... import parameters
+from modoboa.parameters import tools as param_tools
 
 
 class Command(BaseCommand):
-    args = ""
+    """Command class."""
+
     help = "Log table cleanup"
 
     def add_arguments(self, parser):
@@ -36,7 +36,7 @@ def handle(self, *args, **options):
             l.addHandler(logging.StreamHandler())
         self.verbose = options["verbose"]
 
-        log_maximum_age = parameters.get_global_parameter("log_maximum_age")
+        log_maximum_age = param_tools.get_global_parameter("log_maximum_age")
         self.__vprint("Deleting logs older than %d days..." % log_maximum_age)
         limit = timezone.now() - datetime.timedelta(log_maximum_age)
         Log.objects.filter(date_created__lt=limit).delete()


Test patch:
diff --git a/modoboa/core/tests/test_core.py b/modoboa/core/tests/test_core.py
--- a/modoboa/core/tests/test_core.py
+++ b/modoboa/core/tests/test_core.py
@@ -2,6 +2,8 @@
 
 import httmock
 
+from dateutil.relativedelta import relativedelta
+
 from django.core import management
 from django.core.urlresolvers import reverse
 from django.test import TestCase
@@ -42,8 +44,8 @@ def test_authentication(self):
         self.assertTrue(response.url.endswith(reverse("core:dashboard")))
 
 
-class LoadInitialDataTestCase(TestCase):
-    """Try to change the default username."""
+class ManagementCommandsTestCase(TestCase):
+    """Test management commands."""
 
     def test_change_default_admin(self):
         """Use dedicated option."""
@@ -59,6 +61,15 @@ def test_set_secret_key(self):
         self.assertIn("core", lc._parameters)
         self.assertIn("secret_key", lc._parameters["core"])
 
+    def test_clean_logs(self):
+        """Run cleanlogs command."""
+        log1 = factories.LogFactory()
+        factories.LogFactory()
+        log1.date_created -= relativedelta(years=2)
+        log1.save(update_fields=["date_created"])
+        management.call_command("cleanlogs")
+        self.assertEqual(models.Log.objects.count(), 1)
+
 
 class ProfileTestCase(ModoTestCase):
     """Profile related tests."""