Skip to content

Commit

Permalink
add sha1 filter
Browse files Browse the repository at this point in the history
  • Loading branch information
snopoke committed Jul 3, 2019
1 parent 8f79c03 commit 499a44f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
15 changes: 15 additions & 0 deletions commcare_export/env.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from __future__ import unicode_literals, print_function, absolute_import, division, generators, nested_scopes

import hashlib
from datetime import datetime
import operator
import pytz
Expand Down Expand Up @@ -292,6 +294,18 @@ def bool2int(val):
return int(str2bool(val))


@unwrap('val')
def sha1(val):
if _not_val(val):
return None

if not isinstance(val, six.text_type):
val = six.text_type(val)
val = val.encode('utf8')

return hashlib.sha1(val).hexdigest()


@unwrap('val')
def selected_at(val, index):
if not val:
Expand Down Expand Up @@ -410,6 +424,7 @@ def __init__(self, d=None):
'attachment_url': attachment_url,
'filter_empty': _not_val,
'or': _or,
'sha1': sha1,
})
return super(BuiltInEnv, self).__init__(d)

Expand Down
3 changes: 3 additions & 0 deletions tests/test_minilinq.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ def test_eval_collapsed_list(self):
assert Apply(Reference("default"), Literal(None), Literal('a')).eval(env) == 'a'
assert Apply(Reference("default"), Literal('b'), Literal('a')).eval(env) == 'b'
assert Apply(Reference("count-selected"), Literal(u'a bb 日本')).eval(env) == 3
assert Apply(Reference("sha1"), Literal(u'a bb 日本')).eval(env) == 'e25a54025417b06d88d40baa8c71f6eee9c07fb1'
assert Apply(Reference("sha1"), Literal(b'2015')).eval(env) == 'd8e9fec0038ade95f6fd6cfbd3c3c344897594ba'
assert Apply(Reference("sha1"), Literal(2015)).eval(env) == '9cdda67ded3f25811728276cefa76b80913b4c54'

def test_or(self):
env = BuiltInEnv()
Expand Down

0 comments on commit 499a44f

Please sign in to comment.