Skip to content

Commit

Permalink
add codify template tag to turn confirmation ids int something more o…
Browse files Browse the repository at this point in the history
…fficial looking
  • Loading branch information
nicpottier committed Mar 3, 2011
1 parent 0dcff7e commit c72481c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
51 changes: 51 additions & 0 deletions rapidsms_xforms/templatetags/messages.py
@@ -1,11 +1,62 @@
from django import template
from django.utils.safestring import mark_safe

#
# Contains simple filters that may be useful within messages processing.
#

def multiply(input, property):
"""
Just multiplies two values together. Surprisingly this isn't possible in normal templates.
"""
return "%.2f" % (float(input) * float(property))

register = template.Library()
register.filter('multiply', multiply)

def in_base_36(number):
if not isinstance(number, (int, long)):
raise TypeError('number must be an integer')
if number < 0:
raise ValueError('number must be positive')

alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'

base36 = ''
while number:
number, i = divmod(number, 36)
base36 = alphabet[i] + base36

return base36 or alphabet[0]

def codify(input, property=None):
"""
Turns a numerical id into something slightly more impressive. Specifically a 4 letter
base 36 item with an optional prefix, zero padded. This will work up to about 1.5M
entries, at which point it will start skipping to six letters instead up to ~2B, then
just echo the raw value in base 36.
IE, if you pass in 3688, this will return 00A8. If you pass in a prefix of S, then
the value will be S00A8.
"""
val = int(input)

# values that can be represented in 4 letters
if val <= 1679615:
based = in_base_36(val)
based = "0" * (4-len(based)) + based
# things that are six letters
elif val <= 2176782335:
based = in_base_36(val)
based = "0" * (6-len(based)) + based
# huge!
else:
based = in_base_36(val)

if property:
return property + based
else:
return based

register.filter('codify', codify)
codify.is_safe = True
22 changes: 20 additions & 2 deletions rapidsms_xforms/tests.py
Expand Up @@ -391,7 +391,25 @@ def testConfirmationId(self):
self.assertEquals(5, submission6.confirmation_id)

def testTemplateResponse(self):
# first test no template
# codify the confirmation id
self.xform.response = 'Your confirmation id is: {{ confirmation_id|codify:"SA" }}'
self.xform.save()

submission = self.xform.process_sms_submission(IncomingMessage(None, "survey male 10"))

# should be safe to use a static value since we are the first test
self.failUnlessEqual(submission.response, "Your confirmation id is: SA0001")

# no prefix
self.xform.response = 'Your confirmation id is: {{ confirmation_id|codify }}'
self.xform.save()

submission = self.xform.process_sms_submission(IncomingMessage(None, "survey male 10"))

# should be safe to use a static value since we are the first test
self.failUnlessEqual(submission.response, "Your confirmation id is: 0002")

# now test no template
self.xform.response = "Thanks for sending your message"
self.xform.save()

Expand All @@ -404,7 +422,7 @@ def testTemplateResponse(self):
self.xform.save()

submission = self.xform.process_sms_submission(IncomingMessage(None, "survey male 10"))
self.failUnlessEqual(submission.response, "You recorded an age of 10 and a gender of male. Your confirmation id is 2.")
self.failUnlessEqual(submission.response, "You recorded an age of 10 and a gender of male. Your confirmation id is 4.")

# if they insert a command that isn't there, it should just be empty
self.xform.response = "You recorded an age of {{ age }} and a gender of {{ gender }}. {{ not_there }} Thanks."
Expand Down

0 comments on commit c72481c

Please sign in to comment.