Allow extending GAS media files
continuous-integration/drone/tag Build is passing Details
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Ales (Shagi) Zabala Alava 2021-01-07 17:51:35 +01:00
parent ee4218c901
commit 4fa82a63a0
5 changed files with 49 additions and 4 deletions

View File

@ -18,3 +18,5 @@ MEDIA = getattr(settings, 'GAS_MEDIA', {
'gas/js/gas.js',
],
})
EXTRA_MEDIA = getattr(settings, 'GAS_EXTRA_MEDIA', None)

View File

@ -20,8 +20,8 @@
<ul id="main-navigation">
<li>{{ user }}</li>
<li>
<a href="{% url 'gas:change_password' %}"> <i class="fa fa-fw fa-key" title="{% trans "Change password" %}"></i></a>
<a href="{% url 'gas:logout' %}"> <i class="fa fa-fw fa-times text-danger" title="{% trans "Logout" %}"></i></a>
<a href="{% url 'gas:change_password' %}"> <i class="fas fa-key" title="{% trans "Change password" %}"></i></a>
<a href="{% url 'gas:logout' %}"> <i class="fas fa-times" title="{% trans "Logout" %}"></i></a>
</li>
</ul>
</nav>

View File

@ -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):

View File

@ -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

View File

@ -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.