From nobody Wed Oct 30 19:56:01 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=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 152508400237571.10236370617736; Mon, 30 Apr 2018 03:26:42 -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 ADC5730F8DA1; Mon, 30 Apr 2018 10:26:40 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9DBD680886; Mon, 30 Apr 2018 10:26:40 +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 8A4A1180005A; Mon, 30 Apr 2018 10:26:40 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w3UAQdu6000542 for ; Mon, 30 Apr 2018 06:26:39 -0400 Received: by smtp.corp.redhat.com (Postfix) id 98C371799C; Mon, 30 Apr 2018 10:26:39 +0000 (UTC) Received: from mx1.redhat.com (ext-mx03.extmail.prod.ext.phx2.redhat.com [10.5.110.27]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8F71A17CCD for ; Mon, 30 Apr 2018 10:26:36 +0000 (UTC) Received: from mail-pf0-f178.google.com (mail-pf0-f178.google.com [209.85.192.178]) (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 4F2A980F75 for ; Mon, 30 Apr 2018 10:26:25 +0000 (UTC) Received: by mail-pf0-f178.google.com with SMTP id a14so6418314pfi.1 for ; Mon, 30 Apr 2018 03:26:25 -0700 (PDT) Received: from localhost.localdomain ([14.141.249.170]) by smtp.gmail.com with ESMTPSA id r68sm16366659pfi.174.2018.04.30.03.26.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 30 Apr 2018 03:26:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=sqUcgJHR72EYMsKQ8/xoAmyUV2iLCHozwLzNy8nhK64=; b=tB7y4SJyKAvQiV7LyJJodlu5w8T3hO+5QsIWmoWKkQZo6RZGEkBj7H6UEgT7NM5dWQ GgBb2QGMRBptZUwXpereeYWAOqbXOn5+CNJCEe+SpMmxJcCIgNlTw7GvMaO7tk9MId5t /zN6samD2h0QW7H//Zsp1TerIU26btHzY7u4xjTDpyAEE/WSSRMpDUAoux/J3+TYDDsK E+83vm3VrlB9tjlcn7HDUkVBWAXH6wb7WT5vL2H2ZAD+seQ63GPEulCJYdxJuNzdMz5V 3wPDCq+T9kJEtFfoY1+QHprnVmhV1dMvCm6Wcr9YZpn2cJBAMGo0V9DsWNmwi5CzUsSQ ByYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=sqUcgJHR72EYMsKQ8/xoAmyUV2iLCHozwLzNy8nhK64=; b=slsdtyCrj3fHYmty53FAwy1HPep8GA3Tpqa87FIX/Sx3dHYh9zg6iNy0UA4+x4jdB+ WENA/5r2qdlHxdM8N6QfQaIQ2dHfWfaHEniZg4G/+AxPvj0KqfT9Mo+Jrz4Wi+Rc8kYK vT37WczJaDOoUu88pb23ORAtWkj4H0JIWR+9328vG2tXpFTeZnjgcqjGdSc75WNAo1AJ PF1IxlH+8r/Jbx1j49G0DRBgTKpIDttOjreE2MD46kUCbAuFrpwSW2F07tU+g4bmqJm8 LhEW91LEMThaQaxuLqtAss7UOJ/vWBd3J9bwnolGnKciKhzzw6rSY84JVM9w03usTriC m44Q== X-Gm-Message-State: ALQs6tCtVwfI0vmDcJ5N5lul6vDiQunyE1PAqRX74YL5O2wFS54iIGQF vthWeNDb2U4cJ23o1z4qBpBQRQ== X-Google-Smtp-Source: AB8JxZr7OAP4zCe094Vzq8pG6C5IR87dXpaCYmXsgIVaHHgiJ/4iE9IDSHob9fyxExi9Eo3+ZnLyjA== X-Received: by 2002:a17:902:788e:: with SMTP id q14-v6mr11692220pll.98.1525083984083; Mon, 30 Apr 2018 03:26:24 -0700 (PDT) From: Shubham Jain To: patchew-devel@redhat.com Date: Mon, 30 Apr 2018 15:56:09 +0530 Message-Id: <20180430102609.14386-1-shubhamjain7495@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Mon, 30 Apr 2018 10:26:25 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Mon, 30 Apr 2018 10:26:25 +0000 (UTC) for IP:'209.85.192.178' DOMAIN:'mail-pf0-f178.google.com' HELO:'mail-pf0-f178.google.com' FROM:'shubhamjain7495@gmail.com' RCPT:'' X-RedHat-Spam-Score: 2.55 ** (DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, FREEMAIL_REPLY, NML_ADSP_CUSTOM_MED, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_PASS, T_DKIM_INVALID) 209.85.192.178 mail-pf0-f178.google.com 209.85.192.178 mail-pf0-f178.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.27 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: patchew-devel@redhat.com Cc: Shubham Jain Subject: [Patchew-devel] [PATCH v2] serializer: Add custom serialzer for recipients and sender 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.49]); Mon, 30 Apr 2018 10:26:40 +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" - Earlier, recipients and sender were read only serializers and could not b= e used for POST/PUT/PATCH requests. Change it to custome serializer for POS= T request at api/v1/messages/ - Add test corresponding to it --- api/rest.py | 36 +++++++++++++++++++----------------- tests/test_rest.py | 17 +++++++++++++++++ 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/api/rest.py b/api/rest.py index e71bf0b..5610844 100644 --- a/api/rest.py +++ b/api/rest.py @@ -19,7 +19,7 @@ from .search import SearchEngine from rest_framework import (permissions, serializers, viewsets, filters, mixins, generics, renderers) from rest_framework.decorators import detail_route -from rest_framework.fields import SerializerMethodField, CharField, JSONFi= eld +from rest_framework.fields import SerializerMethodField, CharField, JSONFi= eld, EmailField from rest_framework.relations import HyperlinkedIdentityField from rest_framework.response import Response import rest_framework @@ -118,6 +118,22 @@ class HyperlinkedMessageField(HyperlinkedIdentityField= ): kwargs =3D {'projects_pk': obj.project_id, self.lookup_field: obj.= message_id} return self.reverse(view_name, kwargs=3Dkwargs, request=3Drequest,= format=3Dformat) =20 +class AddressSerializer(serializers.Serializer): + name =3D CharField(required=3DFalse) + address =3D EmailField() + def to_representation(self,obj): + if obj[0] !=3D obj[1]: + return {"name":obj[0],"address":obj[1]} + else: + return {"address":obj[1]} + + def create(self,validated_data): + try: + return [validated_data['name'],validated_data['address']] + except: + return [validated_data['address'],validated_data['address']] + + class BaseMessageSerializer(serializers.ModelSerializer): class Meta: model =3D Message @@ -125,21 +141,8 @@ class BaseMessageSerializer(serializers.ModelSerialize= r): =20 resource_uri =3D HyperlinkedMessageField(view_name=3D'messages-detail') =20 - recipients =3D SerializerMethodField() - sender =3D SerializerMethodField() - - def format_name_addr(self, name, addr): - d =3D {} - if name !=3D addr: - d['name'] =3D name - d['address'] =3D addr - return d - - def get_recipients(self, obj): - return [self.format_name_addr(*x) for x in obj.recipients] - - def get_sender(self, obj): - return self.format_name_addr(*obj.sender) + recipients =3D AddressSerializer(many=3DTrue) + sender =3D AddressSerializer() =20 # a message_id is *not* unique, so we can only list class BaseMessageViewSet(mixins.ListModelMixin, viewsets.GenericViewSet): @@ -280,7 +283,6 @@ class ProjectSeriesViewSet(ProjectMessagesViewSetMixin, # Messages =20 # TODO: add POST endpoint connected to email plugin? - class MessageSerializer(BaseMessageSerializer): class Meta: model =3D Message diff --git a/tests/test_rest.py b/tests/test_rest.py index d1fc80f..21bc2b9 100755 --- a/tests/test_rest.py +++ b/tests/test_rest.py @@ -16,6 +16,8 @@ from django.contrib.auth.models import User sys.path.append(os.path.dirname(__file__)) from patchewtest import PatchewTestCase, main from api.models import Message +from api.rest import AddressSerializer +from collections import OrderedDict =20 class RestTest(PatchewTestCase): def setUp(self): @@ -270,6 +272,21 @@ class RestTest(PatchewTestCase): resp =3D self.client.get(message + 'mbox/') self.assertEqual(resp.data, Message.objects.all()[0].get_mbox()) =20 + def test_address_serializer(self): + data1 =3D {"name":"Shubham", "address":"shubhamjain7495@gmail.com"} + serializer1 =3D AddressSerializer(data =3D data1) + valid1 =3D serializer1.is_valid() + valid_data1 =3D serializer1.validated_data + data2 =3D {"name":123, "address":"shubhamjain7495@gmail.com"} + serializer2 =3D AddressSerializer(data =3D data2) + valid2 =3D serializer2.is_valid() + valid_data2 =3D serializer2.validated_data + + self.assertEqual(valid1,True) + self.assertEqual(valid_data1,OrderedDict([('name', 'Shubham'), ('a= ddress', 'shubhamjain7495@gmail.com')])) + self.assertEqual(valid2,True) + self.assertEqual(valid_data2,OrderedDict([('name', '123'), ('addre= ss', 'shubhamjain7495@gmail.com')])) + def test_message_replies(self): series =3D self.apply_and_retrieve('0004-multiple-patch-reviewed.m= box.gz', self.p.id, '1469192015-16487-1-gi= t-send-email-berrange@redhat.com') --=20 2.14.3 (Apple Git-98) _______________________________________________ Patchew-devel mailing list Patchew-devel@redhat.com https://www.redhat.com/mailman/listinfo/patchew-devel