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

Shubham Jain posted 1 patch 6 years, 2 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/patchew-ci tags/patchew/20180424160400.29938-1-shubhamjain7495@gmail.com
There is a newer version of this series
api/rest.py | 19 ++++++++++++++++++-
mbox.py     | 21 +++++++++++++++++++++
2 files changed, 39 insertions(+), 1 deletion(-)
[Patchew-devel] [PATCH] Text to json convertor for mbox
Posted by Shubham Jain 6 years, 2 months ago
Mbox would now return a dictionary object which would help REST API deserialize easily into model object.

[WIP] POST endpoint for messages.

It's a work in progress for creating new message which is improvement of legacy endpoint: "import"
---
 api/rest.py | 19 ++++++++++++++++++-
 mbox.py     | 21 +++++++++++++++++++++
 2 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/api/rest.py b/api/rest.py
index 6aa744d..ee814e8 100644
--- a/api/rest.py
+++ b/api/rest.py
@@ -303,8 +303,25 @@ class StaticTextRenderer(renderers.BaseRenderer):
             return data
 
 class MessagesViewSet(ProjectMessagesViewSetMixin,
-                      BaseMessageViewSet):
+                      BaseMessageViewSet, mixins.CreateModelMixin):
     serializer_class = MessageSerializer
+    
+    def create(self, request, *args, **kwargs):
+        def find_message_projects(m):
+            return [p for p in Project.objects.all() if p.recognizes(m)]
+            
+        msg = MboxMessage(request.data)
+        project = find_message_projects(msg)
+        msg.project = project
+        serializer = self.get_serializer(data=msg.get_json())
+        serializer.is_valid(raise_exception=True)
+        self.perform_create(serializer)
+        headers = self.get_success_headers(serializer.data)
+        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
+
+
+    def perform_create(self, serializer):
+        serializer.save()
 
     @detail_route(renderer_classes=[StaticTextRenderer])
     def mbox(self, request, *args, **kwargs):
diff --git a/mbox.py b/mbox.py
index 3692cc6..6b42e8f 100644
--- a/mbox.py
+++ b/mbox.py
@@ -13,6 +13,7 @@ import email.utils
 import email.header
 import datetime
 import re
+import json
 
 def _parse_header(header):
     r = ''
@@ -267,3 +268,23 @@ class MboxMessage(object):
         if c == 0:
             return True
         return False
+
+    def get_json(self):
+        """
+        """
+        msg = {}
+        msg['message_id'] = self.get_message_id()
+        msg['in_reply_to'] = self.get_in_reply_to() or ""
+        msg['date'] = self.get_date()
+        msg['subject'] = self.get_subject()
+        msg['stripped_subject'] = self.get_subject(strip_tags=True)
+        msg['version'] = self.get_version()
+        msg['sender'] = json.dumps(self.get_from())
+        msg['recipients'] = json.dumps(self.get_to() + self.get_cc())
+        msg['prefixes']=json.dumps(self.get_prefixes())
+        msg['is_series_head'] = self.is_series_head()
+        msg['is_patch'] = self.is_patch()
+        msg['patch_num'] = self.get_num()[0]
+        msg['project'] = self.project 
+        msg['mbox'] = self._mbox
+        return msg
-- 
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] Text to json convertor for mbox
Posted by Paolo Bonzini 6 years, 2 months ago
On 24/04/2018 18:04, Shubham Jain wrote:
>  class MessagesViewSet(ProjectMessagesViewSetMixin,
> -                      BaseMessageViewSet):
> +                      BaseMessageViewSet, mixins.CreateModelMixin):
>      serializer_class = MessageSerializer
> +    
> +    def create(self, request, *args, **kwargs):
> +        def find_message_projects(m):
> +            return [p for p in Project.objects.all() if p.recognizes(m)]

Note that this is returning a list.  Here you do have a project however,
because MessagesViewSet has a projects_pk field in kwargs (it's
registered under projects_router in api/urls.py).  So
find_message_projects is something that you'd add later, when you add
/api/v1/messages/.  For now you're only dealing with
/api/v1/projects/{id}/messages/.

> +        msg = MboxMessage(request.data)

Later you would check the request.content_type, and go through
MboxMessage and msg.get_json() only if it's text/plain.

> +        project = find_message_projects(msg)
> +        msg.project = project

MboxMessage doesn't have a project field.  I think you should first
retrieve the JSON with msg.get_json(), and then set the project URI into
the JSON, for example using Django REST Framework's reverse lookup function.

> +        serializer = self.get_serializer(data=msg.get_json())
> +        serializer.is_valid(raise_exception=True)
> +        self.perform_create(serializer)
> +        headers = self.get_success_headers(serializer.data)
> +        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
> +
> +    def perform_create(self, serializer):
> +        serializer.save()
>  

perform_create is the same as in CreateModelMixin so you can remove it.

I think you are a little confused as to what you are trying to achieve.
As a first step you should:

1) write a test case for MboxMessage.get_json()

2) just add CreateModelMixin and see what happens in the API browser.
Write a test case that POSTs a new message with JSON format.

3) only once you have the above you can start looking at adding
/api/v1/messages/.

Thanks,

Paolo

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