Permalink
Browse files

add nsms.lang app which takes care of activating and setting profile …

…languages
  • Loading branch information...
nicpottier committed Apr 26, 2012
1 parent 6e16beb commit 1da8185dfc453d93013146f4d6145d35bb8e88b4
Showing with 123 additions and 0 deletions.
  1. 0 nsms/lang/__init__.py
  2. +90 −0 nsms/lang/app.py
  3. +33 −0 nsms/utils/__init__.py
View
No changes.
View
@@ -0,0 +1,90 @@
+from rapidsms.apps.base import AppBase
+from nsms.parser import Parser
+from nsms.text.models import gettext as _
+from nsms.utils import get_sms_profile
+from django.conf import settings
+from django.utils import translation
+import re
+
+# hackery to make sure our translation database contains all our the strings used in this app
+_('lang-current-lang', "Your language is set to {{ language }}.")
+_('lang-unknown-language', "Sorry, the language code '{{ code }}' is not supported.")
+_('lang-set-success', "Success, your language is now set to {{ language }}.")
+
+class App(AppBase):
+ """
+ This app is responsible for setting and changing a user's preferred language.
+
+ In order for this app to do it's magic, you need to define what your SMS profile in
+ your settings_common.py:
+
+ SMS_PROFILE = 'chws.chw'
+
+ The language app will respond to messages that begin with the keyword 'lang'. Without
+ an argument it will respond with the currently selected language, with an argument it
+ will try to find a configured language with that name and set it, eg:
+
+ lang en[_us]
+ lang rw
+ """
+ def handle (self, message):
+ profile = get_sms_profile(message.connection)
+
+ # handle activating the appropriate SMS language
+
+ # if this connection has a profile, use that language
+ if profile:
+ translation.activate(profile.language)
+
+ # otherwise use the default SMS language
+ else:
+ translation.activate(settings.DEFAULT_SMS_LANGUAGE)
+
+ parser = Parser(message.text)
+
+ # check whether this is a lang message
+ keyword = parser.next_keyword(['lang'])
+ if keyword:
+ if not profile:
+ message.respond(_('lang-unknown-user', "Sorry, your phone number is not registered."))
+ return True
+
+ # just querying the language
+ language = parser.next_word()
+ if not language:
+ message.respond(_('lang-current-lang', "Your language is set to {{ language }}.", dict(language=profile.get_language_display())))
+ return True
+
+ else:
+ # lowercase the language they passed us
+ language = language.lower()
+
+ lang_mapping = dict()
+ for lang in settings.LANGUAGES:
+ code = lang[0].lower()
+ lang_mapping[code] = code
+
+ parts = code.split('_')
+ if not parts[0] in lang_mapping:
+ lang_mapping[parts[0]] = code
+
+ # this language doesn't exit
+ if not language.lower() in lang_mapping:
+ message.respond(_('lang-unknown-language', "Sorry, the language code '{{ code }}' is not supported.", dict(code=language.lower())))
+ return True
+
+ profile.language = lang_mapping[language.lower()]
+ profile.save()
+
+ # activate the new language
+ translation.activate(profile.language)
+ message.respond(_('lang-set-success', "Success, your language is now set to {{ language }}.", dict(language=profile.get_language_display())))
+
+ return True
+
+ return False
+
+
+
+
+
View
@@ -1,4 +1,37 @@
from django.contrib.auth.models import User, Group
+from django.conf import settings
+import sys
+
+def import_from_string(kls):
+ """
+ Used to load a class object dynamically by name
+ """
+ parts = kls.split('.')
+ module = ".".join(parts[:-1])
+ m = __import__(module)
+ for comp in parts[1:]:
+ m = getattr(m, comp)
+ return m
+
+def get_sms_profile(connection):
+ """
+ Given a connection, looks up the SMS profile using the SMS_PROFILE model registered in settings.py
+ """
+ profile_class_name = getattr(settings, 'SMS_PROFILE', None)
+
+ # no SMS_PROFILE set, then return nothing
+ if not profile_class_name:
+ return None
+
+ profile_class = import_from_string(profile_class_name)
+
+ # see if we can find a match for that connection
+ matches = profile_class.objects.filter(connection=connection)
+ if matches:
+ return matches[0]
+
+ else:
+ return None
def get_connection_user(connection):
"""

0 comments on commit 1da8185

Please sign in to comment.