Skip to content

Commit

Permalink
Merge pull request #26 from hdlim15/bugfix_hashed_cache_key
Browse files Browse the repository at this point in the history
use sha1 instead of built in hash function
  • Loading branch information
Hyuckin David Lim authored Oct 8, 2019
2 parents 4390bb3 + 5c2f183 commit 74291b9
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion autocompleter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = (0, 8, 2)
VERSION = (0, 8, 3)

from autocompleter.registry import registry, signal_registry
from autocompleter.base import AutocompleterBase, AutocompleterModelProvider, AutocompleterDictProvider, Autocompleter
Expand Down
10 changes: 7 additions & 3 deletions autocompleter/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections import OrderedDict
from hashlib import sha1
import redis
import json
import itertools
Expand Down Expand Up @@ -863,19 +864,22 @@ def hash_facets(facets):
Given an array of facet data, return a deterministic hash such that
the ordering of keys inside the facet dicts does not matter.
"""
def sha1_digest(my_str):
return sha1(my_str.encode(encoding='UTF-8')).hexdigest()

facet_hashes = []
for facet in facets:
sub_facet_hashes = []
facet_type = facet['type']
sub_facets = facet['facets']
for sub_facet in sub_facets:
sub_facet_str = 'key:' + sub_facet['key'] + 'value:' + str(sub_facet['value'])
sub_facet_hashes.append(hash(sub_facet_str))
sub_facet_hashes.append(sha1_digest(sub_facet_str))
sub_facet_hashes.sort()
facet_str = 'type:' + facet_type + 'facets:' + str(sub_facet_hashes)
facet_hashes.append(hash(facet_str))
facet_hashes.append(sha1_digest(facet_str))
facet_hashes.sort()
final_facet_hash = hash(str(facet_hashes))
final_facet_hash = sha1_digest(str(facet_hashes))
return final_facet_hash

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='django-autocompleter',
version="0.8.2",
version="0.8.3",
description='A redis-backed autocompletor for Django projects',
author='Ara Anjargolian',
author_email='[email protected]',
Expand Down
8 changes: 4 additions & 4 deletions test_project/test_app/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_hashing_order(self):
'type': 'or',
'facets': [
{'key': 'sector', 'value': 'Technology'},
{'key': 'industry', 'value': 'Software'}
{'value': 'Software', 'key': 'industry'}
]
}
]
Expand Down Expand Up @@ -103,8 +103,8 @@ def test_multiple_facets_hashing_order(self):
{
'type': 'or',
'facets': [
{'key': 'sector', 'value': 'Technology'},
{'key': 'industry', 'value': 'Software'}
{'value': 'Technology', 'key': 'sector'},
{'value': 'Software', 'key': 'industry'}
]
},
{
Expand All @@ -127,7 +127,7 @@ def test_multiple_facets_hashing_order(self):
{
'type': 'or',
'facets': [
{'key': 'sector', 'value': 'Technology'},
{'value': 'Technology', 'key': 'sector'},
{'key': 'industry', 'value': 'Software'}
]
},
Expand Down

0 comments on commit 74291b9

Please sign in to comment.