X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=apps%2Fpatchwork%2Fmodels.py;h=d8a9b3ff5c77cdecce3206ec8ecf11e24e73a9aa;hb=04767c65175f2a76127cd78649c5d92a82a39bb6;hp=a40931aa1c3af6f7079dcc761f310bf2df9b8c91;hpb=f1e089f7736ac8f7b9af784461350c4c169211ad;p=patchwork diff --git a/apps/patchwork/models.py b/apps/patchwork/models.py index a40931a..d8a9b3f 100644 --- a/apps/patchwork/models.py +++ b/apps/patchwork/models.py @@ -28,9 +28,15 @@ import re import datetime, time import string import random -import hashlib -from email.mime.text import MIMEText -import email.utils + +try: + from email.mime.text import MIMEText + import email.utils +except ImportError: + # Python 2.4 compatibility + from email.MIMEText import MIMEText + import email.Utils + email.utils = email.Utils class Person(models.Model): email = models.CharField(max_length=255, unique = True) @@ -167,10 +173,27 @@ class HashField(models.Field): def __init__(self, algorithm = 'sha1', *args, **kwargs): self.algorithm = algorithm + try: + import hashlib + self.hashlib = True + 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: + raise NameError("Unknown algorithm '%s'" % algorithm) + super(HashField, self).__init__(*args, **kwargs) def db_type(self): - n_bytes = len(hashlib.new(self.algorithm).digest()) + if self.hashlib: + n_bytes = len(hashlib.new(self.algorithm).digest()) + else: + n_bytes = len(self.hash_constructor().digest()) if settings.DATABASE_ENGINE == 'postgresql': return 'bytea' elif settings.DATABASE_ENGINE == 'mysql':