diff --git a/plinth/__main__.py b/plinth/__main__.py index c362e6858..c192d6c07 100644 --- a/plinth/__main__.py +++ b/plinth/__main__.py @@ -96,9 +96,6 @@ def setup_server(): 'tools.staticdir.dir': '.'}} cherrypy.tree.mount(None, django.conf.settings.STATIC_URL, config) - # TODO: our modules are mimicking django apps. It'd be better to convert - # our modules to Django apps instead of reinventing the wheel. - # (we'll still have to serve the static files with cherrypy though) for module_name in module_loader.loaded_modules: module = importlib.import_module(module_name) module_path = os.path.dirname(module.__file__) @@ -163,7 +160,12 @@ def configure_django(): } } - template_directories = module_loader.get_template_directories() + applications = ['bootstrapform', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.messages', + 'plinth'] + applications += module_loader.get_modules_to_load() sessions_directory = os.path.join(cfg.data_dir, 'sessions') django.conf.settings.configure( ALLOWED_HOSTS=['127.0.0.1', 'localhost'], @@ -173,11 +175,7 @@ def configure_django(): {'ENGINE': 'django.db.backends.sqlite3', 'NAME': cfg.store_file}}, DEBUG=cfg.debug, - INSTALLED_APPS=['bootstrapform', - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.messages', - 'plinth'], + INSTALLED_APPS=applications, LOGGING=logging_configuration, LOGIN_URL='lib:login', LOGIN_REDIRECT_URL='apps:index', @@ -195,12 +193,10 @@ def configure_django(): SESSION_ENGINE='django.contrib.sessions.backends.file', SESSION_FILE_PATH=sessions_directory, STATIC_URL='/'.join([cfg.server_dir, 'static/']).replace('//', '/'), - TEMPLATE_CONTEXT_PROCESSORS=context_processors, - TEMPLATE_DIRS=template_directories) + TEMPLATE_CONTEXT_PROCESSORS=context_processors) django.setup() - LOGGER.info('Configured Django') - LOGGER.info('Template directories - %s', template_directories) + LOGGER.info('Configured Django with applications - %s', applications) LOGGER.info('Creating or adding new tables to data file') django.core.management.call_command('syncdb', interactive=False) diff --git a/plinth/module_loader.py b/plinth/module_loader.py index f32370f4d..20507de10 100644 --- a/plinth/module_loader.py +++ b/plinth/module_loader.py @@ -32,6 +32,7 @@ from plinth.signals import pre_module_loading, post_module_loading LOGGER = logging.getLogger(__name__) loaded_modules = [] +_modules_to_load = None def load_modules(): @@ -41,17 +42,7 @@ def load_modules(): """ pre_module_loading.send_robust(sender="module_loader") modules = {} - module_directory = os.path.join(cfg.config_dir, 'modules-enabled') - for file_name in os.listdir(module_directory): - full_file_name = os.path.join(module_directory, file_name) - with open(full_file_name, 'r') as file: - for line in file: - line = re.sub('#.*', '', line) - line = line.strip() - if line: - modules[line] = None - - for module_name in modules: + for module_name in get_modules_to_load(): LOGGER.info('Importing %s', module_name) try: modules[module_name] = importlib.import_module(module_name) @@ -143,14 +134,22 @@ def _initialize_module(module): raise -def get_template_directories(): - """Return the list of template directories""" - directory = os.path.dirname(os.path.abspath(__file__)) - core_directory = os.path.join(directory, 'templates') +def get_modules_to_load(): + """Get the list of modules to be loaded""" + global _modules_to_load + if _modules_to_load is not None: + return _modules_to_load - directories = set((core_directory,)) + modules = [] module_directory = os.path.join(cfg.config_dir, 'modules-enabled') - for name in os.listdir(module_directory): - directories.add(os.path.join(directory, 'modules', name, 'templates')) + for file_name in os.listdir(module_directory): + full_file_name = os.path.join(module_directory, file_name) + with open(full_file_name, 'r') as file: + for line in file: + line = re.sub('#.*', '', line) + line = line.strip() + if line: + modules.append(line) - return directories + _modules_to_load = modules + return modules