[Patchew-devel] [PATCH] serializer: Add custom serialzer for recipients and sender

Shubham Jain posted 1 patch 6 years, 2 months ago
Failed in applying to current master (apply log)
There is a newer version of this series
api/rest.py | 35 +++++++++++++++++++----------------
1 file changed, 19 insertions(+), 16 deletions(-)
[Patchew-devel] [PATCH] serializer: Add custom serialzer for recipients and sender
Posted by Shubham Jain 6 years, 2 months ago
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 = {'projects_pk': obj.project_id, self.lookup_field: obj.message_id}
         return self.reverse(view_name, kwargs=kwargs, request=request, format=format)
 
+class AddressSerializer(serializers.Serializer):
+    """ """
+    def to_representation(self,obj):
+        if obj[0] != 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['address'])])
+        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 = Message
@@ -125,21 +142,8 @@ class BaseMessageSerializer(serializers.ModelSerializer):
 
     resource_uri = HyperlinkedMessageField(view_name='messages-detail')
 
-    recipients = SerializerMethodField()
-    sender = SerializerMethodField()
-
-    def format_name_addr(self, name, addr):
-        d = {}
-        if name != addr:
-            d['name'] = name
-        d['address'] = 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 = AddressSerializer(many=True)
+    sender = AddressSerializer()
 
 # 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
 
 # TODO: add POST endpoint connected to email plugin?
-
 class MessageSerializer(BaseMessageSerializer):
     class Meta:
         model = Message
-- 
2.14.3 (Apple Git-98)

_______________________________________________
Patchew-devel mailing list
Patchew-devel@redhat.com
https://www.redhat.com/mailman/listinfo/patchew-devel
Re: [Patchew-devel] [PATCH] serializer: Add custom serialzer for recipients and sender
Posted by Paolo Bonzini 6 years, 2 months ago
On 30/04/2018 07:10, Shubham Jain wrote:
> 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 = {'projects_pk': obj.project_id, self.lookup_field: obj.message_id}
>          return self.reverse(view_name, kwargs=kwargs, request=request, format=format)
>  
> +class AddressSerializer(serializers.Serializer):
> +    """ """
> +    def to_representation(self,obj):
> +        if obj[0] != 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['address'])])
> +        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 = Message
> @@ -125,21 +142,8 @@ class BaseMessageSerializer(serializers.ModelSerializer):

Generally okay, but you are lacking validation (for example you could
use {'name': 123} and it would be accepted.  You need to use name and
address fields.

It's also missing testcases.  Including a testcase for invalid types
seems like a good idea now!

Paolo

>  
>      resource_uri = HyperlinkedMessageField(view_name='messages-detail')
>  
> -    recipients = SerializerMethodField()
> -    sender = SerializerMethodField()
> -
> -    def format_name_addr(self, name, addr):
> -        d = {}
> -        if name != addr:
> -            d['name'] = name
> -        d['address'] = 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 = AddressSerializer(many=True)
> +    sender = AddressSerializer()
>  
>  # 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
>  
>  # TODO: add POST endpoint connected to email plugin?
> -
>  class MessageSerializer(BaseMessageSerializer):
>      class Meta:
>          model = Message
> 

_______________________________________________
Patchew-devel mailing list
Patchew-devel@redhat.com
https://www.redhat.com/mailman/listinfo/patchew-devel