summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeremy Kerr <jk@ozlabs.org>2008-09-11 15:53:08 +1000
committerJeremy Kerr <jk@ozlabs.org>2008-09-11 15:53:08 +1000
commit0deabd4014cbc9419d203356786e966c4f803ea3 (patch)
tree873f31695974f977ab64ff3c231348a5e1c01736
parent7cc12f61df06e90c09b706dc34007bce099a9350 (diff)
downloadpatchwork-0deabd4014cbc9419d203356786e966c4f803ea3.tar.bz2
patchwork-0deabd4014cbc9419d203356786e966c4f803ea3.tar.xz
Add HashField.construct() method
To construct a new hash object for the given algorithm. While we're at it, clean up the hashlib-wrapping code. Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
-rw-r--r--apps/patchwork/models.py20
1 files changed, 10 insertions, 10 deletions
diff --git a/apps/patchwork/models.py b/apps/patchwork/models.py
index 62ce592..226a69c 100644
--- a/apps/patchwork/models.py
+++ b/apps/patchwork/models.py
@@ -176,19 +176,19 @@ class HashField(models.CharField):
self.algorithm = algorithm
try:
import hashlib
- self.hashlib = True
+ def _construct(string = ''):
+ return hashlib.new(self.algorithm, string)
+ self.construct = _construct
self.n_bytes = len(hashlib.new(self.algorithm).hexdigest())
except ImportError:
- self.hashlib = False
- if algorithm == 'sha1':
- import sha
- hash_constructor = sha.new
- elif algorithm == 'md5':
- import md5
- hash_constructor = md5.new
- else:
+ modules = { 'sha1': 'sha', 'md5': 'md5'}
+
+ if algorithm not in modules.keys():
raise NameError("Unknown algorithm '%s'" % algorithm)
- self.n_bytes = len(hash_constructor().hexdigest())
+
+ self.construct = __import__(modules[algorithm]).new
+
+ self.n_bytes = len(self.construct().hexdigest())
kwargs['max_length'] = self.n_bytes
super(HashField, self).__init__(*args, **kwargs)