Commit ea191c45 authored by Stephen Finucane's avatar Stephen Finucane Committed by Damien Lespiau
Browse files

fields: Move all custom fields to specific file



Keep things modular.
Signed-off-by: default avatarStephen Finucane <stephen.finucane@intel.com>
Reviewed-by: default avatarAndy Doan <andy.doan@linaro.org>
Signed-off-by: default avatarDamien Lespiau <damien.lespiau@intel.com>
parent b5b0d332
# 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
......@@ -25,7 +25,6 @@ import random
import re
import threadlocalrequest
import django
from django.conf import settings
from django.contrib import auth
from django.contrib.auth.models import User
......@@ -36,9 +35,9 @@ from django.db.models import Q
import django.dispatch
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 hash_patch, extract_tags
......@@ -221,45 +220,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)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment