From 46364a84e103c4d96555248ad2d7b9a80d60b89d Mon Sep 17 00:00:00 2001 From: FinnStutzenstein Date: Tue, 26 Sep 2017 14:19:48 +0200 Subject: [PATCH] Generate webclient-realm.js while collecting staticfiles (fixes #3422) --- CHANGELOG | 2 +- .../core/management/commands/collectstatic.py | 43 +++++++++++++++++++ openslides/core/views.py | 2 +- openslides/global_settings.py | 4 +- 4 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 openslides/core/management/commands/collectstatic.py diff --git a/CHANGELOG b/CHANGELOG index ab671637a..0255a1b4d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -85,7 +85,7 @@ Core: - Added custom translations in config [#3383]. - Added dynamic webpage title [#3404]. - Added 'go to top'-link [#3404]. -- Added caching for the index views [#3419]. +- Added caching for the index views [#3419, #3424]. - Added projector prioritization [#3425]. Mediafiles: diff --git a/openslides/core/management/commands/collectstatic.py b/openslides/core/management/commands/collectstatic.py new file mode 100644 index 000000000..1b155f31f --- /dev/null +++ b/openslides/core/management/commands/collectstatic.py @@ -0,0 +1,43 @@ +import os +from typing import Any, Dict + +from django.conf import settings +from django.contrib.staticfiles.management.commands.collectstatic import \ + Command as CollectStatic +from django.core.management.base import CommandError +from django.db.utils import OperationalError + +from ...views import WebclientJavaScriptView + + +class Command(CollectStatic): + """ + Custom collectstatic command. + """ + realms = ['site', 'projector'] + js_filename = 'webclient-{}.js' + + def handle(self, **options: Any) -> str: + try: + self.view = WebclientJavaScriptView() + except OperationalError: + raise CommandError('You have to run OpenSlides first to create a ' + + 'database before collecting staticfiles.') + return super().handle(**options) + + def collect(self) -> Dict[str, Any]: + destination_dir = os.path.join(settings.OS_STATICFILES_DIR, 'js') + if not os.path.exists(destination_dir): + os.makedirs(destination_dir) + + for realm in self.realms: + filename = self.js_filename.format(realm) + content = self.view.get(realm=realm).content + path = os.path.join(destination_dir, filename) + with open(path, 'wb+') as f: + f.write(content) + self.stdout.write("Written WebclientJavaScriptView for realm {} to '{}'".format( + realm, + path)) + + return super().collect() diff --git a/openslides/core/views.py b/openslides/core/views.py index d29b0cef4..bd8e5c038 100644 --- a/openslides/core/views.py +++ b/openslides/core/views.py @@ -178,7 +178,7 @@ class WebclientJavaScriptView(utils_views.View): """.format(realm=realm, angular_modules=angular_modules, angular_constants=angular_constants, js_files=js_files) + """ }()); - """) + """).replace('\n', '') self.cache[realm] = content def get(self, *args: Any, **kwargs: Any) -> HttpResponse: diff --git a/openslides/global_settings.py b/openslides/global_settings.py index 2487d7d68..2cacbaacd 100644 --- a/openslides/global_settings.py +++ b/openslides/global_settings.py @@ -81,8 +81,10 @@ LOCALE_PATHS = [ STATIC_URL = '/static/' +OS_STATICFILES_DIR = os.path.join(MODULE_DIR, 'static') + STATICFILES_DIRS = [ - os.path.join(MODULE_DIR, 'static'), + OS_STATICFILES_DIR, ]