forked from adammck/rapidsms-example-project
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add codify template tag to turn confirmation ids int something more o…
…fficial looking
- Loading branch information
1 parent
0dcff7e
commit c72481c
Showing
2 changed files
with
71 additions
and
2 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 |
---|---|---|
@@ -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 |
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