summaryrefslogtreecommitdiff
path: root/recipe/templatetags/ingredientformat.py
diff options
context:
space:
mode:
authorMike Crute <mcrute@gmail.com>2013-04-20 10:56:32 -0400
committerMike Crute <mcrute@gmail.com>2013-04-20 11:00:35 -0400
commit3aa22ec4cd4e8c80a6b3269d86a00912542462dd (patch)
tree10d3f7a05538bf9e7605c7d1677887f0fbedbf1b /recipe/templatetags/ingredientformat.py
parentc5f8644eb1f542a7a6e118955a69d843fd61a24e (diff)
downloadgreenbox-3aa22ec4cd4e8c80a6b3269d86a00912542462dd.tar.bz2
greenbox-3aa22ec4cd4e8c80a6b3269d86a00912542462dd.tar.xz
greenbox-3aa22ec4cd4e8c80a6b3269d86a00912542462dd.zip
Move recipe to the top level
Diffstat (limited to 'recipe/templatetags/ingredientformat.py')
-rw-r--r--recipe/templatetags/ingredientformat.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/recipe/templatetags/ingredientformat.py b/recipe/templatetags/ingredientformat.py
new file mode 100644
index 0000000..fa75633
--- /dev/null
+++ b/recipe/templatetags/ingredientformat.py
@@ -0,0 +1,31 @@
1from recipe.templatetags import register
2from django.template.defaultfilters import stringfilter
3
4
5@register.filter(name="format_ingredient_quantity")
6@stringfilter
7def format_ingredient_quantity(value):
8 fractions = {
9 0.125: '1/8',
10 0.25 : '1/4',
11 0.33 : '1/3',
12 0.50 : '1/2',
13 0.66 : '2/3',
14 0.75 : '3/4',
15 }
16
17 value = float(value)
18 remainder = round(value % 1, 2)
19 whole = int(value - remainder)
20
21 if whole <= 0:
22 whole = ""
23 else:
24 whole = str(whole)
25
26 if remainder <= 0:
27 remainder = ""
28 else:
29 remainder = fractions.get(remainder, str(remainder))
30
31 return "{0} {1}".format(whole, remainder)