<NME> load_initial_data.py
<BEF> """A management command to load Modoboa initial data:

* Create a default super admin if none exists
* Create groups and permissions

"""

from functools import reduce

from django.contrib.auth.models import Group
from django.core.management.base import BaseCommand

from modoboa.lib.permissions import add_permissions_to_group
from ... import constants, extensions, models, signals


class Command(BaseCommand):
    """Command definition."""

    help = "Load Modoboa initial data"  # NOQA:A003

    def add_arguments(self, parser):
        """Add extra arguments to command."""
        parser.add_argument(
            "--admin-username", default="admin",
            help="Username of the initial super administrator."
        )
        parser.add_argument(
            "--extra-fixtures", action="store_true", default=False,
            help="Also load some fixtures from the admin application."
        )

    def handle(self, *args, **options):
        """Command entry point."""
        extensions.exts_pool.load_all()

        if not models.User.objects.filter(is_superuser=True).count():
            admin = models.User(
                username=options["admin_username"], is_superuser=True)

    def handle(self, *args, **options):
        """Command entry point."""
        if not models.User.objects.filter(is_superuser=True).count():
            admin = models.User(
                username=options["admin_username"], is_superuser=True)
            results = signals.extra_role_permissions.send(
                sender=self.__class__, role=groupname)
            permissions = constants.PERMISSIONS.get(groupname, [])
            if results:
                permissions += reduce(
                    lambda a, b: a + b, [result[1] for result in results])
            if not permissions:
                continue
            add_permissions_to_group(group, permissions)

        for extname in list(extensions.exts_pool.extensions.keys()):
            extension = extensions.exts_pool.get_extension(extname)
            try:
        for service_name in ["relay", "smtp"]:
            relay_models.Service.objects.get_or_create(name=service_name)

        extensions.exts_pool.load_all()

        groups = list(constants.PERMISSIONS.keys())
        for groupname in groups:
            group, created = Group.objects.get_or_create(name=groupname)
                )
            else:
                signals.initial_data_loaded.send(
                    sender=self.__class__, extname=extname
                )

        if options["extra_fixtures"]:
            from modoboa.admin import factories
            factories.populate_database()

<MSG> Load extensions before doing anything else.

<DFF> @@ -40,6 +40,8 @@ class Command(BaseCommand):
 
     def handle(self, *args, **options):
         """Command entry point."""
+        extensions.exts_pool.load_all()
+
         if not models.User.objects.filter(is_superuser=True).count():
             admin = models.User(
                 username=options["admin_username"], is_superuser=True)
@@ -59,8 +61,6 @@ class Command(BaseCommand):
         for service_name in ["relay", "smtp"]:
             relay_models.Service.objects.get_or_create(name=service_name)
 
-        extensions.exts_pool.load_all()
-
         groups = list(constants.PERMISSIONS.keys())
         for groupname in groups:
             group, created = Group.objects.get_or_create(name=groupname)

Load extensions before doing anything else.