[PATCH 1/3] fields: Move all custom fields to specific file
Stephen Finucane
stephen.finucane at intel.com
Sat Mar 12 06:29:09 AEDT 2016
Keep things modular.
Signed-off-by: Stephen Finucane <stephen.finucane at intel.com>
---
patchwork/fields.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++
patchwork/models.py | 42 +----------------------------------
2 files changed, 65 insertions(+), 41 deletions(-)
create mode 100644 patchwork/fields.py
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 at 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
diff --git a/patchwork/models.py b/patchwork/models.py
index 5686ec8..252f041 100644
--- a/patchwork/models.py
+++ b/patchwork/models.py
@@ -25,7 +25,6 @@ import datetime
import random
import re
-import django
from django.contrib.auth.models import User
from django.conf import settings
from django.contrib.sites.models import Site
@@ -34,9 +33,9 @@ from django.db import models
from django.db.models import Q
from django.utils.encoding import python_2_unicode_compatible
from django.utils.functional import cached_property
-from django.utils import six
from django.utils.six.moves import filter
+from patchwork.fields import HashField
from patchwork.parser import extract_tags, hash_patch
@@ -178,45 +177,6 @@ class State(models.Model):
ordering = ['ordering']
-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
-
-
@python_2_unicode_compatible
class Tag(models.Model):
name = models.CharField(max_length=20)
--
2.0.0
More information about the Patchwork
mailing list