-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom template tag: settings_value
- Loading branch information
1 parent
cdd55e8
commit e3c9611
Showing
3 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
19 changes: 19 additions & 0 deletions
19
onadata/apps/logger/templatetags/customize_template_by_domain.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from django import template | ||
from django.conf import settings | ||
from onadata.apps.api.tools import get_host_domain | ||
|
||
register = template.Library() | ||
|
||
|
||
@register.simple_tag(takes_context=True) | ||
def settings_value(context, setting): | ||
template_customization = getattr(settings, "TEMPLATE_CUSTOMIZATION", {}) | ||
request = context.get("request") | ||
domain = get_host_domain(request) | ||
if domain in template_customization: | ||
template_setting = template_customization[domain] | ||
elif "*" in template_customization: | ||
template_setting = template_customization["*"] | ||
else: | ||
template_setting = {} | ||
return template_setting[setting] if setting in template_setting else "" |
68 changes: 68 additions & 0 deletions
68
onadata/apps/logger/tests/test_customize_template_by_domain.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from onadata.apps.main.tests.test_base import TestBase | ||
from django.test import override_settings | ||
from onadata.apps.logger.templatetags.customize_template_by_domain import settings_value | ||
|
||
TEMPLATE_CUSTOMIZATION = { | ||
"*": { | ||
"app_name": "Ona", | ||
"login_logo": "/static/ona-logo.png", | ||
"favicon": "/static/ona-favicon-32x32.png", | ||
}, | ||
"api.ona.io": { | ||
"login_background": "#009CDE", | ||
"app_name": "NCDS", | ||
"login_logo": "/static/who-logo.jpeg", | ||
"favicon": "/static/who-favicon-32x32.png", | ||
}, | ||
} | ||
|
||
|
||
class TestCustomizeTemplateTasks(TestBase): | ||
""" | ||
Test api tasks | ||
""" | ||
|
||
@override_settings(TEMPLATE_CUSTOMIZATION=TEMPLATE_CUSTOMIZATION) | ||
@override_settings(ALLOWED_HOSTS=["api.ona.io"]) | ||
def test_for_domain(self): | ||
"""Test settings_value returns correct values""" | ||
request = self.factory.get("/") | ||
request.META["HTTP_HOST"] = "api.ona.io" | ||
self.assertEqual( | ||
settings_value({"request": request}, "login_background"), "#009CDE" | ||
) | ||
self.assertEqual(settings_value({"request": request}, "app_name"), "NCDS") | ||
self.assertEqual( | ||
settings_value({"request": request}, "login_logo"), "/static/who-logo.jpeg" | ||
) | ||
self.assertEqual( | ||
settings_value({"request": request}, "favicon"), | ||
"/static/who-favicon-32x32.png", | ||
) | ||
|
||
@override_settings(TEMPLATE_CUSTOMIZATION=TEMPLATE_CUSTOMIZATION) | ||
@override_settings(ALLOWED_HOSTS=["*"]) | ||
def test_for_no_domain(self): | ||
"""Test settings_value returns correct values""" | ||
request = self.factory.get("/") | ||
self.assertEqual(settings_value({"request": request}, "login_background"), "") | ||
self.assertEqual(settings_value({"request": request}, "app_name"), "Ona") | ||
self.assertEqual( | ||
settings_value({"request": request}, "login_logo"), "/static/ona-logo.png" | ||
) | ||
self.assertEqual( | ||
settings_value({"request": request}, "favicon"), | ||
"/static/ona-favicon-32x32.png", | ||
) | ||
|
||
@override_settings(ALLOWED_HOSTS=["*"]) | ||
def test_for_no_domain_no_settings(self): | ||
"""Test settings_value returns correct values""" | ||
request = self.factory.get("/") | ||
self.assertEqual(settings_value({"request": request}, "login_background"), "") | ||
self.assertEqual(settings_value({"request": request}, "app_name"), "") | ||
self.assertEqual(settings_value({"request": request}, "login_logo"), "") | ||
self.assertEqual( | ||
settings_value({"request": request}, "favicon"), | ||
"", | ||
) |