X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=apps%2Fpatchwork%2Fmodels.py;h=226a69c3a4037d64afbb3684189a2d446df8d3a5;hb=0deabd4014cbc9419d203356786e966c4f803ea3;hp=e2b636ead6208c8a62f9d02f40f9a60d704c581c;hpb=d075be0a249133942434c8ea1f936ec9b055327a;p=patchwork diff --git a/apps/patchwork/models.py b/apps/patchwork/models.py index e2b636e..226a69c 100644 --- a/apps/patchwork/models.py +++ b/apps/patchwork/models.py @@ -176,30 +176,25 @@ class HashField(models.CharField): self.algorithm = algorithm try: import hashlib - self.hashlib = True - n_bytes = len(hashlib.new(self.algorithm).hexdigest()) + 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 - self.hash_constructor = sha.new - elif algorithm == 'md5': - import md5 - self.hash_constructor = md5.new - else: + modules = { 'sha1': 'sha', 'md5': 'md5'} + + if algorithm not in modules.keys(): raise NameError("Unknown algorithm '%s'" % algorithm) - n_bytes = len(self.hash_constructor().hexdigest()) - kwargs['max_length'] = n_bytes + 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) def db_type(self): - if self.hashlib: - import hashlib - n_bytes = len(hashlib.new(self.algorithm).hexdigest()) - else: - n_bytes = len(self.hash_constructor().hexdigest()) - return 'char(%d)' % n_bytes + return 'char(%d)' % self.n_bytes class Patch(models.Model): project = models.ForeignKey(Project)