<NME> _repair.py
<BEF> """Management command to check and fix known problems."""

from django.conf import settings
from django.core.management.base import BaseCommand
from django.utils.encoding import smart_str

from modoboa.core.models import User
from modoboa.admin import models

known_problems = []



def log(message, quiet=False, **options):
    if not quiet:
        print(message)


def fix_owner(qs, dry_run=False, **options):
    """Fix ownership for orphan objects."""
    model = qs.model
    for obj in qs:
        kw = {"cls": model.__name__, "obj": obj}
        if get_object_owner(obj) is not None:
            continue
        if dry_run:
            log("  {cls} {obj} has no owner".format(**kw), **options)
            continue
        if isinstance(obj, User):
            admin = User.objects.filter(
                is_superuser=True, is_active=True).first()
        elif isinstance(obj, models.Domain):
        """Command entry point."""
        for problem in known_problems:
            func = getattr(self, problem)
            title = func.__name__.capitalize().replace("_", " ")
            self.log("", **options)
            self.log("Checking for... {}...".format(title), **options)
            func(**options)
            admin = User.objects.filter(
                is_superuser=True, is_active=True).first()
        grant_access_to_object(admin, obj, is_owner=True)
        kw["admin"] = admin
        log("  {cls} {obj} is now owned by {admin}".format(**kw),
            **options)


@known_problem
def sometimes_objects_have_no_owner(**options):
    """Sometime objects have no owner."""
    owned_models = (
        User.objects.all(),
        models.Domain.objects.all(),
        models.DomainAlias.objects.all(),
        models.Alias.objects.filter(domain__isnull=False),
        models.Mailbox.objects.filter(domain__isnull=False),
    )
    for qs in owned_models:
        fix_owner(qs, **options)


@known_problem
def sometimes_mailbox_have_no_alias(**options):
    """Sometime mailboxes have no alias."""
    alias_created = 0
    recipient_created = 0
    for instance in models.Mailbox.objects.select_related("domain").all():
        alias, created = models.Alias.objects.get_or_create(
            address=instance.full_address,
            domain=instance.domain,
            internal=True)
        if created:
            alias_created += 1
            log("Alias {0} created".format(alias), **options)
        recipient, created = models.AliasRecipient.objects.get_or_create(

    @known_problem
    def sometimes_objects_have_no_owner(self, **options):
        owned_models = (
            User.objects.all(),
            models.Domain.objects.all(),
        log("{0} alias created. {1} alias recipient created".format(
            alias_created, recipient_created), **options)

        )
        for qs in owned_models:
            self.fix_owner(qs, **options)

<MSG> Merge pull request #1042 from bearstech/master

some moar fixes (unwanted autoreplies recipients / missing aliases)
<DFF> @@ -7,6 +7,12 @@ from modoboa.lib.permissions import get_object_owner
 from modoboa.core.models import User
 from modoboa.admin import models
 
+try:
+    from modoboa_postfix_autoreply.models import ARmessage
+except ImportError:
+    ARmessage = None
+
+
 known_problems = []
 
 
@@ -33,7 +39,7 @@ class Repair(BaseCommand):
         """Command entry point."""
         for problem in known_problems:
             func = getattr(self, problem)
-            title = func.__name__.capitalize().replace("_", " ")
+            title = func.__doc__.strip()
             self.log("", **options)
             self.log("Checking for... {}...".format(title), **options)
             func(**options)
@@ -76,6 +82,7 @@ class Repair(BaseCommand):
 
     @known_problem
     def sometimes_objects_have_no_owner(self, **options):
+        """Sometime objects have no owner"""
         owned_models = (
             User.objects.all(),
             models.Domain.objects.all(),
@@ -85,3 +92,52 @@ class Repair(BaseCommand):
         )
         for qs in owned_models:
             self.fix_owner(qs, **options)
+
+    @known_problem
+    def ensure_autoreplies_recipents_are_valids(self, **options):
+        """Sometime autoreply alias exists when ARmessage is not enabled"""
+        if ARmessage is None:
+            return
+        deleted = 0
+        qs = models.AliasRecipient.objects.filter(
+            address__contains='@autoreply.')
+        for alias in qs:
+            address, domain = alias.alias.address.split('@')
+            arqs = ARmessage.objects.filter(
+                mbox__address=address,
+                mbox__domain__name=domain)
+            if not arqs.count():
+                self.log('Delete {0} (No AR found)'.format(alias))
+                deleted += 1
+            else:
+                for ar in arqs:
+                    if not ar.enabled:
+                        self.log('Delete {0} (AR disabled)'.format(alias))
+                        alias.delete()
+                        deleted += 1
+        if deleted:
+            self.log('{0} alias recipient deleted'.format(deleted))
+
+    @known_problem
+    def sometimes_mailbox_have_no_alias(self, **options):
+        """Sometime mailboxes have no alias"""
+        alias_created = 0
+        recipient_created = 0
+        for instance in models.Mailbox.objects.all():
+            alias, created = models.Alias.objects.get_or_create(
+                address=instance.full_address,
+                domain=instance.domain,
+                internal=True)
+            if created:
+                alias_created += 1
+                self.log('Alias {0} created'.format(alias))
+            recipient, created = models.AliasRecipient.objects.get_or_create(
+                alias=alias,
+                address=instance.full_address,
+                r_mailbox=instance)
+            if created:
+                recipient_created += 1
+                self.log('AliasRecipient {0} created'.format(recipient))
+        if alias_created or recipient_created:
+            self.log('{0} alias created. {1} alias recipient created'.format(
+                alias_created, recipient_created))

Merge pull request #1042 from bearstech/master