<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 modoboa.core import load_core_settings, PERMISSIONS
from modoboa.core.extensions import exts_pool
from modoboa.core.models import User, ObjectAccess
from modoboa.lib import events
from modoboa.lib.permissions import add_permissions_to_group
from . import CloseConnectionMixin

    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."""
            ObjectAccess.objects.create(
                user=admin, content_object=admin, is_owner=True)

        exts_pool.load_all()

        superadmin = User.objects.filter(is_superuser=True).first()
            group, created = Group.objects.get_or_create(name=groupname)
            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:
                extension.load_initial_data()
            except Exception as e:
                self.stderr.write(
                    "Unable to load initial data for '{}' ({}).".format(
                        extname, str(e)
                    )
                )
            else:
                signals.initial_data_loaded.send(
                    sender=self.__class__, extname=extname
                )

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

<MSG> Set a default value for the secret key.

see #769

<DFF> @@ -11,7 +11,9 @@ from django.core.management.base import BaseCommand
 from modoboa.core import load_core_settings, PERMISSIONS
 from modoboa.core.extensions import exts_pool
 from modoboa.core.models import User, ObjectAccess
+from modoboa.lib.cryptutils import random_key
 from modoboa.lib import events
+from modoboa.lib import models as lib_models
 from modoboa.lib.permissions import add_permissions_to_group
 from . import CloseConnectionMixin
 
@@ -33,6 +35,12 @@ class Command(BaseCommand, CloseConnectionMixin):
             ObjectAccess.objects.create(
                 user=admin, content_object=admin, is_owner=True)
 
+        param_name = "core.SECRET_KEY"
+        qset = lib_models.Parameter.objects.filter(name=param_name)
+        if not qset.exists():
+            lib_models.Parameter.objects.create(
+                name=param_name, value=random_key())
+
         exts_pool.load_all()
 
         superadmin = User.objects.filter(is_superuser=True).first()

Set a default value for the secret key.