From nobody Fri Apr 26 14:46:29 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 1525065078832421.1811644955352; Sun, 29 Apr 2018 22:11:18 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C28694E4C3; Mon, 30 Apr 2018 05:11:17 +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 B16113091AA6; Mon, 30 Apr 2018 05:11:17 +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 A16EF4CAA6; Mon, 30 Apr 2018 05:11:17 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w3U5BGpj018973 for ; Mon, 30 Apr 2018 01:11:16 -0400 Received: by smtp.corp.redhat.com (Postfix) id 603781895B; Mon, 30 Apr 2018 05:11:16 +0000 (UTC) Received: from mx1.redhat.com (ext-mx11.extmail.prod.ext.phx2.redhat.com [10.5.110.40]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 593AE60BEB for ; Mon, 30 Apr 2018 05:11:12 +0000 (UTC) Received: from mail-pf0-f170.google.com (mail-pf0-f170.google.com [209.85.192.170]) (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 B57E530014D3 for ; Mon, 30 Apr 2018 05:11:01 +0000 (UTC) Received: by mail-pf0-f170.google.com with SMTP id a11so5841707pfn.9 for ; Sun, 29 Apr 2018 22:11:01 -0700 (PDT) Received: from localhost.localdomain ([112.196.135.75]) by smtp.gmail.com with ESMTPSA id q64sm10778017pfg.180.2018.04.29.22.10.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 29 Apr 2018 22:10:59 -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=6YezAfjwppQNqpKxPRQsAa1Leif+OGmjWaB6k07kXGA=; b=p8exLUXTAj5Oz4I7ojvNo10oTLOeV6plBdML5yJcC/qPPVOE/exoyV7H5CR2SEnGYl rC95We+krZVJqO4dJaIaz5vwqbSld+OA5T2rPbO4A3N5DXV9TU4hPUC01jloeRyFc2qs SqAC4GVmuauQ/Imu5Tr6cKHisuUEP52OH/gXm86ePNBYtlT17r2r7K7yVDDEEdNGM7XX VS8lx2K4E9XVTwThPtLLFqXU7evsThuBXuU3Ud93HcAcfZOCLGdlP89xHKXjr9hnxLjK +9GflpoVj7oowGAFAvHdk9zziXjLWKwd8VvpYDmiWlD7xmzFznZoUczjzUK1ru5rMSpU baog== 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=6YezAfjwppQNqpKxPRQsAa1Leif+OGmjWaB6k07kXGA=; b=jCcn3WC7ikBxuafQYWVN3OXKM7kx9QOkdaEv/6NaWGIXNfY6gi3drgVe03ILlVFt28 V41yO9H7QwWgOwgeq7LfQdVQUDBjwZxiG81h/aKqMZwIxsYyMl+Am7uEE8gR6rr/jClX Sdj17RkI5p6i6z7Icl+jBAg8CAvoo18lDWJlc04hrTu0O29R9tVepxHDyanuuBHHDqrZ uBaV0eMOmg2A84pYJKRhsFDD7aOawsju+tBEKEvDAoAt3gWEmYXcjJT05QdYucjc4m7Z JHvh690wTDZzJnA3o9tSqE5fuFFM8qqODKZ2rm3gsxwTCl5PwVIbS4L/vAme6wkwphkR yJVg== X-Gm-Message-State: ALQs6tCgYuX6XtKvqAopK6XQXKr1aF6crspQvUrnBXNnXANb2FEuFCcs x2+iyw/w9xDIRe4LdPt1JP19uA== X-Google-Smtp-Source: AB8JxZpgojb/vNOBvg7sLxvMmp41rWTAmNbyyRcYWtr7GoFX2DWzY0rLcZalwr2gxSQkO+vr8AXDxg== X-Received: by 2002:a65:53c5:: with SMTP id z5-v6mr9280765pgr.413.1525065060512; Sun, 29 Apr 2018 22:11:00 -0700 (PDT) From: Shubham Jain To: patchew-devel@redhat.com Date: Mon, 30 Apr 2018 10:40:45 +0530 Message-Id: <20180430051045.7151-1-shubhamjain7495@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.40]); Mon, 30 Apr 2018 05:11:01 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.40]); Mon, 30 Apr 2018 05:11:01 +0000 (UTC) for IP:'209.85.192.170' DOMAIN:'mail-pf0-f170.google.com' HELO:'mail-pf0-f170.google.com' FROM:'shubhamjain7495@gmail.com' RCPT:'' X-RedHat-Spam-Score: 1.55 * (DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, NML_ADSP_CUSTOM_MED, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_PASS, T_DKIM_INVALID) 209.85.192.170 mail-pf0-f170.google.com 209.85.192.170 mail-pf0-f170.google.com X-Scanned-By: MIMEDefang 2.84 on 10.5.110.40 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: patchew-devel@redhat.com Cc: Shubham Jain Subject: [Patchew-devel] [PATCH] 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.84 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Mon, 30 Apr 2018 05:11:17 +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 be = used for POST/PUT/PATCH requests. Change it to custome serializer for POST = request at api/v1/messages/ --- api/rest.py | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/api/rest.py b/api/rest.py index e71bf0b..70547d3 100644 --- a/api/rest.py +++ b/api/rest.py @@ -118,6 +118,23 @@ 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): + """ """ + def to_representation(self,obj): + if obj[0] !=3D obj[1]: + return {"name":obj[0],"address":obj[1]} + else: + return {"address":obj[1]} + + def to_internal_value(self, data): + try: + return OrderedDict([('name',data['name']),('address',data['add= ress'])]) + except: + return OrderedDict([('name',data['address']),('address',data['= address'])]) + + def create(self,validated_data): + return [validated_data['name'],validated_data['address']] + class BaseMessageSerializer(serializers.ModelSerializer): class Meta: model =3D Message @@ -125,21 +142,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 +284,6 @@ class ProjectSeriesViewSet(ProjectMessagesViewSetMixin, # Messages =20 # TODO: add POST endpoint connected to email plugin? - class MessageSerializer(BaseMessageSerializer): class Meta: model =3D Message --=20 2.14.3 (Apple Git-98) _______________________________________________ Patchew-devel mailing list Patchew-devel@redhat.com https://www.redhat.com/mailman/listinfo/patchew-devel