Refactor: split code into several django apps (we call them modules).
This commit is contained in:
0
dav_base/management/commands/__init__.py
Normal file
0
dav_base/management/commands/__init__.py
Normal file
18
dav_base/management/commands/disable_module.py
Normal file
18
dav_base/management/commands/disable_module.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from django.conf import settings
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Disable a modular app from django-dav installation.'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('module')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
module_name = options['module']
|
||||
|
||||
config = settings.MODULE_CONFIG
|
||||
del config.modules[module_name]
|
||||
config.save()
|
||||
|
||||
self.stdout.write(self.style.SUCCESS('Module \'{}\' disabled.'.format(module_name)))
|
||||
37
dav_base/management/commands/enable_module.py
Normal file
37
dav_base/management/commands/enable_module.py
Normal file
@@ -0,0 +1,37 @@
|
||||
import os
|
||||
import pkg_resources
|
||||
from django.conf import settings
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
|
||||
from dav_base.config.modules import DJANGO_MAIN_MODULE, ModuleMeta
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Enable a modular app in django-dav installation.'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('module', help='the name of the module')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
django_base_dir = settings.BASE_DIR
|
||||
django_main_module = DJANGO_MAIN_MODULE
|
||||
module_name = options['module']
|
||||
|
||||
config = settings.MODULE_CONFIG
|
||||
|
||||
if module_name in config.modules.keys():
|
||||
raise CommandError('Module \'{}\' is already enabled'.format(module_name))
|
||||
|
||||
settings_file_name = 'settings-{}.py'.format(module_name)
|
||||
input_file = os.path.join('django_project_config', settings_file_name)
|
||||
if pkg_resources.resource_exists(module_name, input_file):
|
||||
output_file = os.path.join(django_base_dir, django_main_module, settings_file_name)
|
||||
if not os.path.exists(output_file):
|
||||
with open(output_file, 'w') as f:
|
||||
f.write(pkg_resources.resource_string(module_name, input_file))
|
||||
|
||||
module_meta_obj = ModuleMeta(module_name)
|
||||
config.modules[module_name] = module_meta_obj
|
||||
config.save()
|
||||
|
||||
self.stdout.write(self.style.SUCCESS('Module \'{}\' enabled.'.format(module_name)))
|
||||
17
dav_base/management/commands/list_modules.py
Normal file
17
dav_base/management/commands/list_modules.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from django.conf import settings
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'List enabled modular apps in django-dav installation.'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
pass
|
||||
|
||||
def handle(self, *args, **options):
|
||||
paragraphs = []
|
||||
config = settings.MODULE_CONFIG
|
||||
for obj in config.modules.values():
|
||||
paragraphs.append(str(obj))
|
||||
output = '\n'.join(paragraphs)
|
||||
self.stdout.write(output)
|
||||
Reference in New Issue
Block a user