forked from kobotoolbox/kpi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved 'sitewide_messages' logic to 'hub' app. Added an unittest for it.
- Loading branch information
1 parent
27555ee
commit 8c00ab6
Showing
5 changed files
with
83 additions
and
16 deletions.
There are no files selected for viewing
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,20 @@ | ||
# -*- coding: utf-8 -*- | ||
from django.test import TestCase | ||
|
||
from hub.utils.i18n import I18nUtils | ||
|
||
|
||
class I18nTestCase(TestCase): | ||
fixtures= ['test_data'] | ||
|
||
def setUp(self): | ||
pass | ||
|
||
def test_welcome_message(self): | ||
welcome_message_fr = I18nUtils.get_sitewide_message(lang="fr") | ||
welcome_message_es = I18nUtils.get_sitewide_message(lang="es") | ||
welcome_message = I18nUtils.get_sitewide_message() | ||
|
||
self.assertEqual(welcome_message_fr.raw, "Le message de bienvenue") | ||
self.assertEqual(welcome_message.raw, "Global welcome message") | ||
self.assertEqual(welcome_message_es.raw, welcome_message.raw) |
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 @@ | ||
# -*- coding: utf-8 -*- |
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,41 @@ | ||
# -*- coding: utf-8 -*- | ||
from django.db.models import Q | ||
from django.db.models.functions import Length | ||
from django.utils.translation import get_language | ||
|
||
from ..models import SitewideMessage | ||
|
||
|
||
class I18nUtils(object): | ||
|
||
@staticmethod | ||
def get_sitewide_message(slug="welcome_message", lang=None): | ||
""" | ||
Returns a sitewide message based on its slug and the specified language. | ||
If the language is not specified, it will use the current language. | ||
If there are no results found, it falls back on the global version. | ||
It doesn't exist at all, it returns None. | ||
:param slug: str | ||
:param lang: str|None | ||
:return: MarkupField|None | ||
""" | ||
|
||
# Get default value if lang is not specified | ||
language = lang if lang else get_language() | ||
|
||
# Let's retrieve messages where slug is either: | ||
# - "<slug>_<locale>" | ||
# - "<slug>" | ||
# We order the results by the length of the slug to be sure | ||
# localized version comes first. | ||
sitewide_message = SitewideMessage.objects\ | ||
.filter( | ||
Q(slug="{}_{}".format(slug, language)) | | ||
Q(slug="{}".format(slug)))\ | ||
.order_by(Length("slug").desc())\ | ||
.first() | ||
|
||
if sitewide_message is not None: | ||
return sitewide_message.body | ||
|
||
return None |
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
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