[Patchew-devel] [PATCH v2] Text to json convertor for mbox

Paolo Bonzini pbonzini at redhat.com
Fri Apr 27 16:05:31 UTC 2018


On 27/04/2018 14:32, Shubham Jain wrote:
> 
> 
> On Fri, Apr 27, 2018 at 5:37 PM Paolo Bonzini <pbonzini at redhat.com
> <mailto:pbonzini at redhat.com>> wrote:
> 
>     Very good progress, thanks!
> 
>     On 27/04/2018 13:53, Shubham Jain wrote:
>     >  class BaseMessageViewSet(mixins.ListModelMixin,
>     viewsets.GenericViewSet):
>     > @@ -285,7 +285,8 @@ class
>     ProjectSeriesViewSet(ProjectMessagesViewSetMixin,
>     >  class MessageSerializer(BaseMessageSerializer):
>     >      class Meta:
>     >          model = Message
>     > -        fields = BaseMessageSerializer.Meta.fields + ('mbox', )
>     > +        # fields = BaseMessageSerializer.Meta.fields + ('mbox', )
>     > +        fields = '__all__'
> 
>     What is fields = '__all__'? :)
> 
> Since you mentioned to use browsable api "form" to check for POST. The
> "__all__" helps to get all the fields in the model. This would help with
> the fields which were dependent on mbox since we are not providing any
> mbox. 

Ok, one thing at a time.  First of all let's apply the mbox.py and
tests/test_mbox.py parts.  That's ready except that msg['mbox'] should
be populated as well.

Second, you can apply the JSONField patch locally, and use it to define
a custom serializer for names and addresses.  This will get all fields
right except mbox.

Finally, we need to get the mbox field right.  The "best" way would be
to define a custom Django Field class.  A slightly simpler way would be to:

- rename the existing "mbox" field to e.g. "mbox_blob"

- add getters and setters for "mbox".

- override "save" for Message so that it calls save_mbox() if creating a
new message ("self.pk is None":
https://stackoverflow.com/questions/907695/in-a-django-model-custom-save-method-how-should-you-identify-a-new-object)

I placed a checklist for this at
https://github.com/patchew-project/patchew/issues/75.  Each item can be
a separate patch.  If you have a GitHub account we can give you edit access.

Paolo
> 
> 
>     >      def get_mbox(self, obj):
>     >          return obj.get_mbox()
> 
>     As far as I understand, read_only=True is used to make fields that are
>     included in the API output, but should not be included in the
>     POST/PUT/PATCH input. I thought SerializerMethodField had
> 
>     read_only=True automatically, but maybe it doesn't?
> 
> Yeah this won't work with POST/PUT/PATCH
> (https://github.com/encode/django-rest-framework/blob/master/rest_framework/fields.py> 
>     > @@ -311,9 +312,9 @@ class StaticTextRenderer(renderers.BaseRenderer):
>     >              return data
>>     >  class MessagesViewSet(ProjectMessagesViewSetMixin,
>     > -                      BaseMessageViewSet):
>     > +                      BaseMessageViewSet, mixins.CreateModelMixin):
>     >      serializer_class = MessageSerializer
>     > -
>     > +   
>     >      @detail_route(renderer_classes=[StaticTextRenderer])
>     >      def mbox(self, request, *args, **kwargs):
>     >          message = self.get_object()
> 
>     Can you also write a testcase for the POST operation?  You can create a
>     new file 0001-simple-patch.json.gz to keep the testcase code simple.
> 
> Yes :) 
> 
>     > +        msg['sender'] = json.dumps(self.get_from())
>     > +        msg['recipients'] = json.dumps(self.get_to() + self.get_cc())
> 
>     The json.dumps of course is only temporary; it can be removed when my
>     JSONField patch is applied and the sender/recipients
>     SerializerMethodFields are replaced by a Serializer.
> 
>     > +                    'sender': '["Fam Zheng", "famz at redhat.com
>     <mailto:famz at redhat.com>"]',
>     > +                    'recipients': '[["qemu-devel at nongnu.org
>     <mailto:qemu-devel at nongnu.org>", "qemu-devel at nongnu.org
>     <mailto:qemu-devel at nongnu.org>"], ["Kevin Wolf", "kwolf at redhat.com
>     <mailto:kwolf at redhat.com>"], ["Alberto Garcia", "berto at igalia.com
>     <mailto:berto at igalia.com>"], ["qemu-block at nongnu.org
>     <mailto:qemu-block at nongnu.org>", "qemu-block at nongnu.org
>     <mailto:qemu-block at nongnu.org>"], ["Max Reitz", "mreitz at redhat.com
>     <mailto:mreitz at redhat.com>"]]',
> 
>     So that these also becomes arrays rather than strings, and the
>     individual pairs use {'name': ..., 'address': ...} as in the GET output.
> 
>     Thanks,
> 
>     Paolo
> 




More information about the Patchew-devel mailing list