diff --git a/gas/gas_settings.py b/gas/gas_settings.py index cc474ed..69e9de3 100644 --- a/gas/gas_settings.py +++ b/gas/gas_settings.py @@ -18,3 +18,5 @@ MEDIA = getattr(settings, 'GAS_MEDIA', { 'gas/js/gas.js', ], }) + +EXTRA_MEDIA = getattr(settings, 'GAS_EXTRA_MEDIA', None) diff --git a/gas/templates/gas/base.html b/gas/templates/gas/base.html index bfa13e7..0661946 100644 --- a/gas/templates/gas/base.html +++ b/gas/templates/gas/base.html @@ -20,8 +20,8 @@ diff --git a/gas/tests/test_views.py b/gas/tests/test_views.py index ec79cd0..eb9c867 100644 --- a/gas/tests/test_views.py +++ b/gas/tests/test_views.py @@ -243,6 +243,41 @@ class GASMixinTestCase(TestCase): with self.assertRaises(ImproperlyConfigured): response = view(request) + def test_extra_css(self): + class SampleView(gviews.GASMixin, TemplateView): + template_name = 'test.html' + + view = SampleView.as_view() + request = self.request_factory.get('some_url') + + request.user = self.admin_user + response = view(request) + self.assertEqual(response.status_code, 200) + + gas_settings.EXTRA_MEDIA = None + response = view(request) + self.assertEqual(response.status_code, 200) + self.assertNotIn('test.css', response.context_data['css']) + self.assertNotIn('test.js', response.context_data['js']) + + gas_settings.EXTRA_MEDIA = {'css': ['test.css']} + response = view(request) + self.assertEqual(response.status_code, 200) + self.assertIn('test.css', response.context_data['css']) + self.assertNotIn('test.js', response.context_data['js']) + + gas_settings.EXTRA_MEDIA = {'js': ['test.js']} + response = view(request) + self.assertEqual(response.status_code, 200) + self.assertNotIn('test.css', response.context_data['css']) + self.assertIn('test.js', response.context_data['js']) + + gas_settings.EXTRA_MEDIA = {'css': ['test.css'], 'js': ['test.js']} + response = view(request) + self.assertEqual(response.status_code, 200) + self.assertIn('test.css', response.context_data['css']) + self.assertIn('test.js', response.context_data['js']) + class GASListTestCase(TestCase): def test_filtered(self): diff --git a/gas/views.py b/gas/views.py index 837e948..5c27440 100644 --- a/gas/views.py +++ b/gas/views.py @@ -99,6 +99,11 @@ class GASMixin: def get_context_data(self, **kwargs): ctx = super().get_context_data(**kwargs) + css = gas_settings.MEDIA['css'] + js = gas_settings.MEDIA['js'] + if gas_settings.EXTRA_MEDIA: + css = css + gas_settings.EXTRA_MEDIA.get('css', []) + js = js + gas_settings.EXTRA_MEDIA.get('js', []) ctx.update({ 'base_template': self.base_template, 'header_title': self.get_header_title(), @@ -108,8 +113,8 @@ class GASMixin: 'actions': self.get_actions(), 'gas_title': gas_settings.TITLE, 'logo_static_url': gas_settings.LOGO, - 'css': gas_settings.MEDIA['css'], - 'js': gas_settings.MEDIA['js'], + 'css': css, + 'js': js, }) return ctx diff --git a/readme.md b/readme.md index b8de1a5..83f2741 100644 --- a/readme.md +++ b/readme.md @@ -32,6 +32,9 @@ Project settings can customize the `gas` control panel: * `GAS_TITLE`: Name of the control panel. * `GAS_LOGO`: Icon for the control panel. * `GAS_MEDIA`: A django form's Media like dict with css and js files. + Overrides default css and javascript. +* `GAS_EXTRA_MEDIA`: A django form's Media like dict with css and js + files. Extends current css and javascript instead of overriding. The default values of those settings can be viewed in the `gas/gas_settings.py` file.