diff options
author | Stephen Finucane <stephen.finucane@intel.com> | 2016-03-11 17:21:23 +0000 |
---|---|---|
committer | Stephen Finucane <stephen.finucane@intel.com> | 2016-03-16 09:27:45 +0000 |
commit | b43687b042d84920b835449abb21c44319991a5e (patch) | |
tree | 925887d8340a2cb311c57238137e6ccaed002d03 /patchwork/fields.py | |
parent | 44fe7bae80be9de34c6d6ce4e239f51beb2f16ff (diff) | |
download | patchwork-b43687b042d84920b835449abb21c44319991a5e.tar patchwork-b43687b042d84920b835449abb21c44319991a5e.tar.gz |
fields: Move all custom fields to specific file
Keep things modular.
Signed-off-by: Stephen Finucane <stephen.finucane@intel.com>
Reviewed-by: Andy Doan <andy.doan@linaro.org>
Diffstat (limited to 'patchwork/fields.py')
-rw-r--r-- | patchwork/fields.py | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/patchwork/fields.py b/patchwork/fields.py new file mode 100644 index 0000000..96fdd28 --- /dev/null +++ b/patchwork/fields.py @@ -0,0 +1,64 @@ +# Patchwork - automated patch tracking system +# Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org> +# Copyright (C) 2015 Intel Corporation +# +# This file is part of the Patchwork package. +# +# Patchwork is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# Patchwork is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Patchwork; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +from __future__ import absolute_import + +import django +from django.db import models +from django.utils import six + + +if django.VERSION < (1, 8): + HashFieldBase = six.with_metaclass(models.SubfieldBase, models.CharField) +else: + HashFieldBase = models.CharField + + +class HashField(HashFieldBase): + + def __init__(self, algorithm='sha1', *args, **kwargs): + self.algorithm = algorithm + try: + import hashlib + + def _construct(string=''): + if isinstance(string, six.text_type): + string = string.encode('utf-8') + return hashlib.new(self.algorithm, string) + self.construct = _construct + self.n_bytes = len(hashlib.new(self.algorithm).hexdigest()) + except ImportError: + modules = {'sha1': 'sha', 'md5': 'md5'} + + if algorithm not in modules: + raise NameError("Unknown algorithm '%s'" % algorithm) + + 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 from_db_value(self, value, expression, connection, context): + return self.to_python(value) + + def db_type(self, connection=None): + return 'char(%d)' % self.n_bytes |