From patchwork Sun Oct 14 12:45:32 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Finucane X-Patchwork-Id: 983714 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [203.11.71.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42Y1YV22QSz9s8F for ; Sun, 14 Oct 2018 23:47:10 +1100 (AEDT) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=that.guru Authentication-Results: ozlabs.org; dkim=fail reason="key not found in DNS" (0-bit key; unprotected) header.d=that.guru header.i=@that.guru header.b="M0NIL2s2"; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 42Y1YT4yPgzF31L for ; Sun, 14 Oct 2018 23:47:09 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=that.guru Authentication-Results: lists.ozlabs.org; dkim=fail reason="key not found in DNS" (0-bit key; unprotected) header.d=that.guru header.i=@that.guru header.b="M0NIL2s2"; dkim-atps=neutral X-Original-To: patchwork@lists.ozlabs.org Delivered-To: patchwork@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=none (mailfrom) smtp.mailfrom=that.guru (client-ip=185.234.75.8; helo=relay001.mxroute.com; envelope-from=stephen@that.guru; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=that.guru Authentication-Results: lists.ozlabs.org; dkim=fail reason="key not found in DNS" (0-bit key; unprotected) header.d=that.guru header.i=@that.guru header.b="M0NIL2s2"; dkim-atps=neutral Received: from relay001.mxroute.com (relay001.mxroute.com [185.234.75.8]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 42Y1YN3d8czF0ym for ; Sun, 14 Oct 2018 23:47:04 +1100 (AEDT) Received: from filter002.mxroute.com (unknown [185.133.192.179]) by relay001.mxroute.com (Postfix) with ESMTP id EAC433F28E; Sun, 14 Oct 2018 12:46:31 +0000 (UTC) Received: from one.mxroute.com (one.mxroute.com [195.201.59.211]) by filter002.mxroute.com (Postfix) with ESMTPS id B0C083F051; Sun, 14 Oct 2018 12:46:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=that.guru; s=default; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=JCI/YULFL5MInEM+l1rt2AKAWF6iaIgp0WY+kvpGiIQ=; b=M0NIL2s2uhhchU0HkeVuL+/a88 AtzhE3TNGR6US3Wu6ALbsdhNzI+r8lR1o4foCDcmWYKLINSLoOVgq22eHwq29pNFx77zWa6hbBMB3 uwL+93fV5B7+XAa/atwlaefyZ6pWvMuZnU8gtVq5BIuAkakp396Fv4Zn05v1xJ60oFiGlDiQtHiGl xnMFJyxVQrPTJ42ty91FY1uf5gkPS1cdmcQRc7CI5ZcDH3veFrKvJUVLEq8m11LCFmZ0+xqVpwYhN YLMPc3v/mkZlTelZ3UX20o8LFp5qEZM4QLV1QcxB2ur1nFhiXhpj8GKCLFkoymKQjgFqMSIENgwoo Dgh40Cvw==; From: Stephen Finucane To: patchwork@lists.ozlabs.org Subject: [PATCH v2 01/10] fields: Add ColorField Date: Sun, 14 Oct 2018 13:45:32 +0100 Message-Id: <20181014124541.13393-2-stephen@that.guru> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181014124541.13393-1-stephen@that.guru> References: <20181014124541.13393-1-stephen@that.guru> X-AuthUser: stephen@that.guru X-BeenThere: patchwork@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Patchwork development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mpe@ellerman.id.au, andrew.donnellan@au1.ibm.com MIME-Version: 1.0 Errors-To: patchwork-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Patchwork" This will allow us to store color codes cleanly in the database. Signed-off-by: Stephen Finucane --- v2: - Change input type of widget to 'color' --- patchwork/fields.py | 25 +++++++++++++++++++++++++ patchwork/forms.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/patchwork/fields.py b/patchwork/fields.py index dd29ea08..45ac8212 100644 --- a/patchwork/fields.py +++ b/patchwork/fields.py @@ -28,3 +28,28 @@ class HashField(models.CharField): def db_type(self, connection=None): return 'char(%d)' % self.n_bytes + + +class ColorField(models.Field): + + description = 'Hex color code' + + def get_internal_type(self): + return "PositiveIntegerField" + + def to_python(self, value): + if isinstance(value, six.string_types) or value is None: + return value + return '#%06x' % value + + def from_db_value(self, value, *args, **kwargs): + return self.to_python(value) + + def get_prep_value(self, value): + return int(value.lstrip('#'), 16) + + def formfield(self, *args, **kwargs): + from patchwork import forms # noqa + + kwargs['form_class'] = forms.ColorField + return super(ColorField, self).formfield(*args, **kwargs) diff --git a/patchwork/forms.py b/patchwork/forms.py index f112dccc..ad696a22 100644 --- a/patchwork/forms.py +++ b/patchwork/forms.py @@ -3,8 +3,11 @@ # # SPDX-License-Identifier: GPL-2.0-or-later +import re + from django.contrib.auth.models import User from django import forms +from django.forms import widgets from django.db.models import Q from django.db.utils import ProgrammingError @@ -200,3 +203,29 @@ class MultiplePatchForm(forms.Form): if commit: instance.save() return instance + + +class ColorInput(widgets.Input): + input_type = 'color' + + +class ColorField(forms.CharField): + + widget = ColorInput + default_error_messages = { + 'invalid': 'Enter a valid colour value: e.g. "#ff0022"', + } + + def clean(self, value): + if not re.match('^#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$', value): + raise forms.ValidationError(self.error_messages['invalid']) + + value = int(value.lstrip('#'), 16) + super(ColorField, self).clean(value) + + return value + + def widget_attrs(self, widget): + attrs = super().widget_attrs(widget) + attrs['maxlength'] = 7 + return attrs