mirror of
https://github.com/jwinterm/modoboa.git
synced 2026-11-16 09:34:03 +01:00
WIP: change the way extensions initial data is loaded.
This commit is contained in:
@@ -18,6 +18,7 @@ BASE_EVENTS = [
|
||||
"ExtraAccountActions",
|
||||
"RoleChanged",
|
||||
"GetExtraRoles",
|
||||
"GetExtraRolePermissions",
|
||||
"PasswordChange",
|
||||
"UserCanSetRole",
|
||||
|
||||
@@ -41,6 +42,15 @@ BASE_EVENTS = [
|
||||
"SaveExtraFormFields",
|
||||
]
|
||||
|
||||
PERMISSIONS = {
|
||||
"DomainAdmins": [
|
||||
["core", "user", "add_user"],
|
||||
["core", "user", "change_user"],
|
||||
["core", "user", "delete_user"],
|
||||
],
|
||||
"SimpleUsers": []
|
||||
}
|
||||
|
||||
|
||||
def load_core_settings():
|
||||
"""Load core settings.
|
||||
|
||||
@@ -6,6 +6,7 @@ import sys
|
||||
|
||||
import subprocess
|
||||
|
||||
import django
|
||||
from django.core import management
|
||||
from django.template import Context, Template
|
||||
|
||||
@@ -138,6 +139,7 @@ class DeployCommand(Command):
|
||||
return info
|
||||
|
||||
def handle(self, parsed_args):
|
||||
django.setup()
|
||||
management.call_command(
|
||||
'startproject', parsed_args.name, verbosity=False
|
||||
)
|
||||
@@ -215,17 +217,10 @@ class DeployCommand(Command):
|
||||
os.unlink("%s/settings.pyc" % path)
|
||||
if parsed_args.dbaction == "install":
|
||||
self._exec_django_command(
|
||||
"syncdb", parsed_args.name, '--noinput'
|
||||
)
|
||||
exec_cmd('sed -ri "s|^#(\s+\'south)|\\1|" %s/settings.py' % path)
|
||||
self._exec_django_command(
|
||||
"syncdb", parsed_args.name,
|
||||
"migrate", parsed_args.name, '--noinput'
|
||||
)
|
||||
self._exec_django_command(
|
||||
'migrate', parsed_args.name, '--fake'
|
||||
)
|
||||
self._exec_django_command(
|
||||
"loaddata", parsed_args.name, 'initial_users.json'
|
||||
"load_initial_data", parsed_args.name
|
||||
)
|
||||
elif parsed_args.dbaction == "upgrade":
|
||||
exec_cmd('sed -ri "s|^#(\s+\'south)|\\1|" %s/settings.py' % path)
|
||||
|
||||
@@ -46,7 +46,6 @@ INSTALLED_APPS = (
|
||||
'django.contrib.messages',
|
||||
'django.contrib.sites',
|
||||
'django.contrib.staticfiles',
|
||||
# 'south',
|
||||
'reversion',
|
||||
)
|
||||
|
||||
@@ -150,9 +149,6 @@ STATICFILES_DIRS = (
|
||||
MEDIA_URL = '/media/'
|
||||
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
|
||||
|
||||
# South settings
|
||||
SOUTH_TESTS_MIGRATE = False
|
||||
|
||||
# Modoboa settings
|
||||
#MODOBOA_CUSTOM_LOGO = os.path.join(MEDIA_URL, "custom_logo.png")
|
||||
|
||||
|
||||
@@ -29,6 +29,10 @@ class ModoExtension(object):
|
||||
always_active=self.always_active
|
||||
)
|
||||
|
||||
def load_initial_data(self):
|
||||
"""Declare extension data in this method."""
|
||||
pass
|
||||
|
||||
def load(self):
|
||||
pass
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
"""A management command to load Modoboa initial data:
|
||||
|
||||
* Create a default super admin if none exists
|
||||
* Create groups and permissions
|
||||
|
||||
"""
|
||||
|
||||
from django.contrib.auth.models import Group
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from modoboa.core import 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
|
||||
|
||||
|
||||
class Command(BaseCommand, CloseConnectionMixin):
|
||||
|
||||
"""Command defintion."""
|
||||
|
||||
help = "Load Modoboa initial data"
|
||||
|
||||
def handle(self, *args, **options):
|
||||
"""Command entry point."""
|
||||
|
||||
if not User.objects.filter(pk=1, username="admin").exists():
|
||||
admin = User(username="admin", is_superuser=True)
|
||||
admin.set_password("password")
|
||||
admin.save()
|
||||
ObjectAccess.objects.create(
|
||||
user=admin, content_object=admin, is_owner=True)
|
||||
|
||||
groups = PERMISSIONS.keys() + [
|
||||
role[0] for role in events.raiseQueryEvent("GetExtraRoles")
|
||||
]
|
||||
for groupname in groups:
|
||||
group, created = Group.objects.get_or_create(name=groupname)
|
||||
permissions = (
|
||||
PERMISSIONS.get(groupname, []) +
|
||||
events.raiseQueryEvent("GetExtraRolePermissions", groupname)
|
||||
)
|
||||
if not permissions:
|
||||
continue
|
||||
add_permissions_to_group(group, permissions)
|
||||
|
||||
exts_pool.load_all()
|
||||
for extname in exts_pool.extensions.keys():
|
||||
extension = exts_pool.get_extension(extname)
|
||||
extension.load_initial_data()
|
||||
+1
-1
@@ -7,7 +7,7 @@ from django.db import models, migrations
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('core', '0002_load_initial_data'),
|
||||
('core', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
@@ -1,26 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
def load_initial_data(apps, schema_editor):
|
||||
"""Load initial data."""
|
||||
User = apps.get_model("core", "User")
|
||||
user = User(username="admin", is_superuser=True)
|
||||
user.set_password("password")
|
||||
user.save()
|
||||
|
||||
ObjectAccess = apps.get_model("core", "ObjectAccess")
|
||||
ObjectAccess.objects.create(user=user, content_object=user, is_owner=True)
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('core', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(load_initial_data)
|
||||
]
|
||||
Reference in New Issue
Block a user