Skip to content

Commit

Permalink
Add custom template tag: settings_value
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankApiyo committed Nov 22, 2023
1 parent cdd55e8 commit e3c9611
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
Empty file.
19 changes: 19 additions & 0 deletions onadata/apps/logger/templatetags/customize_template_by_domain.py
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 onadata/apps/logger/tests/test_customize_template_by_domain.py
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"),
"",
)

0 comments on commit e3c9611

Please sign in to comment.