From nobody Fri Apr 19 11:44:42 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=patchew-devel-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=patchew-devel-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1524732217937999.4164019835004; Thu, 26 Apr 2018 01:43:37 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C0DEC30014B9; Thu, 26 Apr 2018 08:43:36 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B28A762674; Thu, 26 Apr 2018 08:43:36 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id A63F44CA96; Thu, 26 Apr 2018 08:43:36 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w3Q8XvxD011755 for ; Thu, 26 Apr 2018 04:33:57 -0400 Received: by smtp.corp.redhat.com (Postfix) id 892462010CEB; Thu, 26 Apr 2018 08:33:57 +0000 (UTC) Received: from mx1.redhat.com (ext-mx06.extmail.prod.ext.phx2.redhat.com [10.5.110.30]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 82B332010CF6 for ; Thu, 26 Apr 2018 08:33:55 +0000 (UTC) Received: from mail-wm0-f68.google.com (mail-wm0-f68.google.com [74.125.82.68]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 55318356CA for ; Thu, 26 Apr 2018 08:33:44 +0000 (UTC) Received: by mail-wm0-f68.google.com with SMTP id b21so11575717wme.4 for ; Thu, 26 Apr 2018 01:33:44 -0700 (PDT) Received: from donizetti.lan (dynamic-adsl-78-12-189-60.clienti.tiscali.it. [78.12.189.60]) by smtp.gmail.com with ESMTPSA id i93sm11001660edc.55.2018.04.26.01.33.41 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 26 Apr 2018 01:33:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:subject:date:message-id; bh=bUkvPmpOZzYHXsPUKPPLI8GMBrnxiA3f+ZTK7ocwCF8=; b=UFnPyEliTJknJWgnCDzXh/ovtxc9WRWNc9HbwjcO/sPlwmvXKte5pcn+MQMHLx5yfi Ns8O0RN0ausCaPR0q7pu+9FPZ8nPOgAt22+PEZBM6kqkZdW1BTw+x4OpAl7zgjygs3GT JxE82g/d68k1bSh7ga7Eoa1in5HrSAcIwAAoAzibODkJMf5omWjTgYuSq/0BWRDpMgpw /N6JW2jcvO+y1RbB1ftwhW43rWHksqC8oGOWFzhX5ROHLQTwjxxJ4h6lAcMnr2IxYYVV VRn235huDcdDjGa2yX0WHlhxGQi1QSJu4pibaeB1qr+x+yY14IyCEfhvdWa7ZHVb7D9J iS6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:subject:date:message-id; bh=bUkvPmpOZzYHXsPUKPPLI8GMBrnxiA3f+ZTK7ocwCF8=; b=bRCpWMeR5+r1au4TkV9z5nYwOovYCBI+tR9px9tRw6z1IscoxTSW+QPw5PnFpZPoo8 2JlTgA0paJRRn+FdQTToqBTBBgTRB8HNNZsRIzB2f3l0Jsw4y8G7mOzhHbp7AKZiqAB/ 3+9Vu2lQbPD09o47VTjMw3nh2m1oeVxjEiA0LKtwzgFmFMAH6Zf2jeEAg9kGsdciZ1jb FbMXaZA/RL8tbrqPEEhCrzKlVzcKVpfN4OVD6Iq5MS/h9ETCV77Eygs/7PWNUC9tbym7 eldx0sekEliEJKRKj5IDWmWOb94YZ742xIOsMzn4/wKjdGb1aX/vu8Uqwl347vDgZySG iBXA== X-Gm-Message-State: ALQs6tDKCo53l9m05w3OhjOYUdFIBLOG9vGoPR1XWP3upk9os5tlIrv/ LhSYEgnChETlBRXi+fRE556l3SbN X-Google-Smtp-Source: AIpwx488CJBrZaVuBNjBaDPWWB/PNBJlqCm5yNp7vnnP7hpo+uPWCoF7KwbO+unHwE7rt+dgvlz1Kg== X-Received: by 10.80.158.141 with SMTP id a13mr36753292edf.78.1524731622566; Thu, 26 Apr 2018 01:33:42 -0700 (PDT) From: Paolo Bonzini To: patchew-devel@redhat.com Date: Thu, 26 Apr 2018 10:33:40 +0200 Message-Id: <20180426083340.9816-1-pbonzini@redhat.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Thu, 26 Apr 2018 08:33:44 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Thu, 26 Apr 2018 08:33:44 +0000 (UTC) for IP:'74.125.82.68' DOMAIN:'mail-wm0-f68.google.com' HELO:'mail-wm0-f68.google.com' FROM:'paolo.bonzini@gmail.com' RCPT:'' X-RedHat-Spam-Score: 0.331 (DKIM_SIGNED, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_PASS, T_DKIM_INVALID) 74.125.82.68 mail-wm0-f68.google.com 74.125.82.68 mail-wm0-f68.google.com X-RedHat-Possible-Forgery: Paolo Bonzini X-Scanned-By: MIMEDefang 2.78 on 10.5.110.30 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.25 X-loop: patchew-devel@redhat.com Subject: [Patchew-devel] [PATCH] models: use JSONField and JSONCharField X-BeenThere: patchew-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Patchew development and discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: patchew-devel-bounces@redhat.com Errors-To: patchew-devel-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.40]); Thu, 26 Apr 2018 08:43:37 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Doing the JSON<->char conversion in the Field simplifies the model. And because the accessor now returns a Python object rather than a strict, it becomes possible to use a Serializer for sender and recipient instead of a SerializerMethodFields. This is important because (as Shubham reminded me...) SerializerMethodField is read-only. Signed-off-by: Paolo Bonzini --- api/migrations/0026_auto_20180426_0829.py | 31 +++++++++++++++++++++++ api/models.py | 29 ++++++++------------- api/rest.py | 5 ++-- requirements.txt | 1 + 4 files changed, 44 insertions(+), 22 deletions(-) create mode 100644 api/migrations/0026_auto_20180426_0829.py diff --git a/api/migrations/0026_auto_20180426_0829.py b/api/migrations/002= 6_auto_20180426_0829.py new file mode 100644 index 0000000..acd5a63 --- /dev/null +++ b/api/migrations/0026_auto_20180426_0829.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.12 on 2018-04-26 08:29 +from __future__ import unicode_literals + +from django.db import migrations +import jsonfield.fields + + +class Migration(migrations.Migration): + + dependencies =3D [ + ('api', '0025_populate_project_maintainers'), + ] + + operations =3D [ + migrations.AlterField( + model_name=3D'message', + name=3D'prefixes', + field=3Djsonfield.fields.JSONField(blank=3DTrue), + ), + migrations.AlterField( + model_name=3D'message', + name=3D'recipients', + field=3Djsonfield.fields.JSONField(), + ), + migrations.AlterField( + model_name=3D'message', + name=3D'sender', + field=3Djsonfield.fields.JSONCharField(db_index=3DTrue, max_le= ngth=3D4096), + ), + ] diff --git a/api/models.py b/api/models.py index 501070c..95fd6dd 100644 --- a/api/models.py +++ b/api/models.py @@ -21,6 +21,7 @@ from django.conf import settings from django.db import models from django.contrib.auth.models import User from django.urls import reverse +import jsonfield from mbox import MboxMessage from event import emit_event, declare_event import lzma @@ -262,9 +263,9 @@ class MessageManager(models.Manager): subject=3Dm.get_subject(), stripped_subject=3Dm.get_subject(strip_tags=3DTr= ue), version=3Dm.get_version(), - sender=3Djson.dumps(m.get_from()), - recipients=3Djson.dumps(m.get_to() + m.get_cc()), - prefixes=3Djson.dumps(m.get_prefixes()), + sender=3Dm.get_from(), + recipients=3Dm.get_to() + m.get_cc(), + prefixes=3Dm.get_prefixes(), is_series_head=3Dm.is_series_head(), is_patch=3Dm.is_patch(), patch_num=3Dm.get_num()[0]) @@ -303,10 +304,9 @@ class Message(models.Model): subject =3D HeaderFieldModel() stripped_subject =3D HeaderFieldModel(db_index=3DTrue) version =3D models.PositiveSmallIntegerField(default=3D0) - sender =3D HeaderFieldModel(db_index=3DTrue) - recipients =3D models.TextField() - # JSON encoded list - prefixes =3D models.TextField(blank=3DTrue) + sender =3D jsonfield.JSONCharField(max_length=3D4096, db_index=3DTrue) + recipients =3D jsonfield.JSONField() + prefixes =3D jsonfield.JSONField(blank=3DTrue) is_series_head =3D models.BooleanField() is_complete =3D models.BooleanField(default=3DFalse) is_patch =3D models.BooleanField() @@ -333,13 +333,10 @@ class Message(models.Model): self._mbox_obj =3D MboxMessage(self.mbox) return self.mbox =20 - def get_prefixes(self): - return json.loads(self.prefixes) - def get_num(self): assert self.is_patch or self.is_series_head cur, total =3D 1, 1 - for tag in self.get_prefixes(): + for tag in self.prefixes: if '/' in tag: n, m =3D tag.split('/') try: @@ -451,17 +448,11 @@ class Message(models.Model): emit_event("SetProperty", obj=3Dself, name=3Dprop, value=3Dvalue, old_value=3Dold_val) =20 - def get_sender(self): - return json.loads(self.sender) - - def get_recipients(self): - return json.loads(self.recipients) - def get_sender_addr(self): - return self.get_sender()[1] + return self.sender[1] =20 def get_sender_name(self): - return self.get_sender()[0] + return self.sender[0] =20 def _get_age(self, date): def _seconds_to_human(sec): diff --git a/api/rest.py b/api/rest.py index 381b569..e3aca3f 100644 --- a/api/rest.py +++ b/api/rest.py @@ -136,11 +136,10 @@ class BaseMessageSerializer(serializers.ModelSerializ= er): return d =20 def get_recipients(self, obj): - return [self.format_name_addr(*x) for x in obj.get_recipients()] + return [self.format_name_addr(*x) for x in obj.recipients] =20 def get_sender(self, obj): - name, addr =3D obj.get_sender() - return self.format_name_addr(*obj.get_sender()) + return self.format_name_addr(*obj.sender) =20 # a message_id is *not* unique, so we can only list class BaseMessageViewSet(mixins.ListModelMixin, viewsets.GenericViewSet): diff --git a/requirements.txt b/requirements.txt index 252f226..ac668e5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,5 +4,6 @@ Markdown>=3D2.6.6 django-debug-toolbar=3D=3D1.8 gunicorn>=3D19 djangorestframework>=3D3.0.0 +jsonfield >=3D 2.0.2, <3.0 drf-nested-routers coreapi-cli \ No newline at end of file --=20 2.17.0 _______________________________________________ Patchew-devel mailing list Patchew-devel@redhat.com https://www.redhat.com/mailman/listinfo/patchew-devel